【问题标题】:Spring MVC: formal unbound in pointcutSpring MVC:切入点中的正式未绑定
【发布时间】:2020-06-19 11:58:45
【问题描述】:

我的 Spring Web 模型-视图-控制器 (MVC) 框架中有这个类。我正在使用面向方面的编程 (AOP),这是一种旨在通过允许分离横切关注点来增加模块化的编程范式。 这门课一切都很好

@Aspect
public class MarketingAspect extends ServiceSupport {

    @Pointcut("execution(* com.tdk.iot.services.client.LicenseService.*(..))")
    public void handleServiceMethod() {
    }

    @Pointcut("execution(* com.tdk.iot.services.client.ApplicantService.*(..))")
    public void handleApplicantServiceMethod() {
    }


    @Before("com.tdk.iot.services.aop.ApplicantAspect.handleServiceMethod()")
    public void before(JoinPoint _jp) {
        User user = getLDAPUser();
        if(user != null &&( (user.getUserRole() != UserRole.MARKETING)) {
            throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MARKETING);
        }
    }


    @Before("com.tdk.iot.services.aop.ApplicantAspect.handleApplicantServiceMethod()")
    public void checkRolebefore(JoinPoint _jp) {
        User user = getLDAPUser();
        if(user != null &&( (user.getUserRole() != UserRole.MARKETING))) {
            throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MARKETING);
        }
    }   
}

我已经更改了getLDAPUser的方法表示法,现在接收HttpServletRequest请求作为参数,所以我将方法修改为

@Before("com.tdk.iot.services.aop.ApplicantAspect.handleApplicantServiceMethod()")
public void checkRolebefore(JoinPoint _jp, HttpServletRequest request) {
    User user = getLDAPUser(request);
    if(user != null &&( (user.getUserRole() != UserRole.MARKETING))) {
        throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MARKETING);
    }
}   

修改这个方法后我得到了这个错误

java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

在我的 XML 中:

<!-- Scan for aspects -->
    <aop:aspectj-autoproxy />       
    <bean id="marketingAspect" class="com.tdk.iot.services.aop.MarketingAspect" />

【问题讨论】:

  • 你是否在你的配置xml文件中启用了spring aop? 注释
  • 你能显示你的应用程序配置吗
  • 当我修改方法时问题开始如下: public void checkRolebefore(JoinPoint _jp, HttpServletRequest request) {
  • 你能把你用过这个注解的代码贴出来

标签: java spring spring-mvc aop


【解决方案1】:

首先是 AspectJ 基础知识:错误formal unbound in pointcut 仅表示您的通知声明了相应切入点未使用(绑定)的参数(反之亦然)。您可以通过args()this()target()@annotation() 等将参数绑定到通知方法参数。

具体的问题是,在您的建议中,您声明了参数HttpServletRequest request。价值应该从哪里来?相应的切入点似乎拦截了另一个没有HttpServletRequest 类型参数的方面的通知方法。因此,只要您没有可以点击 servlet 请求的源,您就必须自己创建一个实例。

我的印象是你需要先了解更多关于 AOP 的知识。随意发布更多代码并说明您想从哪里获取对象,然后我可能会帮助您修复您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多