2013년 8월 11일 일요일

[spring Framework AOP, DI, DL]Spring 2.0 AOP Simple 예제(XML 파일을 이용한 aspect 설정)

1.     필요한 파일을 클래스 패스에 추가한다.



오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의)  

        C:\java\spring-framework-2.5.3\dist\spring.jar
        common-logging.jar파일을 다운로드(http://commons.apache.org/logging/)
commons-logging-1.1.1.jar
파일을 클래스패스에 추가
        AOP를 위해 C:\java\spring-framework-2.5.3\\lib\aspectJ 아래 aspectjrt.jar aspectjrt.jar 파일을 클래스 패스에 추가
 
2.     Springaop2라는 자바프로젝트를 작성 후 Animal.java 인터페이스를 작성
 
[Animal.java]
package springaop2;
 
public interface Animal {
    public void walwal();
}
 
3.     Tomdog.java 인터페이스 구현 클래스 제공
 
        [TomDog.java]
package springaop2;
 
public class TomDog implements Animal {
    public void walwal() {
        System.out.println("I'm Tomdog...");
    }
}
 
4.     Aspect 작성
 
[AnimalAOP.java]
package springaop2;
 
public class AnimalAOP {
    public void beforeWalwal() {
        System.out.println("Hi~ Dog...");
    }
   
    public void afterWalwal() {
        System.out.println("Good Bye Dog...");
    }
}
 
5.     Application-Context.xml 작성
 
[application-context.xml]
<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.xsd">
 
         <!-- this is the object that will be proxied by Spring's AOP infrastructure -->
       <bean id="tomdog" class="springaop2.TomDog" />
      
       <!--  <aop:aspectj-autoproxy /> -->
      
       <aop:config>
          <aop:aspect ref="animalAOP">
              <aop:pointcut id="greeting"  __EXPRESSION__="execution(public * springaop2.Animal.walwal(..))" />
              <aop:before pointcut-ref="greeting" method="beforeWalwal" />
              <aop:after-returning pointcut-ref="greeting" method="afterWalwal" />
          </aop:aspect>
       </aop:config>
      
      
       <!-- this is the actual advice itself -->
       <bean id="animalAOP" class="springaop2.AnimalAOP" />
 
 
</beans>
 
6.     TestClient 작성
 
[TestClient.java]
package springaop;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;;
 
public class TestClient {
    public static void main(String[] args) {
        BeanFactory bean = new FileSystemXmlApplicationContext("C:\\java\\Tomcat 5.5\\webapps\\SpringAOP\\springaop2\\application-context.xml");
       
        Animal tomdog = (Animal)bean.getBean("tomdog");
        tomdog.walwal();
    }
}
 
 
7.     실행결과
 
[결과]
2008. 5. 6 오전 1:47:46 org.springframework.context.support.AbstractApplicationContext prepareRefresh
정보: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@18558d2: display name [org.springframework.context.support.FileSystemXmlApplicationContext@18558d2]; startup date [Tue May 06 01:47:46 KST 2008]; root of context hierarchy
2008. 5. 6 오전 1:47:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
정보: Loading XML bean definitions from file [C:\java\Tomcat 5.5\webapps\SpringAOP\springaop2\application-context.xml]

2008. 5. 6 오전 1:47:47 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

댓글 없음:

댓글 쓰기