>>>>>>>>>>>>> @AspectJ 사용 AOP 예제 1 >>>>>>>>>>>>>>>>>>
------------------------------------------------------------Service.java
package part2;
public interface Service {
public void first();
public void first(String str);
public void second();
public String third();
public void fourth();
public void fifth();
}
------------------------------------------------------------ServiceImp.java
package part2;
public class ServiceImp implements Service {
public ServiceImp() {
System.out.println("생성자");
}
@Override
public void first() {
System.out.println("first()");
}
@Override
public void first(String str) {
System.out.println(str+"first()");
}
@Override
public void second() {
System.out.println("second()");
}
@Override
public String third() {
System.out.println("third()");
return "java";
}
@Override
public void fourth() {
System.out.println("fourth()");
}
@Override
public void fifth() {
int num[]=new int[3];
num[4]=10;
}
}
------------------------------------------------------------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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- annotation을 이용한 AOP설정을 위해 아래와 같이 명시한다 -->
<aop:aspectj-autoproxy/>
<!-- Aspect 빈 객체 선언 -->
<bean name="commAspect" class="part2.AopCommon"/>
<!-- Target 빈 객체 선언 -->
<bean name="svc" class="part2.ServiceImp"/>
</beans>
------------------------------------------------------------AopCommon.java
package part2;
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
public class AopCommon {
@Before("execution(* part2.ServiceImp.first(..))")
public void commMethod1(){
System.out.println("선행 공통메소드()");
}
@After("execution(* part2.ServiceImp.second(..))")
public void commMethod2(){
System.out.println("후행 공통메소드()");
}
@AfterReturning(pointcut="execution(* part2.ServiceImp.third(..))",returning="ret")
public void commMethod3(Object ret){
System.out.println("commMethod3:"+ret.toString());
}
@Around("execution(* part2.ServiceImp.fourth(..))")
public void commMethod4(ProceedingJoinPoint joinpoint){
System.out.println("target 실행전");
try{
joinpoint.proceed();
}catch(Throwable e){
e.printStackTrace();
}
System.out.println("target 실행후");
}
@AfterThrowing(pointcut="execution(* part2.ServiceImp.fifth(..))",throwing="thr")
public void commMethod5(ArrayIndexOutOfBoundsException thr){
System.out.println("commMethod5:"+thr.toString());
}
}
------------------------------------------------------------ServiceMain.java
package part2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ServiceMain {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/part2/aop.xml");
Service svc=(Service)context.getBean("svc");
//svc.first("korea");
//svc.first();
//svc.second();
//svc.third();
//svc.fourth();
try{
svc.fifth();
}catch(ArrayIndexOutOfBoundsException ex){}
}
}
'..열심히 공부하세.. > MVC' 카테고리의 다른 글
| [24] Spring JDBC 예제 (0) | 2012.07.13 |
|---|---|
| [23] @AspectJ 사용 AOP 예제 2 (0) | 2012.07.12 |
| [21] @AspectJ의 Pointcut 살펴보기 (0) | 2012.07.12 |
| [20] 스프링 Advice 예제 2 (0) | 2012.07.11 |
| [19] 스프링 Advice 예제 1 (0) | 2012.07.11 |