I'm trying to make an pointcut of type @Around to intercept all methods of beans annotated with @Repository.
I've tried
@Around("execution(* (@org.springframework.stereotype.Repository *).*(..))")
public void aspect() {
}
Also(should be same)
@Around("@within(org.springframework.stereotype.Repository)")
public void aspect() {
}
From my testing, these poincut expressions match just the methods from the type directly annotated(not the ones inherited like the ones autogenerated DAO (findAll, findById). I want to intercept all methods (from child type annotated and also inherited methods from other types like CrudRepository.
One of usages is this
@org.springframework.stereotype.Repository
public interface SubRepository extends CrudRepository {
}
How could i implement this or it is not possible?. Thank you.
Here is an MCVE. Next time, please provide one by yourself. It should not be my job to do that for you.
Interface + sub-interface:
Abstract class extending sub-interface + concrete class extending it:
Class directly annotated with
@Repositoryas a counter-example:Demo application:
Aspect:
Like you said, the first advice should only kick in for classes directly annotated with
@Repository, i.e. in this example for theDirectlyAnnotatedRepositorybean.The second advice however targets all types annotated with
@Repositoryand their subtypes - please note the+. Depending on what we want, we could add one moreexecutionpointcut to limit the intercepted methods, but I simply chose all. Please also note that without the secondwithinpointcut, we would target many Spring-internal components, too, which would work for some, but throw exceptions for others. So we are limiting the scope to only repository beans in our own application. Instead, we could also exclude the Spring ones we do not want.Console log: