Defining Custom AspectJ Annotations

One of my recent work challenges was to define a custom annotation eligible for Spring autowired dependency injection using Aspect-Oriented Programming (AOP), so I’m my findings describing here.

The following components are required to define a custom AspectJ annotation based interceptor, within a web application.

Annotation definitions

To define a custom annotation, define an @interface decorated with the following:

@Target – Indicates the contexts in which an annotation type is applicable
@Retention – Describes how long the annotations with the annotated type will be retained


@Target(value = METHOD)
@Retention(value = RUNTIME)
public @interface AuthHeadersExtractor {

Define the Aspect

The aspect class should be @Aspect annotated, to mark it as an aspect declaration. It should also be registered with the @Component annotation, thus making it eligible for receiving injections of other managed beans via the @Autowire annotation.


public class AuthHeadersAspect {
AuthHeaders authHeaders;

@Before(“execution(public void com.rakkatak.example..get*(userId, token))”)
public void extractAuthHeaders(String userId, String token){

Please refer to the following for more information on AOP concepts and advice types such as @Before:

Aspect configuration

The aop.xml file is standard AspectJ convention. It should be placed on the class path and will inform the AspectJ weaver that we want to weave our Profiling Aspect into our classes.

<!DOCTYPE aspectj PUBLIC “-//AspectJ//DTD//EN” “”>
<!– only weave classes in this package –>
<include within=” com.rakkatak.example.*” />
<!– use only this aspect for weaving –>
<aspect name=” com.rakkatak.example.aspects.AuthHeadersAspect” />

Initializing the Aspect

Because the @Aspect annotated class is registered using the @Component annotation, additional configuration is required to initialize the aspect. This configuration can occur in a @Configuration file.


public class WebAppConfig {

public AuthHeadersAspect authHeadersAspect() {
AuthHeadersAspect aspect = Aspects.aspectOf(AuthHeadersAspect.class);
return aspect;

The static “Aspects.aspectOf” method will grab the instance of the aspect that AspectJ made when it found the aspect’s class via the element in aop.xml. Returning this instance within a method annotated with @Bean in a component scanned class annotated with @Configuration will pass that aspect instance to Spring to become a managed bean, making it eligible for @Autowired dependency injection.

Use the Aspect

Finally use your annotation on a method.


public class SampleController {

public Greeting greeting(@RequestHeader(value=”userId”) String userId, @RequestHeader(value=”token”) String token) {
return sampleService.getList();

Case Study: Clustering and Externalized Session within a Web Container

My team was recently asked to provide a solution for externalizing session storage in order to support clustering within a web application deployed on JBoss. In order to do this we decided to create a model in which the standard Java session object would be replaced by a custom session object via a RequestWrapper. The type of session object returned would be determined by the session repository configured within the web container. We provided two types of session repositories: 1) An object grid repository that returns a session object that would be stored and persisted within an external database. This type of session object would support clustering and is intended to be used within a production environment 2) An in memory session repository that returns a session object stored within the web container’s memory. This type of session object does not support clustering and would be used for ease of development only.


In order to use a request wrapper, within the servlet filter, we initialized it and added it to the filter chain like this:

public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(httpRequest) {
chain.doFilter(wrapper, response);

The diagram below shows the flow used by the RequestWrapper to retrieve the custom session object.


By doing this there were other challenges that presented themselves such as not being able to detect session events such createSession and destroySession through the web container’s servlet filters. Possible solutions to this would be to explicitly create listeners for each CustomSession object and then initialize these listeners for each servlet filter subsequently created. In addition we would may lose valuable session management functionality made available by the web container in terms of the  lifecycle and cleanup (i.e. session timeout, session expiry). In order to compensate we would need to implement this through our own custom design. This model, does however, allow for sessions to be maintained across a cluster.

Doing a lil research…

Since AngularJS has been described as a Superheroic JavaScript MVW Framework, I decided to give it a shot. BTW, MVW stands for “Model View Whatever”. It was termed this way since it is not close to the other MV* labels. It is not a MVC model as controllers exist for each view, so the closest thing has been described as a MVVM – where the $scope object could be considered the ViewModel that is being decorated by a function that we call a Controller. Since calling something MVVM for “Model View View Model” is a bit rediculous they decided to call the model MVW or “Model View Whatever”.


You can check out some of the apps that I dreamed up below:

Lehra application Beta 0.01

Email your MP Beta 0.01

Angular gives you a lot of flexibility to nicely separate presentation logic from business logic and presentation state. My only complaint thus far is the inability of filters to return objects rather than strings. If anyone has a solution for this I would love to hear it! I had to use a work around of creating more than 1 filter.

Responsive WordPress Websites, yes please!

Yes, I love WordPress because of the wonderful world of themes and plugins that are often developed by independents such as myself. It’s been so refreshing to email individuals responsible for developing a given theme/plugin and having them respond within a day. And the response is totally filter free, like from a real person, 0 red tape! How empowering is that?!


Another reason why I’m loving creating responsive sites because of the bright shiny world of responsive media queries. It’s easy to style a website according to the size of the screen used to display it. That’s why all of my sites are good to go on screens of any size. You can take a look at them here.

Drop me a line if you’d like to talk about getting your own custom WordPress site running.