티스토리 뷰


AOP - 관점 지향 프로그래밍


어떤 기능을 구현하기 위한 세부기능들을 분리하고 각 세부기능중에 

특정 기능을 위해 필요한 세부기능을 핵심관심사항,

여러 기능 구현에 필요한 세부기능들을 공통 관심사항 이라 정의한다.


세부 기능들을 핵심관심사항과 공통관심사항으로 분리해서

공통관심사항으로 이뤄진 프레인에 핵심관심사항을 조립해서 기능구현을 달성!!


<구조 알아 보기>





AOP의 용어


용어

설명 

Aspect 

공통 관심사에 대한 추상적인 명칭. 예를 들어 로깅이나 보안, 트랜잭션과 같은 기능 자체에 대한 용어 

Advice 

실제로 기능을 구현한 객체 

Join points 

공통 관심사를 적용할 수 있는 대상. Spring AOP에서는 각 객체의 메소드가 이에 해당 

Pointcuts 

여러 메소드 중 실제 Advice가 적용될 대상 메소드 

target 

대상 메소드를 가지는 객체 

Proxy 

 Advice가 적용되었을 때 만들어지는 객체

Introduction 

target에는 없는 새로운 메소드나 인스턴스 변수를 추가하는 기능 

Weaving 

Advice와 target이 결합되어서 프록시 객체를 만드는 과정 



Advice의 종류


 타입

기능 

Before Advice 

target의 메소드 호출 전에 적용 

After returning 

target의 호출 이후에 적용 

After throwing 

target의 예외 발생 후 적용 

After 

target의 메소드 호출 후 예외의 발생에 관계없이 적용 

Around 

target의 메소드 호출 이전과 이후 모두 적용(가장 광범위하게 사용됨) 




프록시, 프록시 패턴 구현

 -> Proxy는 일반적인 의미에서는 직접 호출하는 방식이 아니라 간접적인 호출을 하는 것을 의미합니다.




[ AOP ]

012345678910111213141516171819





[ 어노테이션을 이용한 AOP ]

 myAspect.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(public * test.*.doSomething())")
    public void mypt(){    }
    
    @Before("mypt()")
    public void before(JoinPoint jp){
        System.out.println(jp.getSignature());//AOP정보출력
        System.out.println(jp.getTarget());//핵심관심사항객체 리턴
        System.out.println("문을 열고 집에 들어간다");
    }
 
    @After("mypt()")
    public void after(JoinPoint jp){
        System.out.println("문을 열고 집을 나온다");
    }
 
    @AfterReturning(pointcut="mypt()", returning="msg")
    public void after_returning(JoinPoint jp, String msg){
        System.out.println(msg + "//불을 끄고 잠을 잔다");
    }
 
    @AfterThrowing(pointcut="mypt()",throwing="th")
    public void after_throwing(JoinPoint jp,Throwable th) {
        System.out.println(th + "// 119에 신고한다.");
    }
}
cs

applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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:context="http://www.springframework.org/schema/context"
    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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
 
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <context:component-scan base-package="test"></context:component-scan>
</beans>
 
cs


Boy.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package test;
import java.util.Random;
 
import org.springframework.stereotype.Component;
 
@Component
public class Boy implements IPerson{
 
    @Override
    public String doSomething() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("컴퓨터로 롤을 한다.");
        
        if (new Random().nextBoolean())
            throw new Exception("화재발생!!!!!");
        
        return "I am a Boy";
    }
}
 
cs


Test.java

1
2
3
4
5
6
7
8
9
10
    ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
        
        IPerson i = context.getBean("boy",IPerson.class);
        
        try {
            i.doSomething();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
cs


출력

String test.Boy.doSomething()

test.Boy@702657cc

문을 열고 집에 들어간다

컴퓨터로 롤을 한다.

문을 열고 집을 나온다

I am a Boy//불을 끄고 잠을 잔다


그 외에는 위 슬라이드쇼 참고

반응형

'언어 > SPRING' 카테고리의 다른 글

[SPRING] MVC 공부  (0) 2016.11.16
[SPRING] Mybatis-Transaction Management  (0) 2016.11.15
[SPRING] mapper의 sql 태그  (0) 2016.11.11
[SPRING] 동적 SQL  (0) 2016.11.11
[SPRING] sqlSessionFactoryManager - static으로 관리  (0) 2016.11.10
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함