Spring-AOP 슬라이스 + Aspectj 프레임 사용

AOP 슬라이스 개발에는 Aspectj 프레임워크를 사용합니다.
먼저 두 개의 Jar 패키지를 가져와야 합니다. 1.aspectjrt.jar 2. aspectjweaver-XXX.jar
Aspectj에서 지원하는 5가지 메모:
  • @Before: 사전 통지, 방법이 실행되기 전에 실행
  • @After: 백업 알림, 방법이 실행된 후에 실행
  • @AfterReturning: 알림을 되돌려주고 방법이 결과를 되돌려준 후에 실행
  • @AfterThrowing: 이상 알림, 방법이 이상을 던진 후
  • @Around: 알림을 둘러싸고 실행
  • 다음은 두 가지 방식으로 개발할 것이다.
    형식 1: Spring, 슬라이스:Audience Advice 클래스와 메모 결합
    @Aspect
    public class AudienceAdvice {
        /** * 1> execution() :         * 2> * *..UserService.*(..):       * (1) * :     -->   :public  public void * (2) *. :          * (3) .UserService :      UserService     * (4) .* :       * (5) (..) :          * *   :     UserServiceImpl     ,   AOP   * 3>          ,              , *                 *   : init()      after()         * */
        @Pointcut("execution(* *..UserService.*(..))")
        public void init(){};
    
        /** *    */
        @After("init()")
        public void after(){
            System.out.println("after");
        }
    
    
        /** *    */
        @Before("init()")
        public void before(){
            System.out.println("before");
        }
    
        /** *    */
        @Around("init()")
        public void around(ProceedingJoinPoint jp){
            System.out.println("before");
            try {
                //    
                jp.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            System.out.println("after");
        }
    }

    xml 구성:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    
        <!-- service -->
        <bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl">
        </bean>
    
        <!--         ,            -->
        <bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" />
    
        <!-- aop    ,        -->
        <aop:aspectj-autoproxy />
    </beans>

    UserServiceImpl 클래스
    public class UserServiceImpl implements UserService{
        @Override
        public void save() {
    
        }
    }

    Test 섹션
    public class UserServiceTest {  
        @Test
        public void saveTest(){
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml");
            UserService userDao = 
                    applicationContext.getBean("userServiceImpl", UserService.class);
            userDao.save();
            applicationContext.close();
        }
    }

    형식2: XML 구성Aspectj를 사용하여 Spring 슬라이스와 결합:Audience Advice
    public class AudienceAdvice {
    
        public void init(){};
    
        /** *    */
        public void after(){
            System.out.println("after");
        }
    
    
        /** *    */
        public void before(){
            System.out.println("before");
        }
    
        /** *    */
        public void around(ProceedingJoinPoint jp){
            System.out.println("before");
            try {
                jp.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            System.out.println("after");
        }
    
    }

    XML 구성:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    
    
        <bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl">
        </bean>
    
        <!--      -->
        <bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" />
    
        <!-- XML     -->
        <aop:config>
            <!--    -->
            <aop:aspect ref="advice">
                <!--     -->
                <aop:pointcut expression="execution(* *..UserService.*(..))" id="mypoint"/>
                <!-- method :        (AudienceAdvice )      pointcut-ref :      ,         -->
                <!--      -->
                <!-- <aop:before method="before" pointcut-ref="mypoint"/> -->
                <!--      -->
                <aop:around method="around" pointcut-ref="mypoint"/>
            </aop:aspect>
        </aop:config>
    
    </beans>

    UserServiceImpl 서비스 계층:
    public class UserServiceImpl implements UserService{
    
        public UserServiceImpl() {
            System.out.println("   ");
        }
    
        @Override
        public void save() {
            System.out.println("save");
        }
    
    }

    테스트 섹션:
    public class UserServiceTest {  
        @Test
        public void saveTest(){
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml");
            UserService userDao = 
                    applicationContext.getBean("userServiceImpl", UserService.class);
            userDao.save();
            applicationContext.close();
        }
    }

    좋은 웹페이지 즐겨찾기