1

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!

francadaval
  • 2,451
  • 3
  • 26
  • 36

1 Answers1

1

I think that you are using class-based proxies in in your application.

As annotations are not being inherited by the implementing classes in Java, try putting @InSearch on the implementation class.

See Where should I put @Transactional annotation: at an interface definition or at an implementing class?

Community
  • 1
  • 1
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • As I said, the implementation class is dinamicaly created by "Spring Document - MongoDB" based in the interface definition. Annotate the class is not an option. – francadaval Nov 16 '12 at 12:50