Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aspect Oriented Programming Homework #16

Open
wants to merge 5 commits into
base: feature/aspect-oriented-programming
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Chapter 1.pdf
Binary file not shown.
Binary file added Chapter 2.pdf
Binary file not shown.
Binary file added Chapter 3.pdf
Binary file not shown.
Binary file added Chapter 4.pdf
Binary file not shown.
Binary file added Chapter 5.pdf
Binary file not shown.
Binary file added Spring Training-ALL.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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 : ";
Expand All @@ -19,7 +22,7 @@ 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(* *..*Repository.*(..))", throwing = "e")
public void implExceptionHandling(RewardDataAccessException e) {
// Log a failure warning
logger.warn(EMAIL_FAILURE_MSG + e + "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -16,6 +20,8 @@
// where `MonitorFactory` dependency is being injected.
// (It is optional since there is only a single constructor in the class.)

@Component
@Aspect
public class LoggingAspect {
public final static String BEFORE = "'Before'";
public final static String AROUND = "'Around'";
Expand All @@ -35,6 +41,7 @@ public LoggingAspect(MonitorFactory monitorFactory) {
// - Write a pointcut expression that selects only find* methods on
// our repository classes.

@Before("execution(* *..*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() + //
Expand All @@ -47,7 +54,7 @@ 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(* *..*Repository.update*(..))")
public Object monitor(ProceedingJoinPoint repositoryMethod) throws Throwable {
String name = createJoinPointTraceName(repositoryMethod);
Monitor monitor = monitorFactory.start(name);
Expand All @@ -58,7 +65,7 @@ public Object monitor(ProceedingJoinPoint repositoryMethod) throws Throwable {
// - 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");
return repositoryMethod.proceed();

} finally {
monitor.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,6 +16,8 @@
// application since it will be automatically added through
// auto configuration.)
@Configuration
@ComponentScan(basePackages = "com.trendyol.bootcamp.spring.ch05.aspect")
@EnableAspectJAutoProxy
public class AspectsConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down