Spring AOP ( 코드 주입 )
- 순수 자바코드로는 만들기 힘들다.
- 유지보수를 잘 하기위해, 변화에 민감하지 않게 하기 위해 사용
- 중복되는 코드가 너무 많아지는 것을 방지
- 서비스를 이용하기 위해 거쳐야하는 프로세스 (로그인, 보안체크, DB접속, DB종료, 로그아웃) 코드가 중복.
- 기존의 OOP방식의 한계
- 중복되는 코드 발생, 지저분한 코드 (가독성 안좋아짐), 생산성 저하, 재활용 저하, 변화의 어려움
- 프록시 객체를 생성해서 공통기능 추가. ( 중복되는 코드가 삽입되는 효과 )
- 프록시 : 동작하고 있는 것들을 감시하는 역할
- 특징
- Runtime 기반
- Interface 기반
- 프록시기반
- 알아서 코드를 주입하게 함.
- 핵심코드를 만드는 사람은 모른다!!!! 단일팩의 원칙 준수함
1. AOP 용어
- 조인포인트 : 공통 관심 모듈의 기능이 삽입되어 동작될 수 있는 위치.
- 런타임시 어떤 매개변수가 날아왔는지 실시간으로 기억하는 것
- 포인트컷 : 어떤 클래스의 조인트 포인트를 사용할 것인지(어느 핵심코드를 수행하려고 한다.)
공통코드에선언
- 어드바이스 : 어느 시점에 (공통코드를)삽입시킬지
공통코드에선언
- 위빙, 크로스컷팅 : 포인트 컷에 의해 결정된 조인포인트에 지정된 어드바이스를 삽입하는 과정
- 애스팩트 : 공통 기능을 가진 곳
포인트 컷 + 어드바이스
. 환경설정
- 핵심코드에는 아무것도 안써있음.
1-1. Advice용어
- Before Advice : 핵심코드 호출 전
- After Returning Advice
- After Throwing Advice : 핵심코드에 에러발생시
- After Advice : 핵심코드에 에러가 발생하든 말든
- Around Advice
2. AOP사용하기(xml)
- 라이브러리 필요 : pom.xml에 추가
- AspectJ Weaver
- AspectJ Runtime » 1.9.7
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean class="aop.anno.Programmer" id="programmer" />
<bean class="aop.anno.Designer" id="designer" />
<bean class="aop.anno.MyAspect" id="myAspect"/>
</beans>
3. AOP사용하기(어노테이션)
<aop:aspectj-autoproxy />
- @Aspect 클래스에 선언
- 시점(Advise : 메소드 위에 붙음)
- @Before(“execution()”)
- @After
- @AfterReutrning
- @AfterThrowing
- @Around
- aop-anno.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean class="aop.anno.Programmer" id="programmer" />
<bean class="aop.anno.Designer" id="designer" />
<bean class="aop.anno.MyAspect" id="myAspect"/>
</beans>