Spring 2.0 AOP Simple 예제
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. 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();
}
}
댓글 없음:
댓글 쓰기