Spring Boot JNDI资源引用 [英] Spring Boot JNDI resource-ref

查看:117
本文介绍了Spring Boot JNDI资源引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Spring Boot应用程序中声明一个资源引用,该资源将作为war文件部署。为了访问数据库,这是必需的。在传统的Web应用程序中,这已添加到web.xml中。如何以Spring Boot方式实现这一目标?

I need to declare a resource-ref in a Spring Boot application that will be deployed as war file. This is needed in order to access the database. In traditional webapps, this is added to the web.xml. How can I achieve this in a Spring Boot way?

谢谢,
Benjamin

Thanks, Benjamin

推荐答案

前一阵子我们解决了这个问题,所以我想将其发布在这里。

We got that resolved a while back, so I thought, I'd post this here.

src / main / webapp / WEB-INF / web.xml

src/main/webapp/WEB-INF/web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
  <resource-ref>
    <res-ref-name>jdbc/DefaultDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
  </resource-ref>
</web-app>






src / main / resources / application.yml


src/main/resources/application.yml

spring:
  datasource:
    jndi-name: jdbc/DefaultDB






src / main / java // DataSourceConfiguration.java


src/main/java//DataSourceConfiguration.java

@Configuration
public class DataSourceConfiguration {

    @Bean
    @ConditionalOnMissingBean // optional
    public DataSource jndiDataSource(DataSourceProperties properties) {
        InitialContext context = new InitialContext();
        return (DataSource) context.lookup("unmanageddatasource:" + properties.getJndiName());
    }
}

这篇关于Spring Boot JNDI资源引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆