2017년 1월 11일 수요일

[자바교육/스프링교육/스프링프레임워크/스프링부트학원추천_탑크리에듀]Spring 2.0 AOP Simple 예제(XML 파일을 이용한 aspect 설정)

Spring 2.0 AOP Simple 예제(XML 파일을 이용한 aspect 설정) 

1.        필요한 파일을 클래스 패스에 추가한다. 
        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 
정보: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@18558d2]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bfc8e0 
2008. 5. 6 오전 1:47:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bfc8e0: defining beans [tomdog,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,greeting,animalAOP]; root of factory hierarchy 
Hi~ Dog... 
I'm Tomdog... 
Good Bye Dog... 

댓글 없음:

댓글 쓰기