인터페이스(런타임 위빙이 되기위해 필요)부터 정의해두고 

얘를 구현하는 타겟(핵심관심사항구현)클래스 구현



핵심관심사항 클래스 구현

공통관심사항 클래스 구현

위 친구들 빈으로 등록

핵심관심사항 구현 함수를 포인트컷으로 지정

공통관심사항의 각 함수를 적용시점 정보 포함에 advice지정


helloworld: 쉽게 시작하는 프로그래밍

tryhelloworld.co.kr/
기초부터 차근차근, 직접 코드를 작성해 보세요. helloworld에서는 누구나 프로그래머가 될 수 있습니다.


<aop:config>

<aop:aspect ref="myAspect">

<aop:pointcut expression="execution(public String *.doSomething())" id="myPt"/>

<!-- <aop:before method="before" pointcut-ref="myPt"/> -->

<!-- <aop:after-returning method="after_returning" pointcut-ref="myPt" returning="msg"/> -->

<!-- <aop:after-throwing method="after_throwing" pointcut-ref="myPt" throwing="th"/> -->

<!-- <aop:after method="after" pointcut-ref="myPt"/>  -->

<aop:around method="around" pointcut-ref="myPt"/>

</aop:aspect>

</aop:config>


public class MyAspect {

public void before(JoinPoint jp){

System.out.println(jp.getSignature());//AOP정보 출력

System.out.println(jp.getTarget());//핵심관심사항객체 리턴

System.out.println("");

}

public void after(JoinPoint jp){

System.out.println("");

}

public void after_returning(JoinPoint jp, String msg){

System.out.println(msg + "");

}

public void after_throwing(JoinPoint jp, Throwable th){

System.out.println(th.getMessage() + " .");

}

public void around(ProceedingJoinPoint jp){//AOP에 대한 정보를 가지고 있으면서 핵심관심사항을 수동으로 호출 시킬 수 있음

//around advice -> 핵심관심사항 전후 모두를 포함하는 advice 

//around advice에서는 직접 핵심관심사항을 호출함

try{

before(jp);

String msg = (String) jp.proceed();

after_returning(jp, msg);

}catch(Throwable e){

after_throwing(jp, e);

}

after(jp);

}

}



' IOT 기반 응용 SW과정 > Web Programing' 카테고리의 다른 글

Day61  (0) 2016.06.15
Day60 Spring-AOP  (0) 2016.06.14
Day58 AOP (Aspect Oriented Programming)  (0) 2016.06.10
Day57 MyBatis_Spring  (0) 2016.06.09
Day56 MyBatis-Spring  (0) 2016.06.08

WordPress.com: 무료 웹사이트 또는 블로그 만들기

https://ko.wordpress.com/
WordPress.com에서 무료 웹사이트나 블로그를 쉽게 만들어 보세요. 사용자 정의가 가능하며 모바일을 지원하는 수백 개의 디자인과 테마가 무료로 제공됩니다.



AOP (Aspect Oriented Programming) - 관점지향프로그래밍

어떤 기능을 구현하기 위한 세부기능들을 분리하고, 각 세부기능 중에 특정기능을 위해 필요한 세부기능을 핵심관심사항

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

세부기능들을 핵심관심사항과 공통관심사항으로 분리해서 공통관심사항으로 이뤄진 프레임에 핵심관심사항을 조립해서 기능구현을 달성



프록시패턴 (Proxy pattern)

일반적으로 프록시는 다른 무언가와 이어지는 인터페이스의 역할을 하는 클래스이다. 프록시는 어떠한 것(이를테면 네트워크 연결, 메모리 안의 커다란 객체, 파일, 또 복제할 수 없거나 수요가 많은 리소스)과도 인터페이스의 역할을 수행할 수 있다. --위키


프록시객체 : 내가 수행할 기능을 대신 수행해주는 객체

오리지널객체 : 내가 수행할 기능을 구현하는 클래스 객체


프록시패턴 구현 : :

오리지널 객체가 수행할 기능에 대한 껍데기를 인터페이스로 정의하고 구현

오리지널 객체가 구현한 인터페이스를 프록시객체도 구현

프록시 객체의 구현함수에서 오리지널객체의 구현함수를 호출


package pokemon;

public interface IPokemon {

public void fighting();     핵심관심사항

}

 public class Pikachu implements IPokemon{

@Override

public void fighting() {  핵심관심사항 

System.out.println("피카츄 백만볼트!!");

}

}

 public class PokemonProxy implements IPokemon {                        

IPokemon pokemon;


public void setPokemon(IPokemon pokemon) {

this.pokemon = pokemon;

}

@Override

public void fighting()

System.out.println("야생의 동휘몬을 조우하였다!!");  //before aspect 공통관심사항(핵심관심사항전에 수행돼야함)                                 

       try {

pokemon.fighting();   핵심관심사항 

System.out.println("효과는 탁월하였다!!");  //after-returning 공통관심사항(핵심이 잘 수행됐을때)                

} catch (Exception e) {

System.out.println("효과는 없었다..");    //after-throwing 공통관심사항(핵심 수행 중 에러발생)                     

}

System.out.println("가라 몬스터볼!!");          //after-returning 공통관심사항(핵심이 잘 수행됐을때)

}

}

 public class PokeTest {

public static void main(String[] args) {

IPokemon pokemon = new Pikachu();

PokemonProxy pokeProxy = new PokemonProxy();

pokeProxy.setPokemon(pokemon);

pokeProxy.fighting();

}

}

 

AOP 용어


• Target – 핵심사항(Core) 가 구현된 객체

• JoinPoint – 공통관심사항이 적용 될 수 있는 지점(ex:메소드 호출시, 객체생성시 등)

• Pointcut – JoinPoint 중 실제 공통사항이 적용될 대상을 지정.


• Advice

– 공통관심사항(Cross-Cutting) 구현 코드 + 적용시점.

– 적용 시점 : 핵심로직 실행 전, 후, 정상 종료 후, 비정상 종료 후, 전/후가 있다.


• Aspect – Advice + Pointcut

• Weaving – Proxy를 생성하는 것. (컴파일 시점, Class Loading 시점, Weaving 런타임 시점)




' IOT 기반 응용 SW과정 > Web Programing' 카테고리의 다른 글

Day60 Spring-AOP  (0) 2016.06.14
Day59 Spring-AOP  (0) 2016.06.13
Day57 MyBatis_Spring  (0) 2016.06.09
Day56 MyBatis-Spring  (0) 2016.06.08
Day55  (0) 2016.06.03

+ Recent posts