Annotation : class, 메소드, 생성자, 변수 위에 선언
- annotation명 [("element")] (element="value", element="value....)
1. 검증 : 구문을 compile 시점에 검증
2. 설정 : XML 설정
Aspect 어노테이션을 이용한 AOP(Aspect Oriented Programming)
- Aspect 어노테이션을 이용하여 Aspect 클래스에 직접 Advice 및 Pointcut 등을 설정
- 설정 파일에 <aop:aspectj-autoproxy/>를 추가 해야함
- Aspect Class를 <bean>으로 등록
- Annotation(어노테이션)
* @Aspect : Aspect 클래스 선언
* @Before("pointcut")
* @AfterReturning(pointcut="", returning="")
* @AfterThrowing(pointcut="", throwing="")
* @After("pointcut")
* @Around("pointcut")
- Around를 제외한 나머지 메소드들은 첫 argument로 JoinPoint를 가질 수 있다.
- Around 메소드는 argument로 ProceedingJoinPoint를 가질 수 있다.
CoreClass.java
package aop.core;
public class CoreClass {
public void businessMethod(int i) {
if(i<0) {
throw new RuntimeException("i가 0보다 작다");
}
System.out.println("Business메소드가 실행 되었습니다.");
}
}
AdviceClass.java
package aop.common;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //aspect 클래스 선언
public class AdviceClass {
@Before("within(aop.core.CoreClass)") //pointcut 입력
public void beforeLogger() {
System.out.println("Advice Class.beforeLogger()------------");
}
@AfterThrowing(pointcut="within(aop.core.CoreClass)", throwing="ex")
public void afterThrowLogger(Throwable ex) {
System.out.println("AdviceClass.afterThrowLogger()------------");
System.out.println(ex.getMessage());
}
@AfterReturning(pointcut="within(aop.core.CoreClass)", returning="ret")
public void afterReturnLogger(Object ret) {
System.out.println("AdviceClass.afterReturnLogger()------------");
System.out.println(ret);
}
@After("within(aop.core.CoreClass)")
public void afterLogger() {
System.out.println("AdviceClass.afterLogger()----------");
}
@Around("within(aop.core.CoreClass)")
public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable {
System.out.println("AdviceClass.aroundLogger()----------");
return jp.proceed();
}
}
aop.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy/>
<!-- 타겟 -->
<bean name="core" class="aop.core.CoreClass"/>
<!-- Advice -->
<bean name="advice" class="aop.common.AdviceClass"/>
</beans>
TestMain.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import aop.core.CoreClass;
public class TestMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/config/aop.xml");
CoreClass cls = (CoreClass) ctx.getBean("core");
cls.businessMethod(-20);
}
}
실행결과
'Wanna be a Programmer > Spring Framework' 카테고리의 다른 글
Spring MVC 구현(Controller, ModelAndView) (0) | 2012.06.28 |
---|---|
Spring MVC 구현(Dispatcher, HandlerMapping) (0) | 2012.06.27 |
Spring MVC (0) | 2012.06.27 |
스프링 프레임워크 - AOP(Aspect Oriented Programming) (0) | 2012.06.25 |
스프링 프레임워크 및 DI(Dependency Injection)패턴 (0) | 2012.06.22 |