실습 5일차_AOP

 

 

 Spring AOP

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service = ctx.getBean("userService", UserService.class);
		System.out.println("반환값:"+ service.insert());

	}
}

MainClass2.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.DeptService;

public class MainClass2 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		DeptService service = ctx.getBean("deptService", DeptService.class);
		System.out.println("반환값:"+ service.selectList());

	}
}

MainClass3.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.DeptService;
import com.service.EmpService;

public class MainClass3 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		EmpService service = ctx.getBean("empService", EmpService.class);
		try {
		  service.update();
		}catch(Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
	}
}

MainClass4.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.EmpService;
import com.service.PersonService;

public class MainClass4 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		PersonService service = ctx.getBean("personService", PersonService.class);
		try {
		  String str = service.delete();
		  System.out.println("반환값:"+str);
		}catch(Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
	}
}

 

DeptServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class DeptServiceAspect {

	@AfterReturning(pointcut = "execution(* selectList(..))", returning = "retVal")
	public void doAccessCheck(Object retVal) {
		System.out.println("DeptServiceAspect.AfterReturning.selectList");
		System.out.println("리턴값:" + retVal);
	}
}

EmpServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

//부가기능 처리: aspect
@Aspect
public class EmpServiceAspect {

	@AfterThrowing(pointcut = "execution(* update(..))", throwing = "ex")
	public void doRecoveryActions(Exception ex) {
		System.out.println("EmpServiceAspect.AfterThrowing" 
	     + ex.getMessage());
	}
}

PersonServiceAspect.java

package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

//부가기능 처리: aspect
@Aspect
public class PersonServiceAspect {
 
		@Around("execution(* delete(..))")
	    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
	        System.out.println("Before Advice");
	        Object retVal = pjp.proceed();
	        System.out.println("After Advice");
	        System.out.println("retVal:" + retVal);
	        return retVal;
	    }
}

UserServiceAspect.java

package com.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

//부가기능 처리: aspect
@Aspect
public class UserServiceAspect {
  //1. pointcut와 advice 분리
	@Pointcut("execution(* list(..))")// the pointcut expression
	private void anyOldTransfer() {}// the pointcut signature
	
	@Before("anyOldTransfer()")
    public void doAccessCheck(JoinPoint point) {
		System.out.println("point:" + point);
		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.list");
    }
	
	//2. pointcut와 advice 합치기
	@Before("execution(* insert*(..))")
    public void doAccessCheck2(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.insert");
    }
	
	@After("execution(* insert*(..))")
    public void doAccessCheck3(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.AfterAdvice.insert");
    }
}

 

DeptService.java

package com.service;
import java.util.Arrays;
import java.util.List;

//타겟 (target object) ==> 핵심기능
public class DeptService {

	public List<String> selectList(){
		System.out.println("핵심기능:DeptService.selectList");
		return Arrays.asList("AAA","BBB");
	}
}

EmpService.java

package com.service;

//타겟 (target object) ==> 핵심기능
public class EmpService {

	public void update() {
		int num = 10/2;
		System.out.println("핵심기능:EmpService.update");
	}
}

PersonService.java

package com.service;

//타겟 (target object) ==> 핵심기능
public class PersonService {

	public String delete() {
		int num = 10/0;
		System.out.println("핵심기능:PersonService.insert");
		return "Hello";
	}
}

UserService.java

package com.service;

//타겟 (target object) ==> 핵심기능
public class UserService {

	//pointcut
	public String list() {
		return "UserService.list";
	}

	public String insert() {
		System.out.println("핵심기능:UserService.insert");
		return "UserService.insert";
	}
	public String insert2() {
		return "UserService.insert2";
	}	
}

user.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<aop:aspectj-autoproxy />
	<!-- 1. BeforeAdvice와 AfterAdvice 실습 -->
	<bean id="userService" class="com.service.UserService" />
	<bean id="userAspect" class="com.aspect.UserServiceAspect" />
	
	<!-- 2. AfterReturningAdvice 실습 -->
	<bean id="deptService" class="com.service.DeptService" />
	<bean id="deptAspect" class="com.aspect.DeptServiceAspect" />
	
	<!-- 3 AfterThrowingAdvice 실습 -->
	<bean id="empService" class="com.service.EmpService" />
	<bean id="empAspect" class="com.aspect.EmpServiceAspect" />
	
	<!-- 4 AroundAdvice 실습 -->
	<bean id="personService" class="com.service.PersonService" />
	<bean id="personAspect" class="com.aspect.PersonServiceAspect" />
</beans>

 

* aop 추가 방법

[Namespaces] → [Configure Namespaces] 팝업창 OK 버튼 마우스로 클릭

 

* 출력 화면 

MainClass 출력화면

 

MainClass2 출력화면

 

MainClass3 출력화면

 

MainClass4 출력화면

 

 Spring AOP Component_Scan

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
			
		UserService service = ctx.getBean("userService", UserService.class);
		
		System.out.println(service.insert());
		//System.out.println(service.list());

	}
}

MainClass2.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.DeptService;
import com.service.UserService;

public class MainClass2 {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
				
		DeptService service2 = ctx.getBean("deptService", DeptService.class);
		System.out.println("반환값:" + service2.selectList());
		//System.out.println(service.list());
		
	}
}

MainClass3.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.EmpService;

public class MainClass3 {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
			
		EmpService service = ctx.getBean("empService", EmpService.class);
			
		try {
			service.update();
		} catch (Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
		
	}
}

MainClass4.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.EmpService;
import com.service.PersonService;

public class MainClass4 {
	public static void main(String[] args) {

		ApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		PersonService service = ctx.getBean("personService", PersonService.class);
		try {
		  String str = service.delete();
		  System.out.println("반환값:"+str);
		}catch(Exception e) {
			System.out.println("error");
		}
		System.out.println("정상종료");
	}
}

 

DeptServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class DeptServiceAspect {

	@AfterReturning(pointcut = "execution(* selectList(..))", returning = "retVal")
	public void doAccessCheck(Object retVal) {
		System.out.println("DeptServiceAspect.AfterReturning.selectList");
		System.out.println("리턴값:" + retVal);
	}
}

EmpServiceAspect.java

package com.aspect;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

//부가기능 처리: aspect
@Component
@Aspect
public class EmpServiceAspect {

	@AfterThrowing(pointcut = "execution(* update(..))", throwing = "ex")
	public void doRecoveryActions(Exception ex) {
		System.out.println("EmpServiceAspect.AfterThrowing" 
	     + ex.getMessage());
	}
}

PersonServiceAspect.java

package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

//부가기능 처리: aspect
@Component
@Aspect
public class PersonServiceAspect {
 
	 @Around("execution(* delete(..))")
	    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
	        System.out.println("Before Advice");
	        Object retVal = pjp.proceed();
	        System.out.println("After Advice");
	        System.out.println("retVal:" + retVal);
	        return retVal;
	    }
}

UserServiceAspect.java

package com.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//부가기능 처리: aspect
@Component
@Aspect
public class UserServiceAspect {
  //1. pointcut와 advice 분리
	@Pointcut("execution(* list(..))")// the pointcut expression
	private void anyOldTransfer() {}// the pointcut signature
	
	@Before("anyOldTransfer()")
    public void doAccessCheck(JoinPoint point) {
		System.out.println("point:" + point);
		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.list");
    }
	
	//2. pointcut와 advice 합치기
	@Before("execution(* insert*(..))")
    public void doAccessCheck2(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.BeforeAdvice.insert");
    }
	
	@After("execution(* insert*(..))")
    public void doAccessCheck3(JoinPoint point) {
//		System.out.println("point:" + point);
//		System.out.println("method명:" + point.getSignature().getName());
        System.out.println("UserServiceAspect.AfterAdvice.insert");
    }
}

 

DeptService.java

package com.service;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;

//타켓(target object) ==> 핵심기능
@Service
public class DeptService {
	public List<String> selectList(){
		System.out.println("핵심기능:DeptService.selectList");
		return Arrays.asList("AAA", "BBB");
	
	}
}

EmpService.java

package com.service;
import org.springframework.stereotype.Service;

//타켓(target object) ==> 핵심기능
@Service
public class EmpService {
	public void update() {
		int num = 10/2;
		System.out.println("핵심기능:EmpService.update");
	}
}

PersonService.java

package com.service;
import org.springframework.stereotype.Service;

// 타겟 (target object) ==> 핵심기능
@Service
public class PersonService {
	public String delete() {
		int num = 10/2;
		System.out.println("핵심기능:PersonSerivce.insert");
		return "Hello";
	}
}

UserService.java

package com.service;
import org.springframework.stereotype.Service;

// 타켓(target object) ==> 핵심기능
@Service
public class UserService {

	// pointcut
	public String list() {
		return "UserService.list";
	}
	
	public String insert() {
		return "UserService.insert";
	}
	
	public String insert2() {
		return "UserService.insert2";
	}
}

 

user.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<aop:aspectj-autoproxy />
	<context:component-scan base-package="com.*"></context:component-scan>
</beans>

 

* 출력 화면 

MainClass 출력화면
MainClass2 출력화면
MainClass3 출력화면
MainClass4 출력화면

 

 출처

 

AOP : 

+ 강의 교재

 

+ Recent posts