Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advice not getting called in Spring-AOP

I have declared the following Aspect which advices a dao call, I'm trying to run @Before advice but its not working.

Here goes the aspect.

package com.hedgebenefits.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AccessControlAspect {
    @Before("within(com.hedgebenefits.daos..*) && execution(public * *(..))")
    public void daoCall() {
        System.out.println("Before advice invoked for DAO method called ");
    }
}

My application-context.xml has the following tag

<aop:aspectj-autoproxy/>

My Dao class is as follows:

package com.hedgebenefits.daos.impl;

import com.hedgebenefits.daos.AdminDao;
import com.hedgebenefits.domain.Admin;
import org.springframework.stereotype.Repository;

@Repository
public class AdminDaoImpl implements AdminDao{
    @Override
    public void save(Admin admin) {
    }
}

I put a breakpoint but I can see that its not active, I'm definitely doing some silly mistake here but can't figure out. Pl. advice.

like image 419
tintin Avatar asked Aug 20 '12 20:08

tintin


1 Answers

Your aspect needs to be a part of your application context.

  1. If you are using component-scan, either add @Component to your AccessControllerAspect, or setup the component-scan filters to include @Aspect annotations. To setup the filters, take a look at section 3.10.3 of the Spring documentation (Using filters to customize scanning).
  2. If you are using xml configuration, add a bean for AccessControllerAspect.

The act of adding aop:aspectj-autoproxy is not enough. This tells beans that are already a part of your application context how to do the aspecting, it does not automatically include them.

like image 91
nicholas.hauschild Avatar answered Oct 26 '22 17:10

nicholas.hauschild