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

+ Recent posts