Spring Boot Using Embedded Tomcat with JNDI

谁说胖子不能爱 提交于 2020-01-10 04:19:27

问题


I am using Spring Boot with Embedded Tomcat and attempting to use JNDI but getting the following error:

javax.naming.NameNotFoundException: Name [jdbc/dataSource]

Any tips would be greatly appreciated.

Here is my code:

@Configuration
public class TomcatJndiConfiguration{

@Value("${database.driver}")
private String driverClassName;

@Value("${database.url}")
private String databaseUrl;

@Value("${database.username}")
private String databaseUsername;

@Value("${database.password}")
private String databasePassword;

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/dataSource");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", driverClassName);
            resource.setProperty("url", databaseUrl);
            resource.setProperty("password", databaseUsername);
            resource.setProperty("username", databasePassword);
            context.getNamingResources().addResource(resource);
        }
    };
}

@Bean
public DataSource dataSource() throws IllegalArgumentException, NamingException {
    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setJndiName("jdbc/dataSource");
    bean.setLookupOnStartup(true);
    bean.setProxyInterface(DataSource.class);
    bean.setResourceRef(true);
    bean.afterPropertiesSet();
    return (DataSource) bean.getObject();

}
Stacktrace is: 
Caused by: javax.naming.NameNotFoundException: Name [jdbc/dataSource] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
at org.springframework.jndi.JndiObjectTargetSource.afterPropertiesSet(JndiObjectTargetSource.java:97)
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.createJndiObjectProxy(JndiObjectFactoryBean.java:318)
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.access$000(JndiObjectFactoryBean.java:307)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:200)
at com.kronos.daas.configuration.TomcatJndiConfiguration.dataSource(TomcatJndiConfiguration.java:72)

回答1:


You need to set lookupOnStartup to false on the JndiObjectFactoryBean.

Alternatively, if you need the lookup to work during startup, then this answer may be of interest.

Edit: you've also set the JNDI name on your JndiObjectFactory bean incorrectly. It needs to be java:comp/env/jdbc/myDataSource not jdbc/dataSource.

You use a different name when you're looking up the resource versus when you registered it as the registration automatically places the resource beneath java:comp/env/.




回答2:


If you are using spring boot, no need for all of that class. It is already configured in @EnableAutoConfiguration or @SpringBootApplication

Just put the following in your application.xml file or equivalent in application.yml file

spring.datasource.driverClassName=JDBCDriverClassName
spring.datasource.url=databaseUrl
spring.datasource.username=databaseUsername
spring.datasource.password=databasePassword
spring.datasource.jndi-name=java:jdbc/dataSource


来源:https://stackoverflow.com/questions/28131577/spring-boot-using-embedded-tomcat-with-jndi

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