Skip to content

Instantly share code, notes, and snippets.

@rponte
Created July 26, 2012 13:04
Show Gist options
  • Save rponte/3181934 to your computer and use it in GitHub Desktop.
Save rponte/3181934 to your computer and use it in GitHub Desktop.
Mixing Spring AOP Declarative Transaction with @transactional
<?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:context="http://www.springframework.org/schema/context"
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-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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="br.com.triadworks" />
<!-- habilita suporte as anotações transacionais -->
<tx:annotation-driven order="1" />
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="find*" read-only="true" />
<tx:method name="count*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="busca*" read-only="true" />
<tx:method name="lista*" read-only="true" />
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- Configuração do Aspecto das transações. -->
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* br.com.syspdv.triadworks..*.*(..))
and not @annotation(org.springframework.transaction.annotation.Transactional)" />
<aop:advisor order="2" advice-ref="txAdvice"
pointcut-ref="serviceMethods" />
</aop:config>
</beans>
@rponte
Copy link
Author

rponte commented Jul 26, 2012

The real magic is in the pointcut expression: and not @annotation(org.springframework.transaction.annotation.Transactional) - but probably this only works on a method annotated with @transactional, not a class/type.

@lmolas
Copy link

lmolas commented Nov 23, 2015

you could add and not within(@org.springframework.transaction.annotation.Transactional *)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment