I'm trying to use Spring aspects to do save aditional data while storing documents with MongoRepository
. The target is this interface:
@InSearch
public interface ItemRepository extends MongoRepository<Item, Long>,
ItemRepositoryCustom
{
List<Item> findAllByUsername( String username );
List<Item> findAllBySessionId( Long sessionId );
}
When I use this pointcut:
@Pointcut( "execution(* save(..)) && " +
"target(org.springframework.data.mongodb.repository.MongoRepository)" )
private void saveEntity()
{}
This method runs ok before open
method is called:
@Before( "saveEntity() && args(entity)" )
public void beforeSavingEntity( JoinPoint jp, AuditedBean entity )
{ ... }
But I want it to run only with interfaces annotated with @InSearch
so I tried to define the pointcut that way:
@Pointcut( "execution(* save(..)) && " +
"target(org.springframework.data.mongodb.repository.MongoRepository) && " +
"@target(xx.annotations.InSearch)" )
private void saveEntity()
{}
The annotation definition:
@Target( { ElementType.TYPE} )
@Retention(RetentionPolicy.RUNTIME)
public @interface InSearch
{}
With this pointcut the method beforeSavingEntity
is not called. I've not any error on log. I'm not sure if I had understood the documentation about @target
and his differences with @within
. I've tried also with @within
and didn't work either.
How must I define this pointcut to select save
methods execution in a class that implements an interface that extends MongoRepository
and is annotated with @InSearch
?
Thank you!