Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP pointcut for all methods in a controller

I want to run some code before every method in a Spring (3.2.3) @Controller. I have the following defined but it won't run. I suspect the pointcut expression is incorrect.

dispatcher-servlet.xml

<aop:aspectj-autoproxy/>
<bean class="com.example.web.controllers.ThingAspect"/>

c.e.w.c.ThingAspect

@Pointcut("execution(com.example.web.controllers.ThingController.*(..))")
public void thing() {
}

@Before("thing()")
public void doStuffBeforeThing(JoinPoint joinPoint) {
    // do stuff here
}
like image 346
Goose Avatar asked May 12 '14 11:05

Goose


People also ask

How do you specify a single pointcut for multiple packages?

You can use boolean operators: expression="execution(* package1. *. *(..))

What methods does this pointcut expression reference?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

What is the difference between JoinPoint and pointcut?

A join point is an individual place where you can execute code with AOP. E.g. "when a method throws an exception". A pointcut is a collection of join points. E.g. "when a method in class Foo throws an exception".


1 Answers

Your pointcut expression is missing a return type like void, String or *, e.g.

execution(* com.example.web.controllers.ThingController.*(..))
like image 125
kriegaex Avatar answered Sep 30 '22 05:09

kriegaex