Amadeu Cabanilles
Amadeu Cabanilles

Reputation: 973

Spring MVC: formal unbound in pointcut

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" />

Upvotes: 0

Views: 2531

Answers (1)

kriegaex
kriegaex

Reputation: 67437

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.

Upvotes: 1

Related Questions