I'm wondering if I can map this piece of 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 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 hints how to replace the rest?
If you don't want to use any xml at all, then 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 above configuration class in 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 aspect beans
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 advice annotation as shown above.
Currently it isn't possible to translate all XML-based AspectJ settings to a Java based configuration. Probably it will never be. The main reason is that Java doesn't support method literals. But there is a workaround, which was first presented here: https://jira.springsource.org/browse/SPR-8148
- Continue using
<aop:config>
by including the relevant XML snippet using@ImportResource
- Convert any existing
<aop:config>
elements to use@Aspect
style.
Referring to the documentation, I would say that you're already nearly done with your configuration you have described above. You just have to change you config like this:
<aop:config>
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20" />
</aop:config>
Leave the rest like it is and import that 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 could help...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With