如何解决Spring AOP @Pointcut 和 @Before 产生 IllegalArgumentException: error at ::0 在切入点中正式未绑定
我正在做一个包含登录名和帐户的 springboot 项目。我正在尝试 @pointcut
所有控制器方法调用并验证登录信息,以及 @Before
切入点以确保会话存在。因此代码:
@Aspect
@Component
public class AuthAspect {
Logger logger = LoggerFactory.getLogger(AuthAspect.class);
@pointcut("execution(* show.xianwu.game.frisbeescorer.controller.*.*(..))")
public void validateLogin(JoinPoint joinPoint) {
// check the login information
}
@Before("validateLogin()")
public void validateSession(JoinPoint joinPoint) {
// check the session
}
}
然而,这会产生 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectingArgumentResolverBeanPostProcessor' defined in class path resource [org/springframework/data/web/config/ProjectingArgumentResolverRegistrar.class]: BeanPostProcessor before instantiation of bean Failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean Failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
。
删除 validateSession()
和 @Before
使 @pointcut
工作。我该如何解决这个问题?
解决方法
问题在于您在切入点中定义了 JoinPoint
参数。它只属于使用切入点的通知方法,而不属于切入点本身。无论如何您都不会在那里使用它,因为从不执行切入点,该方法只是一个由 @Poinctut
注释修饰的虚拟对象。所以你想要的是这个:
@Pointcut("execution(* show.xianwu.game.frisbeescorer.controller.*.*(..))")
public void validateLogin() {
// check the login information
}
除此之外(与您的问题无关),.*.*
非常具体,并且只匹配完全位于包 show.xianwu.game.frisbeescorer.controller
中的类中的方法。如果您还想在子包中包含类,请改用 ..
语法,在本例中为 show.xianwu.game.frisbeescorer.controller..*
。
由于您正在处理基于 Springboot 的项目,我建议您使用 Spring Security 功能或其他授权和身份验证框架,例如 Shiro。
如果您无论如何都不想使用它们,您可以在超类中使用 @ModelAttributes
在调用任何控制器方法之前调用一个方法。
@Controller
public class ExampleController extends BaseController {...}
public class BaseController {
@ModelAttribute
public void invokeBefore(HttpServletRequest request,HttpServletResponse response) {
// Check the auth info (e.g.,Authorization header) of the request.
}
}
此外,根据我的经验,在 SpringBoot 应用程序中直接使用 @Pointcut
是一种不好的做法。改用 customized Spring annotation。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。