본문 바로가기

프로그래머/JAVA

Spring AOP

 횡단적 관심사(공통사항, 핵심사항)을 분리 구현하는 기술

 

1. Weavin - 분리한 관심사를 모듈에 삽입하는 것

2. Advice - 관점으로서 분리되고 실행시 모듈에 위빙된 구체적인 처리

3. JoinPoint - 실행시 처리 플로우에서 Advice를 위빙하는 포인트, 즉 메서드 호출이나 예외발생 지점

4. Pointcut - 하나 또는 복수개의 joinpoint를 하나로 묶은 것

5. Advisor - Advice와 Pointcut을 하나로 묶어 다루는 것

 

 

 

 

package sample1;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

 

public class HelloApp {

 

        public static void main(String[] args) {

               Resource resource = new ClassPathResource("applicationContext.xml");

               BeanFactory factory = new XmlBeanFactory(resource);

               MessageBean bean = (MessageBean)factory.getBean("proxy");

               bean.sayHello();

        }

}

 

 

package sample1;

 

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

import org.springframework.util.StopWatch;

 

public class LoggingAdvice implements MethodInterceptor {

        @Override

        public Object invoke(MethodInvocation invocation) throws Throwable {

               String methodName = invocation.getMethod().getName();

               StopWatch sw = new StopWatch();

              

               sw.start(methodName);

              

               System.out.println("[LOG] METHOD:" + methodName + " is calling.");

               Object rtnObj = invocation.proceed();

              

               sw.stop();

               System.out.println("[LOG] METHOD:" + methodName + " was called.");

               System.out.println("[LOG] 처리시간:" + sw.getTotalTimeMillis() / 1000 + "");

              

               return rtnObj;

        }

}

 

 

package sample1;

 

public interface MessageBean {

        void sayHello();

}

 

 

 

package sample1;

 

public class MessageBeanImpl implements MessageBean {

        private String name;

       

        public void setName(String name) {

               this.name = name;

        }

 

        @Override

        public void sayHello() {

               try {

                       Thread.sleep(5000);

               } catch (Exception e) {

                       e.printStackTrace();

               } finally {

                       System.out.println("Hello" + name + "!");

               }

        }

}

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://www.springframework.org/schema/beans

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

       

        <bean id = "targetbean" class = "sample1.MessageBeanImpl">

               <property name="name">

                       <value>Spring</value>

               </property>

        </bean>

       

        <bean id = "loggingAdvice" class = "sample1.LoggingAdvice" />

       

        <bean id = "proxy" class = "org.springframework.aop.framework.ProxyFactoryBean">

               <property name="target">

                       <ref local = "targetbean" />

               </property>

               <property name="interceptorNames">

                       <list>

                              <value>helloAdvisor</value>

                       </list>

               </property>

        </bean>

       

        <bean id = "helloAdvisor" class = "org.springframework.aop.support.DefaultPointcutAdvisor">

               <property name="advice">

                       <ref local = "loggingAdvice" />

               </property>

               <property name="pointcut">

                       <bean class = "org.springframework.aop.support.JdkRegexpMethodPointcut">

                              <property name="pattern">

                                      <value>.*sayHello*.</value>

                              </property>

                       </bean>

               </property>

        </bean>

</beans>

 

 

'프로그래머 > JAVA' 카테고리의 다른 글

Spring DI(예제)  (0) 2015.03.04
Spring AOP(AspectJ)  (0) 2015.03.03
JDBC(Java DataBase Connectivity)  (0) 2013.03.29
[JSP] JavaBean  (0) 2013.03.29
[JSP] 세션 리스너  (0) 2013.03.28