Spring CrudRepository中方法的AspectJ切入点 [英] AspectJ pointcut on method in Spring CrudRepository

查看:107
本文介绍了Spring CrudRepository中方法的AspectJ切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring的CrudRepository与注释@RepositoryRestResource结合使用,以实现可在RESTful API中使用的简单CRUD-app.现在,我想在存储库中添加一个 AspectJ切入点,以便每当调用接口中的CRUD方法时,便会执行某些功能.

I'm using Spring's CrudRepository in combination with the annotation @RepositoryRestResource to implement a simple CRUD-app that can be used throught a RESTful API. I now want to add an AspectJ pointcut on my repository, so that some functionalities will be executed whenever a CRUD-method from the interface is called.

首先,我扩展Spring的CrudRepository在我自己的界面中添加一些自定义功能:

First, I extend Spring's CrudRepository to add some custom functionalities in my own interface:

@RepositoryRestResource(collectionResourceRel = "customers", path = "customers")
public interface CustomerRestRepository extends CrudRepository<Customer, Integer>{
   Customer findOneByGuid(@Param("customerGuid") String customerGuid);
   //Other custom methods.
}

一切正常,我可以通过我的REST客户端调用此方法.我不必实现接口CustomerRestRepository,因为Spring在后面做奇迹.这是扩展Springs CrudRepository的关键优势之一.

Everything is working fine and I'm able to call this method via my REST client. I do not have to implement the interface CustomerRestRepository since Spring is doing the job as a miracle in behind. This is one of the crucial advantages of extending Springs's CrudRepository.

我现在面临的问题是,在此自定义方法findOneByGuid() 添加一个AspectJ切入点,例如,它将记录每次调用执行后的方法.

The problem, I'm facing now, is to add an AspectJ pointcut on this custom method findOneByGuid() that will, for example, log every call of the method after it's execution.

到目前为止,我尝试过的是:

What I've tried by so far is:

@Aspect
public aspect AfterCustomerCrudAspect {
   @Pointcut(
        "execution(* com.x.y.z.CustomerRestRepository.findOneByGuid(..))")
   public void customerCrudMethod() {}

   @AfterReturning("customerCrudMethod()")
   public void doSomething() {
      //Do something
   }
}

我也尝试过:

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)

...还有许多我不记得的其他内容.所有结果都令人沮丧:永远不会应用建议.

...and many others I do not remember. All with the same frustrating result: The advice is never applied.

顺便说一句,我没有遇到任何异常,如果我尝试使用execution(* *.*(..)),则该建议效果很好-但是,当然,不仅限于方法findOneByGuid().因此,我认为我的代码总体上是正确的.

By the way, I do not face any exceptions and if I try execution(* *.*(..)), the advice is working well - but, of course, not limited to the method findOneByGuid(). Thus, I think my code is correct in general.

我知道不可能在接口上设置切入点.但是由于我不必自己实现接口CustomerRestRepository即可正常工作,因此我需要找到一种在接口方法上设置切入点的方法-或找到其他解决方案.

I know that it is not possible to set pointcuts on interfaces. But since I do not have to implement the interface CustomerRestRepository by my own to get things working, I need to find a way to set a pointcut on an interface's method - or to find some other solution.

那么,一种可能的解决方案是实现接口CustomerRestRepository.但是,然后我必须自己完成存储库的所有实现工作,并跳过使用Spring CrudRepository的优点.

Well, one possible solution to that would be to implement the interface CustomerRestRepository. But then I've to do all the implementation work for the repository by my own and skip using the advantages of Spring's CrudRepository.

因此,我的问题是,是否有可能在Spring CrudRepository 中的方法上设置AspectJ切入点.

Thus, my question is, if there is a possibility to set a AspectJ pointcut on methods in a Spring CrudRepository.

非常感谢您提供所有答案.

Many thanks in advance for all the answers.

推荐答案

好吧,我以另一种方式解决了我的问题.

Well, I solved my problem in a different way.

有时候,事情没有预期的那么复杂.最好不要在实体更改时在Spring CRUD存储库上添加AspectJ切入点以执行某些功能. (据我所知,这是完全不可能的.)

Sometimes, things are less complicated than expected. Adding an AspectJ pointcut on a Spring CRUD-repository to execute some functionalities, whenever an entity is changed was not the best idea. (And at the best of my knowledge, it is not possible at all.)

有一种更轻松的方式来实现我的要求:包javax.persistence提供了非常适合此工作的批注@EntityListeners.因此,用侦听器注释实体类并在侦听器类中实现所需的功能:

There is a much more easier way to implement my requirement: The package javax.persistence provides the annotation @EntityListeners that suites perfectly to this job. So, annotate the entity class with the listener and implement the needed functionalities within the listener class:

@Entity
@EntityListeners(CustomerEntityListener.class)
//@Table, @NamedQueries and other stuff ...
public class Customer implements Serializable {
   ...
}

EntityListener的实现:

public class CustomerEntityListener {
   @PostPersist
   public void customerPostPersist(Customer customer) {
      //Add functionalities
   }
}

EntityListener还为@PostUpdate@PostRemove等提供注释-访问此站点以获取更多信息.

The EntityListeneralso provides annotation for @PostUpdate, @PostRemove and so on - visit this site for more information.

这篇关于Spring CrudRepository中方法的AspectJ切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆