Merge pull request #292 from zinch84/aop_examples

Aop examples
This commit is contained in:
David Morley 2015-12-15 19:53:01 -06:00
commit c654cb1831
14 changed files with 407 additions and 2 deletions

View File

@ -0,0 +1,51 @@
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() {}
@Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)")
public void loggableMethods() {}
@Pointcut("@args(org.baeldung.aop.annotations.Entity)")
public void methodsAcceptingEntities() {}
@Before("repositoryMethods()")
public void logMethodCall(JoinPoint jp) {
String methodName = jp.getSignature().getName();
logger.info(sdf.get().format(new Date()) + methodName);
}
@Before("loggableMethods()")
public void logMethod(JoinPoint jp) {
String methodName = jp.getSignature().getName();
logger.info("Executing method: " + methodName);
}
@Before("methodsAcceptingEntities()")
public void logMethodAcceptionEntityAnnotatedBean(JoinPoint jp) {
logger.info("Accepting beans with @Entity annotation: " + jp.getArgs()[0]);
}
}

View File

@ -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 {

View File

@ -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));
}
}

View File

@ -0,0 +1,11 @@
package org.baeldung.aop.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Entity {
}

View File

@ -0,0 +1,11 @@
package org.baeldung.aop.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Loggable {
}

View File

@ -1,10 +1,22 @@
package org.baeldung.dao;
import org.baeldung.aop.annotations.Loggable;
import org.baeldung.model.Foo;
import org.springframework.stereotype.Repository;
@Repository
public class FooDao {
public String findById(Long id) {
return "Bazz";
}
@Loggable
public Foo create(Long id, String name) {
return new Foo(id, name);
}
public Foo merge(Foo foo) {
return foo;
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.events;
import org.springframework.context.ApplicationEvent;
public class FooCreationEvent extends ApplicationEvent {
public FooCreationEvent(Object source) {
super(source);
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.model;
import org.baeldung.aop.annotations.Entity;
@Entity
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 + '\'' +
'}';
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="perfomanceMeter" class="org.baeldung.aop.PerformanceAspect"/>
<bean id="fooDao" class="org.baeldung.dao.FooDao"/>
<aop:config>
<aop:pointcut id="anyDaoMethod" expression="@target(org.springframework.stereotype.Repository)"/>
<aop:aspect ref="perfomanceMeter">
<aop:around method="measureMethodExecutionTime" pointcut-ref="anyDaoMethod"/>
</aop:aspect>
</aop:config>
</beans>

View File

@ -0,0 +1,82 @@
package org.baeldung.aop;
import org.baeldung.config.TestConfig;
import org.baeldung.dao.FooDao;
import org.baeldung.model.Foo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class AopLoggingTest {
@Before
public void setUp() {
messages = new ArrayList<>();
logEventHandler = new Handler() {
@Override
public void publish(LogRecord record) {
messages.add(record.getMessage());
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
};
Logger logger = Logger.getLogger(LoggingAspect.class.getName());
logger.addHandler(logEventHandler);
}
@Autowired
private FooDao dao;
private Handler logEventHandler;
private List<String> messages;
@Test
public void givenLoggingAspect_whenCallDaoMethod_thenBeforeAdviceIsCalled() {
dao.findById(1L);
assertThat(messages, hasSize(1));
String logMessage = messages.get(0);
Pattern pattern = Pattern.compile("^\\[\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}:\\d{2}:\\d{3}\\]findById$");
assertTrue(pattern.matcher(logMessage).matches());
}
@Test
public void givenLoggingAspect_whenCallLoggableAnnotatedMethod_thenMethodIsLogged() {
dao.create(42L, "baz");
assertThat(messages, hasItem("Executing method: create"));
}
@Test
public void givenLoggingAspect_whenCallMethodAcceptingAnnotatedArgument_thenArgumentIsLogged() {
Foo foo = new Foo(42L, "baz");
dao.merge(foo);
assertThat(messages, hasItem("Accepting beans with @Entity annotation: " + foo));
}
}

View File

@ -0,0 +1,67 @@
package org.baeldung.aop;
import org.baeldung.config.TestConfig;
import org.baeldung.dao.FooDao;
import org.baeldung.events.FooCreationEventListener;
import org.baeldung.model.Foo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class AopPublishingTest {
@Before
public void setUp() {
logEventHandler = new Handler() {
@Override
public void publish(LogRecord record) {
messages.add(record.getMessage());
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
};
messages = new ArrayList<>();
}
@Autowired
private FooDao dao;
private Handler logEventHandler;
private List<String> messages;
@Test
public void givenPublishingAspect_whenCallCreate_thenCreationEventIsPublished() {
Logger logger = Logger.getLogger(FooCreationEventListener.class.getName());
logger.addHandler(logEventHandler);
dao.create(1L, "Bar");
String logMessage = messages.get(0);
Pattern pattern = Pattern.compile("Created foo instance: " +
Pattern.quote(new Foo(1L, "Bar").toString()));
assertTrue(pattern.matcher(logMessage).matches());
}
}

View File

@ -0,0 +1,67 @@
package org.baeldung.aop;
import org.baeldung.dao.FooDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/org/baeldung/aop/beans.xml")
public class AopXmlConfigPerformanceTest {
@Before
public void setUp() {
logEventHandler = new Handler() {
@Override
public void publish(LogRecord record) {
messages.add(record.getMessage());
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
};
messages = new ArrayList<>();
}
@Autowired
private FooDao dao;
private Handler logEventHandler;
private List<String> messages;
@Test
public void givenPerformanceAspect_whenCallDaoMethod_thenPerformanceMeasurementAdviceIsCalled() {
Logger logger = Logger.getLogger(PerformanceAspect.class.getName());
logger.addHandler(logEventHandler);
final String entity = dao.findById(1L);
assertThat(entity, notNullValue());
assertThat(messages, hasSize(1));
String logMessage = messages.get(0);
Pattern pattern = Pattern.compile("Execution of findById took \\d+ ms");
assertTrue(pattern.matcher(logMessage).matches());
}
}

View File

@ -5,7 +5,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = {"org.baeldung.dao", "org.baeldung.aop"})
@ComponentScan(basePackages = {"org.baeldung.dao", "org.baeldung.aop", "org.baeldung.events"})
@EnableAspectJAutoProxy
public class TestConfig {
}