Create AOP examples
- Use different types of advice - Use various types of pointcut expressions
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package org.baeldung.aop;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.baeldung.events.FooCreationEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class PublishingAspect {
|
||||
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
@Autowired
|
||||
public void setEventPublisher(ApplicationEventPublisher eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Pointcut("@target(org.springframework.stereotype.Repository)")
|
||||
public void repositoryMethods() {}
|
||||
|
||||
@Pointcut("execution(* *..create*(Long,..))")
|
||||
public void firstLongParamMethods() {}
|
||||
|
||||
@Pointcut("repositoryMethods() && firstLongParamMethods()")
|
||||
public void entityCreationMethods() {}
|
||||
|
||||
@AfterReturning(value = "entityCreationMethods()", returning = "entity")
|
||||
public void logMethodCall(JoinPoint jp, Object entity) throws Throwable {
|
||||
eventPublisher.publishEvent(new FooCreationEvent(entity));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user