相关疑难解决方法(0)

使用Spring AOP仅使用注释的拦截方法

在我的Spring上下文文件中,我有这样的东西:

<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>    
Run Code Online (Sandbox Code Playgroud)

abcUserExistsCheck看起来像这样:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

被这些东西拦截的类看起来像这样:

public class Klazz {
    public ResponseObject doSomething(RequestObject request) {...}
}
Run Code Online (Sandbox Code Playgroud)

这有效.在将呼叫传递给Klazz之前,会根据需要执行UserExistCheck.问题是,这是我让它运作的唯一方法.通过使用注释而不是上下文文件来实现这一点似乎对我的小脑子来说太过分了.那么......我究竟应该如何在UserExistsCheck和Klazz中注释方法?我还需要其他东西吗?另一堂课?上下文文件中还有什么内容?

java spring annotations spring-aop

3
推荐指数
1
解决办法
8039
查看次数

标签 统计

annotations ×1

java ×1

spring ×1

spring-aop ×1