Java Reference
In-Depth Information
java -javaagent: spring-instrument.jar
com.apress.springenterpriserecipes.bookshop.aspectj.Main
To enable transaction management for a domain object's method, you can simply annotate it with
@Transactional , just as you did for methods of Spring beans.
package com.apress.springenterpriserecipes.bookshop.aspectj;
...
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;
@Configurable
public class Book {
...
@Transactional
public void purchase(String username) {
...
}
}
Finally, to enable Spring's AnnotationTransactionAspect for transaction management, you just
define the <tx:annotation-driven> element and set its mode to aspectj . The <tx:annotation-driven>
element takes two values for the mode attribute: aspectj and proxy . aspect stipulates that the container
should use load-time or compile-time weaving to enable the transaction advice. This requires the
spring-aspects jar to be on the classpath, as well as the appropriate configuration at load time or
compile time. Alternatively, proxy stipulates that the container should use the Spring AOP mechanisms.
It's important to note that the aspect mode doesn't support configuration of the @Transactional
annotation on interfaces. Then the transaction aspect will automatically get enabled. You also have to
provide a transaction manager for this aspect. By default, it will look for a transaction manager whose
name is transactionManager .
<beans xmlns= " http://www.springframework.org/schema/beans "
xmlns:xsi= " http://www.w3.org/2001/XMLSchema-instance "
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/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-3.0.xsd " >
...
<tx:annotation-driven mode= " aspectj " />
<bean id= " transactionManager "
class= " org.springframework.jdbc.datasource.DataSourceTransactionManager " >
<property name= " dataSource " ref= " dataSource " />
</bean>
</beans>
Search WWH ::




Custom Search