Intercepting method with Spring AOP using only annotations

Posted by fish on Stack Overflow See other posts from Stack Overflow or by fish
Published on 2010-04-10T02:18:14Z Indexed on 2010/04/10 2:23 UTC
Read the original article Hit count: 461

Filed under:
|
|
|

In my Spring context file I have something like this:

<bean id="userCheck" class="a.b.c.UserExistsCheck"/>
<aop:config>
      <aop:aspect ref="userCheck">
         <aop:pointcut id="checkUser"
                expression="execution(* a.b.c.d.*.*(..)) &amp;&amp; args(a.b.c.d.RequestObject)"/>
         <aop:around pointcut-ref="checkUser" method="checkUser"/>
      </aop:aspect>
</aop:config>    

a.b.c.UserExistsCheck looks like this:

@Aspect
public class UserExistsCheck {

@Autowired
private UserInformation userInformation;

public Object checkUser(ProceedingJoinPoint pjp) throws Throwable {
    int userId = ... //get it from the RequestObject passed as a parameter
    if (userExists(userId)) {
        return pjp.proceed();
    } else {
        return new ResponseObject("Invalid user);
    }
}

And the class that is being intercepted with this stuff looks like this:

public class Klazz {
    public ResponseObject doSomething(RequestObject request) {...}
}

This works. UserExistCheck is executed as desired before the call is passed to Klazz. The problem is that this is the only way I got it working. To get this working by using annotations instead of the context file seems to be just too much for my small brain. So... how exactly should I annotate the methods in UserExistsCheck and Klazz? And do I still need something else too? Another class? Still something in the context file?

© Stack Overflow or respective owner

Related posts about spring

Related posts about spring-aop