aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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
Author

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

PriyaK Kulkarni
Greenhorn

Joined: Apr 19, 2012
Posts: 1
Hi,

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="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- Unit Test -->
<bean id="yanDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${com.yahoo.satg.targeting.jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${com.yahoo.satg.targeting.jdbc.url}</value>
</property>
<property name="username" value="${com.yahoo.satg.targeting.jdbc.username}"></property>
<property name="password" value="${com.yahoo.satg.targeting.jdbc.password}"></property>
</bean>

<bean id="nsmSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="yanDS" />
</property><!--
<property name="mappingJarLocations" value="nsm-db-1.0-SNAPSHOT.jar"/>-->

<property name="mappingResources">
<list>
<value>./AdPosMap.hbm.xml</value>
<value>./AttrType.hbm.xml</value>
<value>./AttrTypeOwner.hbm.xml</value>
<value>./AttrTypeOwnerVsn.hbm.xml</value>
<value>./AttrTypeVsn.hbm.xml</value>
<value>./DynAttrValue.hbm.xml</value>
<value>./DynAttrValueVsn.hbm.xml</value>
<value>./Env.hbm.xml</value>
<value>./NsdChangeRequest.hbm.xml</value>
<value>./NsdVersion.hbm.xml</value>
<value>./NsdVersionChgreq.hbm.xml</value>
<value>./NsdVersionDeploy.hbm.xml</value>
<value>./Release.hbm.xml</value>
<value>./SysAttrValue.hbm.xml</value>
<value>./SysAttrValueVsn.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<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>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>

<bean id="nsmDbServiceTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

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


<!-- ***************** DAO Impls ******************* -->
<bean id="attrTypeDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.AttrTypeDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="attrTypeVsnDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.AttrTypeVsnDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="attrTypeOwnerDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.AttrTypeOwnerDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="attrTypeOwnerVsnDAOTarget"
class="com.yahoo.yant.nsm.persistence.dao.AttrTypeOwnerVsnDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="dynAttrValueDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.DynAttrValueDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="dynAttrValueVsnDAOTarget"
class="com.yahoo.yant.nsm.persistence.dao.DynAttrValueVsnDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="envDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.EnvDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="nsdChangeRequestDAOTarget"
class="com.yahoo.yant.nsm.persistence.dao.NsdChangeRequestDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="nsdVersionChgreqDAOTarget"
class="com.yahoo.yant.nsm.persistence.dao.NsdVersionChgreqDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="nsdVersionDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.NsdVersionDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="nsdVersionDeployDAOTarget"
class="com.yahoo.yant.nsm.persistence.dao.NsdVersionDeployDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="releaseDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.ReleaseDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="sysAttrValueDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.SysAttrValueDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="sysAttrValueVsnDAOTarget"
class="com.yahoo.yant.nsm.persistence.dao.SysAttrValueVsnDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>
<bean id="adPosMapDAOTarget" class="com.yahoo.yant.nsm.persistence.dao.AdPosMapDAOImpl">
<property name="sessionFactory" ref="nsmSessionFactory" />
</bean>

<!-- ***************** DB Service ******************* -->
<bean id="nsmDbService" class="com.yahoo.yant.nsm.persistence.service.NsmDbService"
factory-method="createInstance">

<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" />
</bean>
</beans>



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;

@BeforeSuite
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();
try{
File file = new File("./src/test/resources/test.properties");
props.load(new FileInputStream(file));
}catch(IOException e){
e.printStackTrace();
}
}

@AfterSuite
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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

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

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I get the bean for one class and for other it is null