Reputation: 19789
I've made an application with Spring Roo (I'm still a newbie) and I'd like to do some processing after the entity is persisted. I've set up the application with Service and DAO layer. In the service I created a custom method called triggerChange(MyEntity myEntity). I'd like that this method would be invoked after saving the entity but I don't know how could I call that method without modifying the *ServiceImpl_Roo_Service managed by Roo (which shouldn't be editted).
So I have a code like this one:
The service:
public class MyEntityServiceImpl implements MyEntityService {
//this is the method I want to invoke inside or after invoking save()
public void triggerChange(MyEntity myEntity) {
...
}
}
The Aspect for the service:
privileged aspect MyEntityServiceImpl_Roo_Service {
...
public void MyEntityServiceImpl.saveMyEntity(MyEntity myEntity) {
myEntityRepository.save(myEntity);
}
}
How could I customize save method?
Thanks
Upvotes: 1
Views: 1841
Reputation: 2842
I've just had to perform some business logic after a save method, just like you wanted.
Let's say I have to log that a save operation has been performed. For doing so, I've created this aspect:
package com.malsolo.aspects;
import org.apache.log4j.Logger;
import com.malsolo.myproject.domain.MyEntity;
aspect MyEntityAspect {
private final Logger logger = Logger.getLogger(MyEntityAspect.class);
pointcut persistEntity() : execution(* MyEntity.persist(..));
public Logger getLogger() {
return logger;
}
after() : persistEntity() {
logger().info("Entity persisted "+thisJoinPoint);
}
}
It writes:
2011-11-30 11:47:27,056 [main] INFO com.malsolo.aspects.ModifyRolAspect - Entity persisted execution(void com.malsolo.myproject.domain.MyEntity.persist())
I hope it helps you.
Notes:
Upvotes: 0
Reputation: 2842
You have AspectJ enabled in your Spring application thanks to Roo. Just create an Aspect (after or around the method call)
You also can to move the methods from the Roo aspects (.aj)
In STS select the desired methods (and even attributes) from the aspect class, right click, refactor->push in... and press Review-OK or OK directly (I recommend the former in order to review the changes)
Another way: with Roo running, just create a method/attribute with the same signature in the class. Roo will remove the equivalent from the aspect.
Upvotes: 4