Add more pointcut expressions examples
- Add examples for '@arg' and '@annotation' PCDs
This commit is contained in:
parent
bb22dc58fd
commit
b94d10d82a
|
@ -26,9 +26,26 @@ public class LoggingAspect {
|
|||
@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) throws Throwable {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package org.baeldung.dao;
|
||||
|
||||
import org.baeldung.aop.annotations.Loggable;
|
||||
import org.baeldung.model.Foo;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
@ -10,7 +11,12 @@ public class FooDao {
|
|||
return "Bazz";
|
||||
}
|
||||
|
||||
@Loggable
|
||||
public Foo create(Long id, String name) {
|
||||
return new Foo(id, name);
|
||||
}
|
||||
|
||||
public Foo merge(Foo foo) {
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package org.baeldung.model;
|
||||
|
||||
import org.baeldung.aop.annotations.Entity;
|
||||
|
||||
@Entity
|
||||
public class Foo {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
|
|
@ -2,6 +2,7 @@ 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;
|
||||
|
@ -18,6 +19,7 @@ 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;
|
||||
|
||||
|
@ -27,6 +29,8 @@ public class AopLoggingTest {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
messages = new ArrayList<>();
|
||||
|
||||
logEventHandler = new Handler() {
|
||||
@Override
|
||||
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
|
||||
|
@ -54,9 +59,6 @@ public class AopLoggingTest {
|
|||
|
||||
@Test
|
||||
public void givenLoggingAspect_whenCallDaoMethod_thenBeforeAdviceIsCalled() {
|
||||
Logger logger = Logger.getLogger(LoggingAspect.class.getName());
|
||||
logger.addHandler(logEventHandler);
|
||||
|
||||
dao.findById(1L);
|
||||
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$");
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue