AspectJ with Spring

Spring provides small libraries to enable AspectJ aspects into Spring projects. This library is named spring-aspects.jar. As we know from our earlier discussion, Spring allows dependency injection or AOP advice only on Spring bean. With Spring's AspectJ support using this small library, we can enable any object created outside the container for Spring-driven configuration. Just annotate the outside object with @Configurable. Annotating a non-Spring bean with @Configurable would require AnnotationBeanConfigurerAspect in spring-aspects.jar. The AnnotationBeanConfigurerAspect configuration needed by Spring can be done by annotating our configuration Java configuration class with @EnableSpringConfigured

Spring provides a finer way to enable Load-time weaving (LTW) by enabling a per-class loader basis. This gives much more fine-grained control, especially when we are deploying large or multiple applications into a single JVM environment.

To use LTW with Spring, we need to implement our aspect or advice as we had implemented earlier in the AOP concepts sections and, as per the AspectJ concepts, we need to create aop.xml in the META-INF folder, as follows:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages --
>
<include within="com.packt.springhighperformance.ch3.bankingapp
.service.impl.TransferServiceImpl"/>
<include within="com.packt.springhighperformance.ch3.bankingapp
.aspects.TransferServiceAspect"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.packt.springhighperformance.ch3.bankingapp
.aspects.TransferServiceAspect"/>
</aspects>
</aspectj>

The last thing we need to do is annotate our Java-based Spring configuration with @EnableLoadTimeWeaving. We need to add -javaagent:path/to/org.springframework.instrument-{version}.jar in our server launch script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset