Injecting dependency (genericService) is not working for the timertask and my service class is wrapped inside the transaction as shown below.
Code:
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="iterate*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="genericService" class="com.silicerayan.hydra.service.impl.GenericServiceHibernate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="genericServiceTX" parent="baseTransactionProxy">
<property name="target"><ref local="genericService"/></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
(by the way, does the ordering of bean decleration matters?)
My question is that why the following works with the a java main method:
XML:
Code:
....
<bean id="pastDueActivityAlert" class="com.silicerayan.hydra.jobs.PastDueActivitiesEmailNotification">
<property name="genericService"><ref bean="genericService"/></property>
</bean>
<bean id="genericServiceTX" parent="baseTransactionProxy">
<property name="target"><ref local="genericService"/></property>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="iterate*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
My java code:
Code:
public class PastDueActivitiesEmailNotification extends TimerTask{
....
public PastDueActivitiesEmailNotification() {
ApplicationContext ctx = new FileSystemXmlApplicationContext(PATH_CONTEXT);
SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
Session s = sessionFactory.openSession();
System.out.println("Session code" + s.hashCode());
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
genericService = (GenericService)ctx.getBean("genericServiceTX");
}
public static void main(String[] args) {
PastDueActivitiesEmailNotification pd = new PastDueActivitiesEmailNotification();
pd.run();
}
But the exact same configuration does not work in my web application (when run under tomcat). Any pointer or suggestions will be greatly appreciated. Please keep in mind that I have to lazy load my objects.