After refactoring one of our internal applications at work to use Spring XML configuration file based JNDI lookup today, several of the application's unit tests which involve testing of JNDI lookups started failing. The following error appeared for each failed test in the jUnit XML log file:
<error message="Error creating bean with name '[dsName]': 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" type="org.springframework.beans.factory.BeanCreationException"> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory .initializeBean(AbstractAutowireCapableBeanFactory.java:1338) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory .doCreateBean(AbstractAutowireCapableBeanFactory.java:473) ... (snipped for brevity) ... at org.springframework.context.support.ClassPathXmlApplicationContext .<init>(ClassPathXmlApplicationContext.java:83) at [internal namespace].util.TestUtil.getSpringContext(TestUtil.java:167)
Cause
We discovered that the cause of the problem in our case was that, as the error message indicated, Spring wasn't seeing a .properties file present in the project which set a value for the java.naming.factory.initial property.
The line in our Spring context .xml file looked like:
<jee:jndi-lookup id="[dsName]" jndi-name="jdbc/[dsPath]/[dsName]" expected-type="javax.sql.DataSource" />
The line in our .properties file (which Spring wasn't finding properly), which set the naming factory class to a class provided by our J2EE application server (WebLogic), was:
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
The .properties file was located in the same folder as the Spring context .xml file.
Solution
In the Spring context .xml file, just prior to the jee:jndi-lookup tag, we added a bean of type PropertiesFactoryBean to act as a pointer to the .properties file:
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:productInquiry.properties"/> </bean>
Then, we added a reference to that new bean to the existing jee:jndi-lookup tag (the added part is bolded):
<jee:jndi-lookup id="ot1ds" jndi-name="jdbc/[dspath]/[dsname]" expected-type="javax.sql.DataSource" environment-ref="jdbcConfiguration"/>
This change fixed the problem, and got our tests to run successfully.
I hope this is helpful to someone!
This post was really helpful. I was using sql server instead of weblogic though.
ReplyDeleteThank you very much..
Susheela.
Thanx for this post., my situation was exact like ur., had a refactoring process an then boom., test fails.
ReplyDeleteThis post led me to refactor my own Spring 2.5 app. I am not there yet. I do have jndi lookup problems.
ReplyDeleteWhat does your applicationContext.xml file look like?
Thanks very much.
I am getting a strange error: Caused By: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'ssv6PU' must be of type [javax.persistence.EntityManagerFactory], but was actually of type [weblogic.jdbc.common.internal.RmiDataSource]
ReplyDelete