7

I'm trying to setup a Spring 3 Web MVC project, using the @Controller, annotation-based approach.

package my.package

@Controller
@RequestMapping("/admin/*")
public class AdminMultiActionController {

@RequestMapping(value = "admin.htm", method = RequestMethod.GET)
public String showAdminSection() {
    return "admin";
}

My dispatcher-servlet has the following Controller handlers:

<context:component-scan base-package="my.package" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

The webapp is running good with the supplied maven artifacts:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>

Now I wanted to add @AspectJ AOP. I got the libs:

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.9</version>
</dependency>

added to my applicationContext.xml:

<aop:aspectj-autoproxy/>

Made sure to create the relevant bean in the applicationContext.xml as well:

<bean id="securityInterceptor" class="my.package.service.SecurityInterceptor"/>

And started fleshing out the @Aspect:

package my.package.service

@Aspect
public class SecurityInterceptor {

@Pointcut("execution(* showAdminSection(..))")// the pointcut expression
private void foo() {
    System.out.println("fooo");
}// the pointcut signature

Now this is where it stopped working. "fooo" is never printed.

Could it be, because the pointcutted (spelling?) objects must be spring-managed beans and my @Controller in combination with the DefaultAnnotationHandlerMapping is not perceived as such?

Any help would be appreciated. If I forgot to include any information, please ask. Hope someone can help me out here.

Thanks a lot!

chzbrgla
  • 5,158
  • 7
  • 39
  • 56

3 Answers3

8

Argh god.. finally got it working!

Thanks for your reply Affe!

For the curious:

  1. Don't use component-scan and defaultannotationhandlers to get your controllers
  2. Wire them in spring xml
  3. Don't have said controllers in dispatcher-servlet whilst aop config sits in applicationContext.
  4. Move both to the dispatcher-servlet

  5. Of course Affe is right: don't forget the advice :p

chzbrgla
  • 5,158
  • 7
  • 39
  • 56
7

To second chzbrgla, for those who view this later (like me), my issue resolved after moving the controller component scanning and aop config to the dispatcher servlet like so:

...
<context:component-scan base-package="com.mypackage.controller"/>
<!--  Configure aspects. -->
<bean id="myAspect1" class="com.mypackage.intercept.SiteAccessAspect"/>

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="myAspect1" />
</aop:aspectj-autoproxy>
...

As you can see, component scanning still worked in my case. Moving both to the dispatcher servlet solved it for me.

The apspect (myApect1, in this case) was configured as follows:

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {}

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

@Before("controller() && method()")
public void doAccessCheck(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature()); // For testing purposes.
}
Scifiballer24
  • 593
  • 2
  • 8
  • 14
  • when i tried to use the above mentioned aspect, i'm getting the below error Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut controller at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:302) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression............................................................................... Please suggest – Tarun Gupta Aug 10 '15 at 05:15
4

The pointcut method defines the pointcut, it doesn't get called on a match, you need to define something to actually happen. e.g.,

@Before("foo()")
public void beforeFoo(JoinPoint joinPoint) {
    System.out.println("foooo");
}
Affe
  • 47,174
  • 11
  • 83
  • 83