I\'m wondering if I can map this piece of xml-configuration to Spring JavaConfig:
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 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.