Sunday, May 22, 2011

Spring beans and annotations in servlet with aspectj

Sometimes you have limited set of technologies to use on your project (for example you cannot go beyond already approved technology stack or you have very conservative team lead/manager etc).
In these cases usage of techniques like this or this is a good solution to go.
But i've noticed that lots of people with total freedom of technologies to choose are still using solutions like mentioned before or some self-made variation of these.
I believe that if there is a ready, tested and well-known solution to your problem - you should this solution and not to invent another bicycle.
In case of using Spring in a servlet (GWT RPC or any other) this solution is aspectj.

Here is a short how-to use Spring in a servlet. I will use Maven but you can easily adapt this to what you are using for dependency management and/or build.

First, we need to define required dependencies in POM file:
and add aspectj compiler phase:
Then create Spring config file that will tell Spring to manage classes created outside of Spring context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
Last thing you need is to add org.springframework.beans.factory.annotation.Configurable annotation to servlet class.
Afterwards you can simply use @Autowired annotation on servlet fields.
public class Servlet extends HttpServlet {
    private Bean bean;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
@Transactional annotations should be configured in Spring context XML like following: <tx:annotation-driven mode="aspectj"/>, and the same with @Secured.
Of course you will need to add some HTTP filter that will translate exceptions to user-readable format but this is out of scope of this article.

1 comment:

  1. Thank you for this! In exploring your solution, I found an even easier way. Instead of marking a class as @Configurable and sucking in all of aspectJ one can simply mark the class as @Component, @Repository, @Service, or @Controller, depending on the class's function and this makes it a Spring managed resource.

    I suspect marking it as @Configurable keeps it from being a Spring managed resource and if so, there may be use cases where this is desirable.