JavaConfig: Replacing aop: advisor and tx: advice

I am wondering if I can map this part of the xml configuration to Spring JavaConfig:

<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName"> <aop:config> <aop:pointcut id="serviceAnnotatedClass" expression="@within(org.springframework.stereotype.Service)" /> <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut-ref="serviceAnnotatedClass" order="20" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="is*" read-only="true" /> <tx:method name="ownTransaction*" propagation="REQUIRES_NEW" rollback-for="Exception" /> <tx:method name="*" rollback-for="Exception" /> </tx:attributes> </tx:advice> </beans> 

So far I have figured out how to replace aop: pointcut with

 <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20"/> 

and

 import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectConfig { @Pointcut("@within(org.springframework.stereotype.Service)") public void serviceAnnotatedClass() {} } 

Any tips on how to replace the rest?

+7
source share
2 answers

It is currently not possible to convert all XML-based AspectJ parameters to a Java-based configuration. Probably this will never happen. The main reason is that Java does not support method literals. But there is a workaround that was first introduced here: https://jira.springsource.org/browse/SPR-8148

  • Continue to use <aop:config> , including the appropriate XML fragment using @ImportResource
  • Convert any existing <aop:config> elements to use the @Aspect style.

Referring to the documentation , I would say that you have almost finished your configuration described above. You just need to change the configuration as follows:

 <aop:config> <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20" /> </aop:config> 

Leave the rest as is and import this resource:

 import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect @ImportResource("classpath:/aop-config.xml") public class AspectConfig { @Pointcut("@within(org.springframework.stereotype.Service)") public void serviceAnnotatedClass() {} } 

I hope I can help ...

+3
source

If you don't want to use any xml code at all, you can create a separate Java configuration class for aspects

 @Configuration @EnableAspectJAutoProxy @ComponentScan(basePackages = "com.myAspects") public class AspectConfig { //Here you can define Aspect beans or just use @ComponentScan (as above) //to scan the @Aspect annotation in com.myAspects package } 

And import the above configuration class into your main AppConfig class

 @Configuration @EnableWebMvc @Import({ AspectConfig.class }) @ComponentScan(basePackages = { "pkg1", "pkg2", "pkg3" }) public class AppConfiguration extends WebMvcConfigurationSupport { //Other configuration beans or methods } 

Now create your beans aspect

 import com.myAspects; @Component @Aspect public class LoggingAspect { @Before("execution(* com.service.*.*(..))") public void logBefore(){ System.out.println("before advice called"); } @After("execution(* com.service.*.*(..))") public void logAfter(){ System.out.println("after advice called"); } } 

You can use pointcut along with annotation of recommendations, as shown above.

+5
source

All Articles