Pregunta

i have a problem with JNDI for test my application, not is the datasource, but load a configuration to environment. My application context the test:

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="appDataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="jndiClient" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/client"/>
    <property name="jndiEnvironment">
        <props>
            <prop key="properties1">44</prop>
            <prop key="properties2">0</prop>
            <prop key="properties3">XPTO</prop>
        </props>
    </property>
</bean>

I need load this jndiClient. In my class:

    @Component
    public class FactoryLocator  {
         @Qualifier("jndiClient")
         @Autowired
         private JndiObjectFactoryBean jndiClient;

happened and the following error when I run the test:

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jndi.JndiObjectFactoryBean br.com.simova.bob.factory.FactoryLocator.jndiClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiClient' defined in class path resource [test-appContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 53 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiClient' defined in class path resource [test-appContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:873)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:815)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 55 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    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:178)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 65 more

Thank for any help!

¿Fue útil?

Solución

First and foremost, your tests typically should not rely on JNDI being present in a test environment.

The whole point of using JndiObjectFactoryBean is so that you can decouple your application from a direct dependency on JNDI, using dependency injection of components instead.

With that in mind, I would highly recommend that you never inject an instance of JndiObjectFactoryBean into one of your application components. Instead, you should really be injecting the object returned by the JndiObjectFactoryBean into your components. By doing so, you can then configure a mock or stub of your component when writing unit and integration tests, thereby avoiding the need for JNDI for testing.

Now, if for some very good reason you simply must rely directly on JNDI, Spring does provide a mechanism for setting up an in-memory JNDI context. See the Javadoc for SimpleNamingContextBuilder for details.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top