OOP언어를 좀 더 OOP 답게 도와 줌

1.API 준비

<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-aop</artifactId>
				<!-- 버전 생략 -->
</dependency>

2. AOP 사용

main method가 있는 클래스 선언부에 
@EnableAspectJAutoProxy 선언(생략 가능)

@SpringBootApplication
//@EnableAspectJAutoProxy
public class StudyBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(StudyBootApplication.class, args);
	}

}

3. AOP 주요 용어

1. advice
	 - 핵심 로직이 진행 될 때 실행해야할 공통 로직

2. point-cut
	 - 핵심 로직

3. join point
	 - 공통로직을(advice) 핵심로직(point-cut)에 언제 실행 할 것이냐 결정
	 1) Before          : 핵심로직 실행 전
	 2) AfterReturning  : 핵심로직 실행 후, 정상적인 종료일 때만
	 3) AfterThowing    : 핵심로직 실행 후, 예외가 발생했을 때만
	 4) After           : AfterReturning + AfterThowing
	 5) Around          : Before + After

4. Aspect
	- Advice 를 언제(join-point) 누구에게(point-cut) 실행 하는 것을 설정	

5. Weaving
	- Advice를 point-cut에 적용하는 행위
6. proxy 

4. Execution

execution(접근지정자(생략가능) 리턴타입 패키지명.클래스명.메서드명(매개변수타입) )

- execution(public *.*)         : 모든 public 메서드
- execution( * set*())          : 모든 set으로 시작하는 메서드들 중 매개변수가 없는것
- execution( * set*(*))         : 모든 set으로 시작하는 메서드들 중 매개변수가 한개 선언한것
- execution( * set*(*,*))       : 모든 set으로 시작하는 메서드들 중 매개변수가 두개 선언한것
- execution( * set*(Integer, *)): 모든 set으로 시작하는 메서드들 중 매개변수가 두개 선언한것, 첫번째는 Integer
- execution( * set*(..))        : 모든 set으로 시작하는 메서드들 중 매개변수가 0개 이상 상관 X
- execution( * com.iu.home.service.Service.*()) : 
- execution( * com.iu.home.board.*.*()) :  
- execution( * set*() || execution(* set*(*))) : 
- execution( * set*() && execution(* set*(*))) : 

5. 예시

@Component
@Slf4j
@Aspect
public class Card {
	
	
	@Before("execution(* com.iu.home.aop.test.Transeport.airplane())")
	public void before() {
		log.info(" ------------Before --------- ");
	}
	@After("execution(* com.iu.home.aop.test.Transeport.get*())")
	public void after() {
		log.info(" ------------After --------- ");
	}
		
	
	@Around("execution(* com.iu.home.aop.test.Transeport.take*())")
	public Object cardCheck(ProceedingJoinPoint joinPoint) throws Throwable {
		log.info("--- 삐빅 승차 입니다 -----");
		Object obj = joinPoint.proceed();
		log.info("--- 삐빅 하차 입니다 -----");
		return obj;
		
	}

}

Transaction 처리

Spring에서는 AOP를 이용해서 Transction 처리
Legacy에서는 XML파일, Annotation에 설정

1. 설정

1. main method가 있는 클래스 선언부
	 - @EnableTransactionManagement 선언
	 - 생략 해도 가능 

2. Service class에 Transaction 처리
	 @Transactional(rollbackFor=Exception.class)

	 a. 메서드 선언부
	 b. 클래스 선언부
	 c. Interface 선언부 
 
	 - 메서드는 public 이어야 함

3. application.properties
	 - Service에 @Transactional 사용시
	 - Service class가 부모 없이 단독 사용시 에러 방지
	 spring.aop.proxy-target-class=true