Spring Crudepository中方法的AspectJ切入点
我使用Spring的Spring Crudepository中方法的AspectJ切入点,spring,interface,aspectj,crud,pointcut,Spring,Interface,Aspectj,Crud,Pointcut,我使用Spring的CRUDepository和注释@RepositoryRestResource来实现一个简单的CRUD应用程序,可以通过RESTful API使用。现在我想在我的存储库中添加一个AspectJ切入点,这样每当调用接口中的CRUD方法时,就会执行一些功能 首先,我扩展了Spring的crudepository,在我自己的界面中添加了一些自定义功能: @RepositoryRestResource(collectionResourceRel = "customers", path
CRUDepository
和注释@RepositoryRestResource
来实现一个简单的CRUD应用程序,可以通过RESTful API使用。现在我想在我的存储库中添加一个AspectJ切入点,这样每当调用接口中的CRUD方法时,就会执行一些功能
首先,我扩展了Spring的crudepository
,在我自己的界面中添加了一些自定义功能:
@RepositoryRestResource(collectionResourceRel = "customers", path = "customers")
public interface CustomerRestRepository extends CrudRepository<Customer, Integer>{
Customer findOneByGuid(@Param("customerGuid") String customerGuid);
//Other custom methods.
}
我也试过:
1) execution(* com.x.y.z.CustomerRestRepository+.findOneByGuid(..))
2) execution(* org.springframework.data.repository.Repository+.*(..))
3) within(com.x.y.z.CustomerRestRepository)
4) annotation(RepositoryRestResource)
…还有很多我不记得的。所有这些都带来了同样令人沮丧的结果:这些建议从未得到应用
顺便说一句,我不会遇到任何异常,如果我尝试执行execution(**.*(..)
,建议会很好地工作-当然,不限于方法findOneByGuid()
。因此,我认为我的代码总体上是正确的
我知道在接口上设置切入点是不可能的。但是,由于我不必自己实现接口CustomerRestRepository
,因此我需要找到一种方法来设置接口方法的切入点,或者找到其他解决方案
好的,一个可能的解决方案是实现接口CustomerRestRepository
。但是,我必须自己完成存储库的所有实现工作,并跳过使用Spring的crudepository
的优点
因此,我的问题是,是否有可能在Springcrudepository
中的方法上设置AspectJ切入点
非常感谢所有的答案。好吧,我用另一种方式解决了我的问题 有时候,事情并不像预期的那么复杂。在Spring CRUD存储库中添加AspectJ切入点以执行某些功能,无论何时更改实体都不是最好的主意。(据我所知,这根本不可能。) 有一种更简单的方法来实现我的需求:包
javax.persistence
提供了注释@EntityListeners
,它非常适合这个工作。因此,用监听器注释实体类,并在监听器类中实现所需的功能:
@Entity
@EntityListeners(CustomerEntityListener.class)
//@Table, @NamedQueries and other stuff ...
public class Customer implements Serializable {
...
}
实体侦听器的实现
:
public class CustomerEntityListener {
@PostPersist
public void customerPostPersist(Customer customer) {
//Add functionalities
}
}
EntityListener
还为@PostUpdate
、@PostRemove
等提供注释-。这是我能想到的最好使用Spring AOP而不是AspectJ的唯一用例。SpringAOP应该非常适合Spring自己的代理基础架构。
public class CustomerEntityListener {
@PostPersist
public void customerPostPersist(Customer customer) {
//Add functionalities
}
}