Spring Boot JNDI数据源查找失败-在上下文“ java:”中找不到名称comp / env / jdbc [英] Spring boot JNDI datasource lookup failure - Name comp/env/jdbc not found in context "java:"

查看:381
本文介绍了Spring Boot JNDI数据源查找失败-在上下文“ java:”中找不到名称comp / env / jdbc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我面临的问题是尝试查找JNDI dataSource时应用程序启动失败( jdbc /在相关数据服务模块中如下所示。

  @Configuration 
@Profile( prod )
公共类JndiDataConfig实现了DataConfig {

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

}

我的Spring Boot配置:

  @Configuration 
@ComponentScan(basePackages = { au.com.aiaa.fileupload.data。*,演示})
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class,DataSourceAutoConfiguration.class})
公共类SampleApplication扩展了SpringBootServletInitializer {

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

@Override
受保护的SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(applicationClass);
}

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

@Bean
公共静态属性fileUploadJndiProperties()引发NamingException {
JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean();
jndiFactoryBean.setJndiName( props / FileUploadProperties);
jndiFactoryBean.setExpectedType(Properties.class);
jndiFactoryBean.setLookupOnStartup(true);
jndiFactoryBean.afterPropertiesSet();
return(Properties)jndiFactoryBean.getObject();
}

}

请注意,我能够查找道具/ FileUploadProperties成功。但是无法对数据源执行相同操作。



我的疑问是它正在尝试加载不是我想要的 EmbeddedWebApplicationContext 。 / p>

堆栈跟踪为:

 实例化bean失败;嵌套的异常是org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[公共javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource()抛出javax.naming.NamingException]引发了异常;嵌套的异常是** javax.naming.NameNotFoundException:在上下文 java:中找不到名称comp / env / jdbc。** org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java上的
:301)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java :537)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory。 java:302)
,位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
在org.org .springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at ** org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)**
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
在org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
在org.springfram ework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:142)
在org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:89)在org.springframework的
。 boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:51)
在org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)


。 .................

原因:org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public javax.sql.DataSource au.com .aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource()引发javax.naming.NamingException]引发异常;嵌套的异常是** javax.naming.NameNotFoundException:在上下文 java:中找不到名称comp / env / jdbc。** org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java中的
:188)在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)中的
...
...省略了132个常见框架
原因:javax.naming。 NameNotFoundException:在上下文 java:中找不到名称comp / env / jdbc。 com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970)的
com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377)的
com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220)上的
com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142)上的
com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436)的
com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477)的
com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)的
com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)的
org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)的
javax.naming.InitialContext.lookup(InitialContext.java:436)的
au.com的
.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource(JndiDataConfig.java:4 1)在au.com.aiaa.fileupload.data.dao.configuration上的
。在au.com.aiaa上的JndiDataConfig $$ EnhancerBySpringCGLIB $$ 8001dbbe.CGLIB $ dataSource $ 0(< generated>)
。 fileupload.data.dao.configuration.JndiDataConfig $$ EnhancerBySpringCGLIB $$ 8001dbbe $$ FastClassBySpringCGLIB $$ 3c9e0518.invoke(< Generated>) 228)org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)中的
au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig $$ EnhancerBySpringCGLIB $ 8001dbbe.dataSource(生成的)
在sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
在java.l ang.reflect.Method.invoke(Method.java:611)
在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)

我在这里想念什么?甚至当我尝试在SampleApplication.java中显式定义dataSource bean方法时,如下所示,它也会失败,并出现相同的错误。

  @Bean 
公共静态数据源dataSource()抛出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();
}

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

解决方案

您需要使用 jdbc /获得资源引用 web.xml 中的fileUploadDS 名称。并确保在安装过程中或通过 ibm-web-bnd.xml 文件将其绑定到实际的数据源名称。



web.xml 中的定义:

 < resource-ref> 
<说明/>
< res-ref-name> jdbc / fileUploadDS< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< res-sharing-scope> Shareable< / res-sharing-scope>
< / resource-ref>

如果您不想使用 web.xml ,然后在普通的Java EE应用程序中,您只需在Web组件(Servlet,过滤器)中添加以下类注释:

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

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


I have setup a spring boot (v 1.1.9) application to deploy as a WAR file. And I'm trying to integrate this web application with an existing data service module (added as a maven dependency).

Environment trying to deploy: WebSphere Application Server 8.5.5.4

The issue I'm facing is an application start-up failure when try to look-up a JNDI dataSource (jdbc/fileUploadDS) as below within the dependent data service module.

@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");
    }

}

My Spring Boot configuration:

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

}

Note that I'm able to lookup props/FileUploadProperties successfully. But failing to do the same for a datasource.

My doubt is it is trying to load a EmbeddedWebApplicationContext which is not what I want.

The stack trace is:

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)

What am I missing here? Even when I try to explicitly define the dataSource bean method in SampleApplication.java like below it fails with the same error.

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

I referred this and it says we need to set enableNaming() on servlet container? Can I do something similar for non-embedded web application context? Or is it purely a WAS 8.5 issue??

解决方案

You need to have resource reference with jdbc/fileUploadDS name in your web.xml. And make sure it is bound to actual datasource name during installation or via ibm-web-bnd.xml file.

Definition in 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>

If you dont want to use web.xml, then in normal Java EE app you could just add in web component (servlet, filter) the following class annotation:

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

but I'm not Spring-boot expert, so don't know, if it will work or is possible there.

这篇关于Spring Boot JNDI数据源查找失败-在上下文“ java:”中找不到名称comp / env / jdbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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