0

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?

4

1 回答 1

1

你需要

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

代替

<aop:aspectj-autoproxy/>

仅供参考 - 您可以在切入点表达式中定位具体类或特定接口的实现。

Spring 只能针对公共方法,因此您可以从切入点表达式中删除“公共”部分。此外,如果您愿意,可以像这样声明您的建议以及切入点表达式:

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

你现在应该很好了,但是如果你仍然有一些问题,这里有一个使用 Spring AOP 的简单日志示例 -在 spring 中使用 AOP 进行日志记录?

于 2013-05-11T18:07:36.427 回答