Add more pointcut expressions examples

- Add examples for '@arg' and '@annotation' PCDs
This commit is contained in:
Dmitry Zinkevich 2015-12-06 18:44:56 +03:00 committed by David Morley
parent f3feab88f3
commit f47b735f83
6 changed files with 68 additions and 5 deletions

View File

@ -26,9 +26,26 @@ public class LoggingAspect {
@Pointcut("@target(org.springframework.stereotype.Repository)") @Pointcut("@target(org.springframework.stereotype.Repository)")
public void repositoryMethods() {} 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()") @Before("repositoryMethods()")
public void logMethodCall(JoinPoint jp) throws Throwable { public void logMethodCall(JoinPoint jp) {
String methodName = jp.getSignature().getName(); String methodName = jp.getSignature().getName();
logger.info(sdf.get().format(new Date()) + methodName); 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

@ -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,5 +1,6 @@
package org.baeldung.dao; package org.baeldung.dao;
import org.baeldung.aop.annotations.Loggable;
import org.baeldung.model.Foo; import org.baeldung.model.Foo;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -10,7 +11,12 @@ public class FooDao {
return "Bazz"; return "Bazz";
} }
@Loggable
public Foo create(Long id, String name) { public Foo create(Long id, String name) {
return new Foo(id, name); return new Foo(id, name);
} }
public Foo merge(Foo foo) {
return foo;
}
} }

View File

@ -1,5 +1,8 @@
package org.baeldung.model; package org.baeldung.model;
import org.baeldung.aop.annotations.Entity;
@Entity
public class Foo { public class Foo {
private Long id; private Long id;
private String name; private String name;

View File

@ -2,6 +2,7 @@ package org.baeldung.aop;
import org.baeldung.config.TestConfig; import org.baeldung.config.TestConfig;
import org.baeldung.dao.FooDao; import org.baeldung.dao.FooDao;
import org.baeldung.model.Foo;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -18,6 +19,7 @@ import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -27,6 +29,8 @@ public class AopLoggingTest {
@Before @Before
public void setUp() { public void setUp() {
messages = new ArrayList<>();
logEventHandler = new Handler() { logEventHandler = new Handler() {
@Override @Override
public void publish(LogRecord record) { public void publish(LogRecord record) {
@ -42,7 +46,8 @@ public class AopLoggingTest {
} }
}; };
messages = new ArrayList<>(); Logger logger = Logger.getLogger(LoggingAspect.class.getName());
logger.addHandler(logEventHandler);
} }
@Autowired @Autowired
@ -54,9 +59,6 @@ public class AopLoggingTest {
@Test @Test
public void givenLoggingAspect_whenCallDaoMethod_thenBeforeAdviceIsCalled() { public void givenLoggingAspect_whenCallDaoMethod_thenBeforeAdviceIsCalled() {
Logger logger = Logger.getLogger(LoggingAspect.class.getName());
logger.addHandler(logEventHandler);
dao.findById(1L); dao.findById(1L);
assertThat(messages, hasSize(1)); assertThat(messages, hasSize(1));
@ -64,4 +66,17 @@ public class AopLoggingTest {
Pattern pattern = Pattern.compile("^\\[\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}:\\d{2}:\\d{3}\\]findById$"); Pattern pattern = Pattern.compile("^\\[\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}:\\d{2}:\\d{3}\\]findById$");
assertTrue(pattern.matcher(logMessage).matches()); 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));
}
} }