@markdown
# AOP annotaion 용어, 패턴매칭, 자동주입
<br/>
## AOP Advice 용어
____
- 어떤 메소드를 어느 시점에 실행할지 결정한다.
- `Before Advice` : 대상 객체의 메서드 호출 전에 공통 기능 실행
- `After Returning Advice` : 대상 객체의 메서드가 `Exception` 없이 실행된 이후에 공통 기능 실행
- `After Throwing Adivce` : 대상 객체의 메서드를 실행하는 도중 `Exception`이 발생한 경우에 공통기능 실행
- `After Advice` : 대상 객체의 메서드를 실행하는 도중에 `Exception`이 발생했는지의 여부에 상관없이 메서드 실행 후 공통 기능을 실행
- `Around Advice` : 대상 객체의 메서드 실행 전, 후 또는 `Exception` 발생 시점에 공통 기능을 실행
### Point Cut 지정자
____
- execution 실행결과 [접근지정자] 리턴타입 [패키지.클래스이름]메소드()
- `메소드(..)` : 0개 이상
- `메소드(*)` : 1개 이상 사용 가능
##AspectJ에서의 사용되는 표현식
____
### 와일드 카드
- `*` : 모든 문자
- `..` : 0개 이상의 문자
- `+` : 주어진 타입의 서브 클래스나 인터페이스
### 패턴매칭
- `set* (..)` : set으로 시작하는 모든 메서드
- `* main(..)` : 리턴타입 상관없이 이름이 main 인 모든 메서드
### Type
- `Java.io.*` : Java.io 밑에 있는 모든 클래스 지칭
- `ch4.aop..*` : ch4.aop 패키지 및 그 하위 모든 패키지의 클래스 지칭
- `Number+ +` : 서브 타입 지칭, Number 타입으로 표현되는 모든 하위 클래스
- `!(Number+)` : Number 타입으로 표현되지 않는 모든 클래스
### Modifier 설정
- public static void main(..) 매개변수에 상관없이 접근제한자가 `public`인 `main` 함수
- !private * * (..) 접근제한자가 `private` 가 아닌 모든 메서드
- * main(..) 접근제한자 상관하지 않는 형식
## 스키마 기반 AOP annotation
____
- `<aop:before>` : @Before("execution(public * anno.*Controller.*(..))")
- `<aop:afterreturning>` : @AfterReturning(pointcut="execution(public *anno.*Controller.*(..))", returning="retVal")
- `<aop:afterthrowing>` : @AfterThrowing(pointcut="execution(public * anno.*Controller.*(..))", throwing="ex")
- `<aop:after>` : @After("execution(public * anno.*Controller.*(..))")
- `<aop:around>` : @Around("execution(public * anno.*Controller.*(..))")
### Aspect 관리
- `advice` : 타겟 메소드를 실행시키는 시점(before, after)
- `pointcut` : 타겟 메소드 지정
- `advisor` : 타겟 메소드가 실행되는 어떤 시점에서 공통 메소드가 실행될지 관리하는 단위
```
<aop:config>
<!-- AOP 적용할 클래스 설정 -->
<!-- Advisor -->
<aop:aspect ref="myAspect">
<!-- 타겟 메소드를 설정하기 전에 before 메소드를 실행시키고 싶을때 -->
<aop:before method="before" pointcut="execution(* runSomething())"/>
<aop:after method="after" pointcut="execution(* runSomething())"/>
</aop:aspect>
</aop:config>
```
### aop.pointcut 생성 후 재사용성 높이기
```
<aop:config>
<aop:pointcut expression="execution(* runSomething())" id="myPointCut01"/>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut-ref="myPointCut01"/>
<aop:after method="after" pointcut-ref="myPointCut01"/>
</aop:aspect>
</aop:config>
```
<br/>
### Around annotation 사용한 AOP
____
- 특정 `After`, `Before`에 넣지 않고 원하는 곳에 공통 부분을 가져와 주입 할 수 있다.
```
<aop:aspectj-autoproxy/>
<bean id="myAspect" class="AOP06.MyAspect"></bean>
<bean id="class1" class="AOP06.Class1"></bean>
<bean id="class2" class="AOP06.Class2"></bean>
@Around("execution(* AOP06.*.runSomething())")
public void around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("Before 메시지");
joinPoint.proceed();
System.out.println("After 메시지");
}
```
'웹 프로그래밍' 카테고리의 다른 글
MyBatis - 설치, Mapping 태그 (0) | 2017.07.11 |
---|---|
Spring - RequestMapping, Form 데이터 처리, ResponseBody 사용법 (0) | 2017.07.10 |
Spring - DI 자동 주입, AOP (0) | 2017.07.06 |
Spring - 핵심 개념(IoC, DI) (0) | 2017.07.05 |
Spring - Maven 설치 및 Eclipse 환경설정 하기 (0) | 2017.07.04 |