삼성SDS_멀티캠퍼스/Spring Frame Work

45일 차 Spring(스프링 프레임워크) AOP 설정을 xml로 하기

박성우기 2015. 11. 11. 16:46
반응형

package aop5;


//점심을 먹으러 스카이 라운지로 간다

//메뉴를 본다

//그들은 밥을 먹지 않는다

//밥을 먹는다


//예외상황 :  죽을 것 같으면 밥을 먹는다

public class Girl implements PersonImpl {

public void doSomething() {

System.out.println("그들은 밥을 거의 먹지 않는다");

}

}













package aop5;


//점심을 먹으러 스카이 라운지로 간다

//메뉴를 본다

//A또는 B코스 둘 중 하나가 괜찮아 보인다

//밥을 먹는다


//예외상황 :  죽을 것 같으면 밥을 먹는다

public class Man implements PersonImpl {

public void doSomething() {

System.out.println("어지간 하면 가리지 않는다");

}

}












package aop5;


public interface PersonImpl {

public void doSomething();

}












package aop5;


public class Aspect {



public void before() {

System.out.println("점심을 먹으러 스카이 라운지로 간다");

System.out.println("메뉴를 본다");

}


public void afterReturn() {

System.out.println("그들은 밥을 먹지 않는다");

}


public void afterThrow() {

System.out.println("죽을 것 같으면 밥을 먹는다");

}


public void after() {

System.out.println("밥을 먹는다");

}


}











package aop5;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;


public class Test {


public static void main(String[] args) {


ApplicationContext context = new FileSystemXmlApplicationContext("src/aop5/applicationContext.xml");

PersonImpl man = context.getBean("man", PersonImpl.class);

man.doSomething();


}

}













<?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.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">


<bean id="man" class="aop5.Man"></bean>

<bean id="girl" class="aop5.Girl"></bean>

<bean id="aspect" class="aop5.Aspect"></bean>



<aop:config>

<aop:pointcut expression="execution (public void aop5.*.doSomething())"

id="pt" />



<aop:aspect ref="aspect">

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

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

<aop:after-returning method="afterReturn"

pointcut-ref="pt" />

<aop:after-throwing method="afterThrow"

pointcut-ref="pt" />

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

</aop:aspect>



</aop:config>




</beans>








결과창








반응형