Question

I'm really having problem in advicing methods with pointcuts expressions. I have the following configuration:

Spring 3.1.2.RELEASE

pom.xml

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.2</version>
    </dependency>

servlet.xml

    <aop:aspectj-autoproxy/>

the class I want to advice

@Repository(value="userDaoImpl")
@Transactional
public class UserDaoImpl implements UserDao{

    //attributes and methods

    @Override
    public void delete(ProfiledUser user) throws HibernateException
    {
        sessionFactory.getCurrentSession().delete(user);        
    }
}

it implements the UserDao Interface which extends a GenericDao<T> Interface

here's my Advice

@Aspect
@Component("userDeletionAspect")
public class UserDeletionAspect {

    @Pointcut("execution(public void aa.bb.cc.dd.UserDaoImpl.delete(..))")
    public void objectDeletionPointcut(){}

    @Before("objectDeletionPointcut()")
    public void notifyDeletion(JoinPoint jp)
    {       
        System.out.println("pointcut executed");
    }
}

which doesn't work. It means that when the UserDaoImpl's delete method is executed it isn't intercepted at all.

From Spring Documentation I read that spring proxies work with Interfaces so I tried to change the Pointcut definition as follow:

  @Pointcut("execution(* aa.bb.cc.dd.GenericDao.delete(..))")

but nothing changes. How can I intercept the .delete() method of the UserDaoImpl class?

Was it helpful?

Solution

You need

<aop:aspectj-autoproxy>
    <aop:include name="userDeletionAspect"/>
</aop:aspectj-autoproxy>

instead of

<aop:aspectj-autoproxy/>

FYI - You can target either concrete classes or implementations of a specific interface in your pointcut expressions.

Spring can only target public methods so you can remove the 'public' part from your pointcut expressions. Also, if you want you can declare your advice along with the pointcut expression like so:

@Before("execution(void aa.bb.cc.dd.UserDaoImpl.delete(..))")
public void notifyDeletion(JoinPoint jp) {       
     System.out.println("pointcut executed");
}

You should be good to go now, but If you are still having some issues, here's a simple logging example using Spring AOP - logging with AOP in spring?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top