annotation방식으로 aop 설정하기


1. 디펜더시 세팅

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.1.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.1.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>4.1.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.8.6</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.8.6</version>

</dependency>

</dependencies> 


2. annotation방식에서도 일단 등장인물들을 빈으로 등록부터

namespace : context, aop체크

<context:component-scan base-package="*"/>

+ <aop:aspectj-autoproxy></aop:aspectj-autoproxy>



@Component

@Aspect  <<<<< (pointcut + advice)의 정보를 입력


public class MyAspect{         

@Pointcut("excution(public * test.* . abcd()")

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.getMessage() + "  : 가라 몬스터볼!!");

}



@Component

public class Pikachu implements IPokemon{


@Override

public String fighting() throws Exception {

// TODO Auto-generated method stub

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

if(new Random().nextBoolean())

throw new Exception("피카츄 전광석화!!");

return "pika!!";

}

}



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

Day62 Spring MVC  (0) 2016.06.16
Day61  (0) 2016.06.15
Day59 Spring-AOP  (0) 2016.06.13
Day58 AOP (Aspect Oriented Programming)  (0) 2016.06.10
Day57 MyBatis_Spring  (0) 2016.06.09

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

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



핵심관심사항 클래스 구현

공통관심사항 클래스 구현

위 친구들 빈으로 등록

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

공통관심사항의 각 함수를 적용시점 정보 포함에 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

+ Recent posts