使用嵌入式Tomcat和JNDI进行Spring引导

时间:2015-01-24 23:42:24

标签: spring-boot jndi embedded-tomcat-8

我正在使用带有嵌入式Tomcat的Spring Boot并尝试使用JNDI,但收到以下错误:

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

任何提示将不胜感激。

这是我的代码:

@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)

2 个答案:

答案 0 :(得分:1)

您需要在JndiObjectFactoryBean上将lookupOnStartup设置为false。

或者,如果您需要在启动期间进行查找,那么this answer可能会引起您的兴趣。

编辑:您还错误地在JndiObjectFactory bean上设置了JNDI名称。它必须是java:comp/env/jdbc/myDataSource而不是jdbc/dataSource

您在查找资源时使用的名称与注册资源时使用的名称不同,因为注册会自动将资源放在java:comp/env/下面。

答案 1 :(得分:0)

如果您使用的是春季启动,则不需要所有课程。 它已在@EnableAutoConfiguration中配置或 @SpringBootApplication

只需将以下内容放入application.xml文件中的application.xml文件或等效文件

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