在我的 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.*.*(..)) && args(a.b.c.d.RequestObject)"/>
<aop:around pointcut-ref="checkUser" method="checkUser"/>
</aop:aspect>
</aop:config>
a.b.c.UserExistsCheck 看起来像这样:
@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);
}
}
被这些东西拦截的类看起来像这样:
public class Klazz {
public ResponseObject doSomething(RequestObject request) {...}
}
这有效。 UserExistCheck 在调用传递给 Klazz 之前根据需要执行。问题是这是我让它工作的唯一方法。通过使用注释而不是上下文文件来完成这项工作对于我的小大脑来说似乎太多了。那么...我到底应该如何注释 UserExistsCheck 和 Klazz 中的方法?我还需要其他东西吗?又一堂课?上下文文件中还有什么东西吗?
您启用了基于注释的 AOP 吗?这文档说你必须添加
<aop:aspectj-autoproxy/>
到你的弹簧配置。然后你需要在你的前面添加注释checkUser
方法。看起来像你想要的@Around
建议,如上所述here.
@Aspect
public class UserExistsCheck {
@Around("execution(* a.b.c.d.*.*(..)) && args(a.b.c.d.RequestObject)")
public Object checkUser(ProceedingJoinPoint pjp) throws Throwable {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)