Ok, here's my spring config:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<aop:config>
<aop:advisor advice-ref="debugLog" pointcut="execution(* *.*(..))"/>
</aop:config>
<aop:aspectj-autoproxy/>
<context:property-placeholder location="classpath:database.properties"/>
<context:component-scan base-package="com.mycompany.myproject.backend.services.impl" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<context:component-scan base-package="com.mycompany.myproject.backend.dao.hibernate">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<bean id="debugLog" class="org.springframework.aop.interceptor.SimpleTraceInterceptor">
<property name="useDynamicLogger" value="true" />
</bean>
<tx:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="entityInterceptor">
<bean class="com.mycompany.myproject.backend.util.hibernate.AuditInterceptor" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
</bean>
</beans>
So you see it's not a custom-made aspect, it's just Spring AOP's SimpleTraceInterceptor.
I omit the DAO classes, they are really straight forward. As stated before they all are annotated with @Repoository.
And here's the test code (imports omitted for brevity). The purpose of initSession() is to clean the DB (only within the transaction, of course) to avoid side effects with existing objects, and then create one single object we need initially:
Code:
package com.mycompany.myproject.backend.dao.hibernate;
@ContextConfiguration(locations = { "classpath:applicationContext-backend.xml" })
public abstract class AbstractDAOTest extends AbstractTransactionalTestNGSpringContextTests
{
@Autowired
protected SessionFactory sessionFactory;
@Autowired
private BenutzerDAO benutzerDAO;
//...more DAOs injected
@BeforeMethod
protected void initSession()
{
//cleanup Database
benutzerDAO.deleteAll();
//...call deleteAll() on more DAOs
sessionFactory.getCurrentSession().flush();
Benutzer testUser = new Benutzer();
testUser.setKennung("00");
testUser.setNachname("Tester");
testUser.setVorname("Bester");
testUser.setPasswort("pass");
testUser.setRolle(Rolle.ARBEITSVORBEREITUNG);
UserSession.setUser(testUser);
benutzerDAO.saveOrUpdate(testUser);
sessionFactory.getCurrentSession().flush();
}
}
public class BenutzerDAOHibernateTest extends AbstractDAOTest
{
@Autowired
private BenutzerDAO benutzerDAO;
@Test
public void testFindByNummer()
{
//ein passendes Objekt in der DB anlegen...
Benutzer benutzer = new Benutzer();
benutzer.setKennung("47");
benutzer.setPasswort("pass");
benutzer.setNachname("nachname");
benutzer.setVorname("vorname");
benutzerDAO.saveOrUpdate(benutzer);
//...und finden!
Benutzer result = benutzerDAO.findByKennung("47");
assertNotNull(result);
assertSame(result, benutzer);
result = benutzerDAO.findByKennung("48");
assertNull(result);
}
}
I have not found anything suspicious in the traces, regardless on how I define the pointcut. The traces always say that the transaction that the test runs in is rolled back. When I define the pointcut as pointcut="execution(* *.*(..))", however, the transaction is actually committed, and the SimpleTraceInterceptor is working (I see the output in the traces). When I define the pointcut as pointcut="execution(* com.mycompany.*.*(..))" (that's how I understand it is supposed to be for tracing all classes from mycompany) then no commit, but also no output from SimpleTraceInterceptor.