2013년 8월 11일 일요일

Spring 2.0 AOP 예제 [Struts, Spring, Spring3.2, iBATIS, Spring MVC, Spring JDBC, Spring Framework,Spring교육, Spring강좌, Spring IoC, Spring DI, Spring DL, 구로디지털 Spring교육, spring MyBatis, Hibernate]

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.     springaop라는 자바프로젝트를 작성 후 Animal.java 인터페이스를 작성
 
[Animal.java]
package springaop;
 
public interface Animal {
    public void walwal();
}
 
3.     Tomdog.java 인터페이스 구현 클래스 제공
 
        [TomDog.java]
package springaop;
 
public class TomDog implements Animal {
    public void walwal() {
        System.out.println("I'm Tomdog...");
    }
}
 
4.     Aspect 작성
 
[AnimalAOP.java]
package springaop;
 
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
 
@Aspect
public class AnimalAOP {
    @Pointcut("execution(public * Animal.walwal(..))")
    public void greeting() {      
    }
   
    @Before("greeting()")
    public void hi() {
        System.out.println("Hi ... ");
    }
   
    @AfterReturning("greeting()")
    public void doBeforeOne() {
        System.out.println("Good Bye ~~~");
    }
}
 
 
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="springaop.TomDog" />
      
       <aop:aspectj-autoproxy />
      
       <!-- this is the actual advice itself -->
       <bean id="animalAOP" class="springaop.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\\springaop\\application-context.xml");
       
        Animal tomdog = (Animal)bean.getBean("tomdog");
        tomdog.walwal();
    }
}

댓글 없음:

댓글 쓰기