1

How do I implement AOP with an annotated Controller?

I've search and found two previous posts regarding the problem, but can't seem to get the solutions to work.

posted solution 1

posted solution 2

Here's what I have:

Dispatch Servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.foo.controller"/>

    <bean id="fooAspect" class="com.foo.aop.FooAspect" />

    <aop:aspectj-autoproxy>
        <aop:include name="fooAspect" />
    </aop:aspectj-autoproxy>
</beans>

Controller:

@Controller
public class FooController {

    @RequestMapping(value="/index.htm", method=RequestMethod.GET)
    public String showIndex(Model model){
        return "index";
    }
}

Aspect:

@Aspect
public class FooAspect {

    @Pointcut("@target(org.springframework.stereotype.Controller)")
    public void controllerPointcutter() {}

    @Pointcut("execution(* *(..))")
    public void methodPointcutter() {}

    @Before("controllerPointcutter()")
    public void beforeMethodInController(JoinPoint jp){
        System.out.println("### before controller call...");
    }

    @AfterReturning("controllerPointcutter() && methodPointcutter() ")
    public void afterMethodInController(JoinPoin jp) {
        System.out.println("### after returning...");
    }

    @Before("methodPointcutter()")
    public void beforeAnyMethod(JoinPoint jp){
        System.out.println("### before any call...");
    }
}

The beforeAnyMethod() works for methods NOT in a controller; I cannot get anything to execute on calls to controllers. Am I missing something?

Community
  • 1
  • 1
tommy
  • 11
  • 1
  • 2

2 Answers2

9

In order to put an aspect on a HandlerMethod in a class annotated with @Controller in Spring 3.1 you need to have the proxy-target-class="true" attribute on the aspectj-autoproxy element. You also need to have the CGLIB and the ASM libraries as a dependency in your WAR/EAR file. You can either specify your annotated aspect class as a bean and use the aop:include as stated above or you can leave the aop:include out and add a filter similar to this in your component scan element:

<context:component-scan>
  <context:include-filter type="aspectj"
    expression="com.your.aspect.class.Here"/>
</context:component-scan>

I do not know if this is a requirement as a result of Spring 3.1 only but I know that without this, you will not be able to put an aspect on your controller HandlerMethod. You would get an error similar to:

Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
HandlerMethod details: 
Controller [$Proxy82]
Method [public void com.test.TestController.testMethod(java.security.Principal,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException]
Resolved arguments: 
[0] [null] 
[1] [type=com.ibm.ws.webcontainer.srt.SRTServletResponse] [value=com.ibm.ws.webcontainer.srt.SRTServletResponse@dcd0dcd]

This is not required if your aspect is on a method in a class that is not a controller

mario
  • 25
  • 1
  • 8
  • 18
  • Myy application Context is as follows [applicationContext.xml](http://pastebin.com/raw.php?i=cCHEMX0p) Have 2 Aspect Java classes, LogControllerAspect and LogDAOAspect. LogDAOAspect is fine, but LogControllerAspect is logging twice. The 2 Java files are [here](http://pastebin.com/raw.php?i=L3sMUBPg) – Anand Rockzz Sep 11 '13 at 00:39
  • Figured it out, it was a log4j issue. Not Spring AOP issue. Full details about it is [here](http://stackoverflow.com/a/18735007/234110) – Anand Rockzz Sep 11 '13 at 18:14
  • "You also need to have the CGLIB and the ASM libraries as a dependency in your WAR/EAR file." - I released it without added this one – Veniamin May 01 '14 at 16:59
3

I am going to state an alternative solution (sorry not a direct answer) but what you wanting to do is probably best done via interceptors and filters.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • I wasn't able to get this working using aspects so I went with the interceptors route. – tommy Mar 17 '11 at 00:09
  • That said, I'm still interested in a solution if anyone can provide it! – tommy Mar 17 '11 at 00:32
  • tommy, you never said why my solution above did not work? I used it in two separate applications currently running in production. Interceptors and filters are good but the syntax for applying aspect is much richer and flexible. – mario Dec 21 '14 at 02:08