Spring引导JNDI数据源查找失败 - 在上下文中找不到名称comp / env / jdbc“java:”

时间:2014-12-15 07:31:22

标签: datasource war jndi spring-boot websphere-8

我已经设置了一个spring boot(v 1.1.9)应用程序来部署为WAR文件。我试图将此Web应用程序与现有数据服务模块集成(作为maven依赖项添加)。

尝试部署的环境: WebSphere Application Server 8.5.5.4

我遇到的问题是,当尝试在依赖数据服务模块中查找JNDI数据源( jdbc / fileUploadDS )时,应用程序启动失败。

@Configuration
@Profile("prod")
public class JndiDataConfig implements DataConfig {

    @Bean
    public DataSource dataSource() throws NamingException {
          Context ctx = new InitialContext();
          return (DataSource) ctx.lookup("java:comp/env/jdbc/fileUploadDS");
    }

}

我的Spring Boot配置:

@Configuration
@ComponentScan(basePackages = { "au.com.aiaa.fileupload.data.*", "demo" })
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class })
public class SampleApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    private static Class<SampleApplication> applicationClass = SampleApplication.class;

    @Bean
    public static Properties fileUploadJndiProperties() throws NamingException {
        JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean();
        jndiFactoryBean.setJndiName("props/FileUploadProperties");
        jndiFactoryBean.setExpectedType(Properties.class);
        jndiFactoryBean.setLookupOnStartup(true);
        jndiFactoryBean.afterPropertiesSet();
        return (Properties) jndiFactoryBean.getObject();
    }

}

请注意,我能够成功查找props / FileUploadProperties。但是没有为数据源做同样的事情。

我怀疑它是否正在尝试加载 EmbeddedWebApplicationContext ,这不是我想要的。

堆栈跟踪是:

Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource() throws javax.naming.NamingException] threw exception; nested exception is **javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".**
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
        at **org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)**
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:142)
        at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:89)
        at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:51)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)


..................

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource() throws javax.naming.NamingException] threw exception; nested exception is **javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".**
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
        ... 132 common frames omitted
Caused by: javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
        at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970)
        at com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377)
        at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220)
        at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142)
        at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436)
        at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477)
        at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
        at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)
        at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
        at javax.naming.InitialContext.lookup(InitialContext.java:436)
        at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource(JndiDataConfig.java:41)
        at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe.CGLIB$dataSource$0(<generated>)
        at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe$$FastClassBySpringCGLIB$$3c9e0518.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
        at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe.dataSource(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
        at java.lang.reflect.Method.invoke(Method.java:611)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)

我在这里缺少什么?即使我尝试在SampleApplication.java中显式定义dataSource bean方法,如下所示也会失败并出现相同的错误。

@Bean
    public static DataSource dataSource() throws NamingException {
        JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean();
        jndiFactoryBean.setJndiName("java:comp/env/jdbc/fileUploadDS");
        jndiFactoryBean.setExpectedType(DataSource.class);
        jndiFactoryBean.setLookupOnStartup(true);
        jndiFactoryBean.setResourceRef(true);
        jndiFactoryBean.afterPropertiesSet();
        return (DataSource) jndiFactoryBean.getObject();
    }

我提到了this,它说我们需要在servlet容器上设置enableNaming()?我可以为非嵌入式Web应用程序上下文执行类似的操作吗?或者它纯粹是一个WAS 8.5问题?

4 个答案:

答案 0 :(得分:8)

您需要在jdbc/fileUploadDS中使用web.xml名称提供资源参考。并确保在安装期间或通过ibm-web-bnd.xml文件绑定到实际数据源名称。

web.xml中的定义:

<resource-ref>
    <description />
    <res-ref-name>jdbc/fileUploadDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

如果您不想使用web.xml,那么在普通的Java EE应用程序中,您只需在Web组件(servlet,filter)中添加以下类注释:

@Resource(name="jdbc/fileUploadDS", type=javax.sql.DataSource.class, lookup="jdbc/fileUploadDS")

但我不是Spring-boot专家,所以不知道,它是否可行或是否可行。

答案 1 :(得分:1)

我面临同样的问题。我不知道如何在spring boot中定义标记,因为spring boot中没有web.xml文件。

到目前为止,我才知道我们必须在应用程序文件中定义它,从我们开始使用spring应用程序。我想我们需要使用这个方法来设置数据源:

  @Bean(destroyMethod="")
  @ConfigurationProperties(prefix="datasource.mine")
  public DataSource dataSource() throws Exception {
      JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
      return dataSourceLookup.getDataSource("java:jdbc/configurationFile");
  }

答案 2 :(得分:1)

我能够将我的Spring-Boot应用程序(部署在Websphere Application Server 9中)连接到WAS数据源。 以下代码适用于我,用于连接到DataSource:

@Bean(name = "WASDataSource")
public DataSource WASDataSource() throws Exception {        
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
return dataSourceLookup.getDataSource("DSNAME");
}   

@Bean(name = "WASDataSourceJdbcTemplate")
public JdbcTemplate jdbcTemplate_WASDS(@Qualifier("WASDataSource")
DataSource dsDB2) {
return new JdbcTemplate(dsDB2);
}

注意:数据源“DSNAME”的名称是显示在Websphere控制台UI上的名称。 你可以看到via - &gt;选择资源&gt; JDBC&gt;数据源。

然后我创建了jdbc模板:

@Autowired
@Qualifier("WASDataSourceJdbcTemplate")
private JdbcTemplate db2WASTemplate;`

使用查询方法运行查询工作正常:
    db2WASTemplate.query()

我没有创建任何Web.xml或ibm-web-bnd.xml文件

答案 3 :(得分:0)

我刚刚使用自定义数据源配置了spring boot,如下所示:

@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

并且在application.properties文件中我像往常一样定义了所有数据源设置

spring.datasource.driver-class-name= ***
spring.datasource.url= ***
spring.datasource.username= ***
spring.datasource.password= ***
spring.datasource.jndi-name=jdbc/myDB

与@SpringBootApplication一起使用所有其他默认设置

并不合适

由于