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

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

问题描述

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

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

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

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

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

}

我的 Spring Boot 配置:

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

}

请注意,我能够成功查找 props/FileUploadProperties.但未能对数据源执行相同的操作.

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

我怀疑它正在尝试加载一个 EmbeddedWebApplicationContext,这不是我想要的.

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

堆栈跟踪是:

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 方法,如下所示,它也会失败并出现相同的错误.

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

我提到了 this 并且它说我们需要设置 enableNaming() 在 servlet 容器上?我可以为非嵌入式 Web 应用程序上下文做类似的事情吗?还是纯粹是 WAS 8.5 的问题??

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??

推荐答案

您需要在 web.xml 中有带有 jdbc/fileUploadDS 名称的资源引用.并确保它在安装期间或通过 ibm-web-bnd.xml 文件绑定到实际数据源名称.

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.

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、过滤器)中添加以下类注释:

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

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

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天全站免登陆