简体   繁体   中英

Customize 'find' logic in JPA repositories

I have the following question for the community: is there any way to customize the 'find' logic for all my JPA repositories?

For example, I would like my custom 'find' logic to be invoked automatically everytime a method containing the word 'find' is created in my JPA repositories (findAll, findById, findOne, ..).

I'm trying to follow the official documentation ( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.customize-base-repository ) but I don't understand if I'm doing something wrong or if it's not possible to implement what I have in mind with this straregy.

Thank you in advance.

You can do it with the use of Spring AOP (I suggest you read the docs). When using annotations, your pointcut expression (inside an aspect) would look like so:

@Pointcut("execution(*org.yourpackage.repositories.*.find*(..))")
public void customFind() {
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM