Create AOP examples
- Use different types of advice - Use various types of pointcut expressions
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package org.baeldung.aop;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class LoggingAspect {
|
||||
|
||||
private static Logger logger = Logger.getLogger(LoggingAspect.class.getName());
|
||||
|
||||
private ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("[yyyy-mm-dd hh:mm:ss:SSS]");
|
||||
}
|
||||
};
|
||||
|
||||
@Pointcut("@target(org.springframework.stereotype.Repository)")
|
||||
public void repositoryMethods() {}
|
||||
|
||||
@Before("repositoryMethods()")
|
||||
public void logMethodCall(JoinPoint jp) throws Throwable {
|
||||
String methodName = jp.getSignature().getName();
|
||||
logger.info(sdf.get().format(new Date()) + methodName);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class PerformanceAspect {
|
||||
private static Logger logger = Logger.getLogger(PerformanceAspect.class.getName());
|
||||
|
||||
@Pointcut("within(@org.springframework.stereotype.Repository *)")
|
||||
public void repositoryClassMethods() {};
|
||||
public void repositoryClassMethods() {}
|
||||
|
||||
@Around("repositoryClassMethods()")
|
||||
public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,16 @@
|
||||
package org.baeldung.dao;
|
||||
|
||||
import org.baeldung.model.Foo;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class FooDao {
|
||||
|
||||
public String findById(Long id) {
|
||||
return "Bazz";
|
||||
}
|
||||
|
||||
public Foo create(Long id, String name) {
|
||||
return new Foo(id, name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.events;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class FooCreationEvent extends ApplicationEvent {
|
||||
|
||||
public FooCreationEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.baeldung.events;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Component
|
||||
public class FooCreationEventListener implements ApplicationListener<FooCreationEvent> {
|
||||
|
||||
private static Logger logger = Logger.getLogger(FooCreationEventListener.class.getName());
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(FooCreationEvent event) {
|
||||
logger.info("Created foo instance: " + event.getSource().toString());
|
||||
}
|
||||
}
|
||||
19
spring-mvc-java/src/main/java/org/baeldung/model/Foo.java
Normal file
19
spring-mvc-java/src/main/java/org/baeldung/model/Foo.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.model;
|
||||
|
||||
public class Foo {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Foo(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user