This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Spring and the fly likes I get the bean for one class and for other it is null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "I get the bean for one class and for other it is null" Watch "I get the bean for one class and for other it is null" New topic

I get the bean for one class and for other it is null

PriyaK Kulkarni

Joined: Apr 19, 2012
Posts: 1

I am writing some testcases and I am getting the DBService from the spring bean. But when I run the tests soms of them get the DBservice properly and some others get it null.

The spring.xml is :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:sec="" xmlns:xsi=""
xmlns:aop="" xmlns:tx=""

<!-- Unit Test -->
<bean id="yanDS"
<property name="driverClassName">
<property name="url">
<property name="username" value="${}"></property>
<property name="password" value="${}"></property>

<bean id="nsmSessionFactory"
<property name="dataSource">
<ref bean="yanDS" />
<property name="mappingJarLocations" value="nsm-db-1.0-SNAPSHOT.jar"/>-->

<property name="mappingResources">

<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>

<bean id="transactionManager"
<property name="sessionFactory" ref="nsmSessionFactory" />

<bean id="nsmDbServiceTemplate" abstract="true"
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<prop key="*">PROPAGATION_REQUIRED</prop>

<!-- ************ DAO **************** -->
<bean id="attrTypeDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="attrTypeDAOTarget" />
<bean id="attrTypeVsnDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="attrTypeVsnDAOTarget" />
<bean id="attrTypeOwnerDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="attrTypeOwnerDAOTarget" />
<bean id="attrTypeOwnerVsnDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="attrTypeOwnerVsnDAOTarget" />
<bean id="dynAttrValueDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="dynAttrValueDAOTarget" />
<bean id="dynAttrValueVsnDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="dynAttrValueVsnDAOTarget" />
<bean id="envDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="envDAOTarget" />
<bean id="nsdChangeRequestDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="nsdChangeRequestDAOTarget" />
<bean id="nsdVersionChgreqDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="nsdVersionChgreqDAOTarget" />
<bean id="nsdVersionDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="nsdVersionDAOTarget" />
<bean id="nsdVersionDeployDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="nsdVersionDeployDAOTarget" />
<bean id="releaseDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="releaseDAOTarget" />
<bean id="sysAttrValueDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="sysAttrValueDAOTarget" />
<bean id="sysAttrValueVsnDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="sysAttrValueVsnDAOTarget" />
<bean id="adPosMapDAO" parent="nsmDbServiceTemplate">
<property name="target" ref="adPosMapDAOTarget" />

<!-- ***************** DAO Impls ******************* -->
<bean id="attrTypeDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="attrTypeVsnDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="attrTypeOwnerDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="attrTypeOwnerVsnDAOTarget"
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="dynAttrValueDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="dynAttrValueVsnDAOTarget"
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="envDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="nsdChangeRequestDAOTarget"
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="nsdVersionChgreqDAOTarget"
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="nsdVersionDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="nsdVersionDeployDAOTarget"
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="releaseDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="sysAttrValueDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="sysAttrValueVsnDAOTarget"
<property name="sessionFactory" ref="nsmSessionFactory" />
<bean id="adPosMapDAOTarget" class="">
<property name="sessionFactory" ref="nsmSessionFactory" />

<!-- ***************** DB Service ******************* -->
<bean id="nsmDbService" class=""

<property name="attrTypeDAO" ref="attrTypeDAO" />
<property name="attrTypeVsnDAO" ref="attrTypeVsnDAO" />
<property name="attrTypeOwnerDAO" ref="attrTypeOwnerDAO" />
<property name="attrTypeOwnerVsnDAO" ref="attrTypeOwnerVsnDAO" />
<property name="dynAttrValueDAO" ref="dynAttrValueDAO" />
<property name="dynAttrValueVsnDAO" ref="dynAttrValueVsnDAO" />
<property name="envDAO" ref="envDAO" />
<property name="nsdChangeRequestDAO" ref="nsdChangeRequestDAO" />
<property name="nsdVersionChgreqDAO" ref="nsdVersionChgreqDAO" />
<property name="nsdVersionDAO" ref="nsdVersionDAO" />
<property name="nsdVersionDeployDAO" ref="nsdVersionDeployDAO" />
<property name="releaseDAO" ref="releaseDAO" />
<property name="sysAttrValueDAO" ref="sysAttrValueDAO" />
<property name="sysAttrValueVsnDAO" ref="sysAttrValueVsnDAO" />
<property name="adPosMapDAO" ref="adPosMapDAO" />

The class where I am getting the bean is: I have the proper imports here

public class NsmBaseTest {

public NsmBaseTest(){


protected NsmDbService nsmDbService;
protected ApplicationContext appContext;
Properties props = null;
Logger logger = null;

public void beforeSuite() {
appContext = new ClassPathXmlApplicationContext("classpath:nsm_bl_qa_test_spring-config.xml");
nsmDbService = (NsmDbService) appContext.getBean("nsmDbService");
logger = Logger.getLogger("NSMT logger");
props = new Properties();
File file = new File("./src/test/resources/");
props.load(new FileInputStream(file));
}catch(IOException e){

public void afterSuit(){
appContext = null;

protected int getRandomNumber(){
Random random = new Random();
return (random.nextInt());

There are 2 classes which extend from this NsmBaseTest.
So the problem is nsmDBservice is OK for one of the class and null for the other class. The class which gets NsmDBService as null is not the same each time I run the tests.
The driver class, url, username and password is taken from pom.xml of maven.

Can someone help with this? This is an intermittent issue and needs to be resolved urgently.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Please post code and configuration using the CODE tags so we can read that. The Code button is right there above where you post.



Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: I get the bean for one class and for other it is null
jQuery in Action, 3rd edition