background preloader

AspectJ

Facebook Twitter

AspectJ example. Spring framework comes with AOP support.

AspectJ example

In fact, as stated in Spring reference documentation, “One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don’t want to, AOP complements Spring IoC to provide a very capable middleware solution. AOP is used in the Spring Framework to… … provide declarative enterprise services, especially as a replacement for EJB declarative services. Nevertheless Spring AOP framework comes with certain limitations in comparison to a complete AOP implementation, such as AspectJ. Furthermore Spring AOP framework uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. “If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used.

To summarize, when working with Spring AOP framework you should have two important things in mind : It’s possible to use any of the approaches mentioned above. Caching with Spring AOP. In this article, I will show how to easily implement annotation based method caching with Spring AOP and EhCache.

caching with Spring AOP

I assume that the application we implement the caching for is running inside the Spring container. Our usecase Imagine we have a service that can be asked for a list of countries and news: public class MyServiceImpl implements MyService { public List<Country> getCountries() { } public List<NewsItem> getLatestNews( String topic ) { } } public class MyServiceImpl implements MyService { public List<Country> getCountries() { // load country list from database } public List<NewsItem> getLatestNews( String topic ) { // load latest news from database } Now in this example we can be sure that the list of countries never changes and for the latest news it would be ok to see an update with a delay of 30 minutes. Using a simple approach, we could just create a subclass of MyServiceImpl that puts the data in some Map and returns it from there, for example. Annotations + AspectJ in Spring. Tracing and auditing is one of the most common ways to monitor an application.

Annotations + AspectJ in Spring

If your application is Spring-based, you can use Spring AOP for auditing and monitoring(via interceptors). But what if you want to add custom annotations to each audited method and have an access to these custom annotation at run time? In order to do that you need Load Time Weaving (LTW) with AspectJ in the Spring framework. Here is how to add AspectJ auditing to your Spring-based web application.Note : In order to use ASPECTJ LTW with Spring based web application, you have to use release Spring 2.5.0RC2 or later. 1.

Package com.audit.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value=ElementType.METHOD) @Retention(value=RetentionPolicy.RUNTIME) public @interface AppAudit { public String stepName(); public String stepDescription(); } AspectJ cheat sheet. This cheat sheet uses AspectJ’s @AspectJ style.

AspectJ cheat sheet

It’s also possible to use the original AspectJ syntax like this example demonstrates, but I prefer to use standard Java classes with the AspectJ logic inside annotations. Pointcuts The definition of a pointcut from the AspectJ homepage: A pointcut is a program element that picks out join points and exposes data from the execution context of those join points.

Pointcuts are used primarily by advice. A pointcut example: Pointcut designators. @AspectJ summary examples. AspectJ 5 Development Kit Developer's Notebook. AspectJ Programming Guide.