Announcement Announcement Module
Collapse
No announcement yet.
Custom TransactionAnnotationParser without AspectJ Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom TransactionAnnotationParser without AspectJ

    Hi all, I have a Spring + JPA setup that works fine. The transactional services use annotation metadata as following:

    Code:
    @Service
    public class MyServiceImpl implements MyService {
    
        @Override
        @Transactional
        public void myMethod() {
            // Implementation here
        }
    }
    and the applicationContext.xml contains the <tx:annotation-driven /> namespace tag.

    Instead of the standard setup provided by <tx:annotation-driven />, I would like to customize the parser that reads the transactional metadata (I want to add extra transactional metadata to the methods to be used in my dialect).

    Therefore I removed the <tx:annotation-driven /> tag from the applicationContext.xml and I re-defined the beans that are usually added by <tx:annotation-driven /> as following:

    Code:
    <aop:config />
    
    <bean name="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    	<property name="transactionManagerBeanName" value="transactionManager" />
    	<property name="transactionAttributeSource">
    			<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource">
    			<constructor-arg index="0">
    				<bean class="com.example.MyAnnotationParser" />
    			</constructor-arg>
    		</bean>
    	</property>
    </bean>
    
    <bean id="org.springframework.transaction.config.internalTransactionAdvisor"
    	class="org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor">
    	<property name="transactionAttributeSource">
    			<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource">
    			<constructor-arg index="0">
    				<bean class="com.example.MyAnnotationParser" />
    			</constructor-arg>
    		</bean>
    	</property>
    	<property name="adviceBeanName" value="txInterceptor"/>
    </bean>
    Everytime I start the server Spring complains about missing AspectJ... How could I reproduce the behavior of <tx:annotation-driven /> defining explicitly all the needed beans?

    With <tx:annotation-driven /> everything works fine without AspectJ, I would like to have the same, but defined in my applicationContext.xml rather than using <tx:annotation-driven />.

    Does anybody have a clue?

  • #2
    Hi all,
    I found a way to fix this. The <tx:annotation-driven /> tag defines an "autoproxy" before instantiating the transaction interceptors. This can be achieved in XML by defining a bean "org.springframework.aop.framework.autoproxy.Defau ltAdvisorAutoProxyCreator" in the BeanFactory before defining the transaction interceptors. The following code duplicates the logic done in <tx:annotation-driven />, but replacing the default TransactionAnnotationParser with a custom one.

    Code:
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
    
    <bean name="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0"
    	class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource">
    	<constructor-arg index="0">
    		<bean class="com.example.MyTransactionAnnotationParser" />
    	</constructor-arg>
    </bean>
    
    <bean name="org.springframework.transaction.interceptor.TransactionInterceptor#0"
    	class="org.springframework.transaction.interceptor.TransactionInterceptor">
    	<property name="transactionManagerBeanName" value="transactionManager" />
    	<property name="transactionAttributeSource" ref="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0" />
    </bean>
    
    <bean id="org.springframework.transaction.config.internalTransactionAdvisor"
    	class="org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor">
    	<property name="transactionAttributeSource" ref="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0" />
    	<property name="adviceBeanName" value="org.springframework.transaction.interceptor.TransactionInterceptor#0"/>
    </bean>
    I hope this can help somebody in the future having the same problem...

    Comment

    Working...
    X