I'm reworking an older Spring project to more reflect how things are supposed to be done in Spring 3.0.x.
One of the changes I made was to the repository/dao layer. As advised by best practices, I no longer extend from HibernateDaoSupport
to use the HibernateTemplate
, but instead I use the Hibernate SessionFactory
directly by using sessionFactory.getCurrentSession()
, which is supposed to work with Spring 3.0.x and above.
This has been a very big boon for the project as a whole, as it gets rid of all of the wrapping code caused by HibernateTemplate
. However, I just noticed that I can no longer call into Service methods that were using @PostConstruct (or were using the bean's onStartUp
attribute in the XML application context)
For example, this method used to work just fine using HibernateTemplate
, but now Hibernate throws an exception complaining that there is no session bound to the thread:
@Override
@PostConstruct
public void onStartUp() {
logger.debug("Starting Bootstrap Service...");
createSysAdminUser();
createDefaultRoles();
createDefaultThemes();
createStopListIfDoesNotExist();
stopListService.load();
partialMappingService.load();
dictionaryService.load();
}
I could just remove this @PostConstruct
method call... it's the only one in the system. It is called when the application starts up to bootstrap data for a new application. Most of the time, it does nothing on a production system, but it's handy to have it for test and development databases that were created fresh.
Any ideas as to why and how I can fix it?
Thanks!
EDIT: Here is my transaction manage advice config:
<aop:config>
<aop:advisor advice-ref="transactionAdvice"
pointcut="execution(* *..service.*.*(..))" order="1"/>
<!-- gets sub packages like service.user -->
<aop:advisor advice-ref="transactionAdvice"
pointcut="execution(* *..service.*.*.*(..))" order="2"/>
</aop:config>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>