Spring MVC: formal unbound in pointcut

ぃ、小莉子 提交于 2020-06-29 13:37:07

问题


I have this class in my Spring Web model-view-controller (MVC) framework. I am using aspect-oriented programming (AOP), a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. Everything is fine with this class

@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);
        }
    }   
}

I have changed the method notation getLDAPUser and now receives HttpServletRequest request as a parameter, so I modified the method as

@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);
    }
}   

and after modified this method I got this ERROR

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

in my XML:

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

回答1:


First the AspectJ basics: The error formal unbound in pointcut simply means that your advice declares a parameter not used (bound) by the corresponding pointcut (or vice versa). You can bind parameters to advice method parameters via args(), this(), target(), @annotation() etc.

The concrete problem is that in your advice you declare the parameter HttpServletRequest request. Where should the value come from? The corresponding pointcut seems to intercept another aspect's advice method which does not have any parameter of type HttpServletRequest. So as long as you do not have a source you can tap for the servlet request, you will have to create an instance by yourself.

My impression is you need to learn a bit more about AOP first. Feel free to post more code and explain where you want to get the object from, then I can probably help you fix your code.



来源:https://stackoverflow.com/questions/40607883/spring-mvc-formal-unbound-in-pointcut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!