Spring Boot JNDI resource-ref

不打扰是莪最后的温柔 提交于 2019-12-23 12:35:01

问题


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?

Thanks, Benjamin


回答1:


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


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

spring:
  datasource:
    jndi-name: jdbc/DefaultDB

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());
    }
}


来源:https://stackoverflow.com/questions/36202920/spring-boot-jndi-resource-ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!