diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java b/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java index 9317677fe2..72ac610abd 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java +++ b/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java @@ -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]); + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java b/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java new file mode 100644 index 0000000000..f964c3979e --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java @@ -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 { +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java b/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java new file mode 100644 index 0000000000..ef2863957f --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java @@ -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 { +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java b/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java index e0da61c547..f204440b2d 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java +++ b/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java @@ -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; + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java b/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java index 17510f4836..0b1a553afc 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java +++ b/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java @@ -1,5 +1,8 @@ package org.baeldung.model; +import org.baeldung.aop.annotations.Entity; + +@Entity public class Foo { private Long id; private String name; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java b/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java index 7aff17c424..4c8fcd50a8 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java +++ b/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java @@ -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)); + } }