diff --git a/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/DBExceptionHandlingAspect.java b/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/DBExceptionHandlingAspect.java index a2635b7..a019e00 100644 --- a/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/DBExceptionHandlingAspect.java +++ b/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/DBExceptionHandlingAspect.java @@ -1,12 +1,15 @@ package com.trendyol.bootcamp.spring.ch05.aspect; import com.trendyol.bootcamp.spring.ch05.exception.RewardDataAccessException; +import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; -@Aspect +@Aspect +@Component public class DBExceptionHandlingAspect { public static final String EMAIL_FAILURE_MSG = "Failed sending an email to Mister Smith : "; @@ -19,7 +22,10 @@ public class DBExceptionHandlingAspect { // - Configure this advice method to enable logging of // exceptions thrown by Repository class methods. // - Select the advice type that seems most appropriate. - + @AfterThrowing( + pointcut = "execution(* com.trendyol.bootcamp.spring.ch05.repository.*.*Repository.*(..))", + throwing = "e" + ) public void implExceptionHandling(RewardDataAccessException e) { // Log a failure warning logger.warn(EMAIL_FAILURE_MSG + e + "\n"); diff --git a/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/LoggingAspect.java b/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/LoggingAspect.java index 92c6e4d..463f2b6 100644 --- a/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/LoggingAspect.java +++ b/src/main/java/com/trendyol/bootcamp/spring/ch05/aspect/LoggingAspect.java @@ -5,8 +5,12 @@ import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; // TODO-02: Use AOP to log a message before // any repository's find...() method is invoked. @@ -15,7 +19,8 @@ // - Optionally place @Autowired annotation on the constructor // where `MonitorFactory` dependency is being injected. // (It is optional since there is only a single constructor in the class.) - +@Aspect +@Component public class LoggingAspect { public final static String BEFORE = "'Before'"; public final static String AROUND = "'Around'"; @@ -34,7 +39,7 @@ public LoggingAspect(MonitorFactory monitorFactory) { // - Decide which advice type is most appropriate // - Write a pointcut expression that selects only find* methods on // our repository classes. - + @Before("execution(* com.trendyol.bootcamp.spring.ch05.repository.*.*Repository.find*(..))") public void implLogging(JoinPoint joinPoint) { // Do not modify this log message or the test will fail logger.info(BEFORE + " advice implementation - " + joinPoint.getTarget().getClass() + // @@ -47,18 +52,17 @@ public void implLogging(JoinPoint joinPoint) { // - Mark this method as an around advice. // - Write a pointcut expression to match on all update* methods // on all Repository classes. - + @Around("execution(* com.trendyol.bootcamp.spring.ch05.repository.*.*Repository.update*(..))") public Object monitor(ProceedingJoinPoint repositoryMethod) throws Throwable { String name = createJoinPointTraceName(repositoryMethod); Monitor monitor = monitorFactory.start(name); try { // Invoke repository method ... - + return repositoryMethod.proceed(); // TODO-08: Add the logic to proceed with the target method invocation. // - Be sure to return the target method's return value to the caller // and delete the line below. - return new String("Delete this line after completing TODO-08"); } finally { monitor.stop(); diff --git a/src/main/java/com/trendyol/bootcamp/spring/ch05/config/AspectsConfig.java b/src/main/java/com/trendyol/bootcamp/spring/ch05/config/AspectsConfig.java index 9ce19f4..b7b1989 100644 --- a/src/main/java/com/trendyol/bootcamp/spring/ch05/config/AspectsConfig.java +++ b/src/main/java/com/trendyol/bootcamp/spring/ch05/config/AspectsConfig.java @@ -3,7 +3,9 @@ import com.trendyol.bootcamp.spring.ch05.monitor.jamon.JamonMonitorFactory; import com.trendyol.bootcamp.spring.ch05.monitor.MonitorFactory; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; // TODO-04: Update Aspect related configuration // - Add a class-level annotation to scan for components @@ -14,6 +16,8 @@ // application since it will be automatically added through // auto configuration.) @Configuration +@ComponentScan("com.trendyol.bootcamp.spring.ch05.aspect") +@EnableAspectJAutoProxy public class AspectsConfig { @Bean diff --git a/src/test/java/com/trendyol/bootcamp/spring/ch05/SystemTestConfig.java b/src/test/java/com/trendyol/bootcamp/spring/ch05/SystemTestConfig.java index 70260f5..0fb0c22 100644 --- a/src/test/java/com/trendyol/bootcamp/spring/ch05/SystemTestConfig.java +++ b/src/test/java/com/trendyol/bootcamp/spring/ch05/SystemTestConfig.java @@ -1,5 +1,6 @@ package com.trendyol.bootcamp.spring.ch05; +import com.trendyol.bootcamp.spring.ch05.config.AspectsConfig; import com.trendyol.bootcamp.spring.ch05.config.RewardsConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -15,7 +16,7 @@ * and you should see one line of LoggingAspect output in the console. */ @Configuration -@Import({RewardsConfig.class}) +@Import({RewardsConfig.class, AspectsConfig.class}) public class SystemTestConfig { diff --git a/src/test/java/com/trendyol/bootcamp/spring/ch05/TestConstants.java b/src/test/java/com/trendyol/bootcamp/spring/ch05/TestConstants.java index d5bbb38..027970e 100644 --- a/src/test/java/com/trendyol/bootcamp/spring/ch05/TestConstants.java +++ b/src/test/java/com/trendyol/bootcamp/spring/ch05/TestConstants.java @@ -10,5 +10,5 @@ public class TestConstants { // TODO-01: Enable checking of console output in our Tests. // - Change the value below to true - public static final boolean CHECK_CONSOLE_OUTPUT = false; + public static final boolean CHECK_CONSOLE_OUTPUT = true; } diff --git a/src/test/java/com/trendyol/bootcamp/spring/ch05/service/RewardNetworkTests.java b/src/test/java/com/trendyol/bootcamp/spring/ch05/service/RewardNetworkTests.java index c63bac5..b73357b 100644 --- a/src/test/java/com/trendyol/bootcamp/spring/ch05/service/RewardNetworkTests.java +++ b/src/test/java/com/trendyol/bootcamp/spring/ch05/service/RewardNetworkTests.java @@ -67,7 +67,7 @@ void testRewardForDining(CaptureSystemOutput.OutputCapture capture) { // TODO-06: Run this test. It should pass AND you should see TWO lines of // log output from the LoggingAspect on the console - int expectedMatches = 2; + int expectedMatches = 4; checkConsoleOutput(capture, expectedMatches); // TODO-09: Save all your work, and change the expected matches value above from 2 to 4.