스프링 AOP 포인트컷(pointcut),AnnotationMatchingPointcut/포인트컷/Advisor/타겟클래스/위…

스프링 AOP 포인트컷(pointcut),AnnotationMatchingPointcut/포인트컷/Advisor/타겟클래스/위빙(자바동영상/스프링동영상/자바교육/스프링교육/스프링프레임워크/스프링학원/자바학원/자바/JAVA) 


포인트컷(Pointcut) – AnnotationMatchingPointcut

 

n  Application이 어노테이션 기반이라면 커스텀 어노테이션을 사용해서 포인트컷을 지정하고 어드바이스를 특정 어노테이션이 적용된 모든 메소드 또는 타입에 적용하고 싶을 때 사용하는 포인트컷 이다.

n  어노테이션을 사용해서 포인트컷을 정의할 수 있도록 해주는 AnnotationMatchingPointcut 클래스를 제공한다.

 

[pom.xml]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>aoptest</groupId>

  <artifactId>aoptest</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>aoptest</name>

  <description>aoptest</description>

 

  <properties>

             <org.springframework-version>5.2.9.RELEASE</org.springframework-version>

             <aspectj-version>1.9.6</aspectj-version>

       </properties>

 

       <dependencies>

             <dependency>

                    <groupId>org.springframework</groupId>

                    <artifactId>spring-context</artifactId>

                    <version>${org.springframework-version}</version>

             </dependency>

 

             <dependency>

                    <groupId>org.aspectj</groupId>

                    <artifactId>aspectjweaver</artifactId>

                    <version>${aspectj-version}</version>

             </dependency>

 

             <dependency>

                    <groupId>org.aspectj</groupId>

                    <artifactId>aspectjrt</artifactId>

                    <version>${aspectj-version}</version>

             </dependency>

 

       </dependencies>

 

 

</project>

 

[AdviceRequired.java]

package onj.edu.aop6;

 

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

 

// 런타임에 사용할 수 있는 사용자 지정 주석임

@Retention(RetentionPolicy.RUNTIME)

//이 어노테이션을 타입레벨과 메소드레벨에서 적용할 수 있도록

@Target({ElementType.TYPE, ElementType.METHOD}) 

public @interface AdviceRequired {  //interface를 어노테이션으로 선언

}

 

[First.java]

package aoptest;

 

public class First {

           @AdviceRequired  //어드바이스가 적용

           public void hello() {

                      System.out.println("hello1 ... ");

           }

 

           public void sayHello() {

                      System.out.println("sayHello ... ");

           }

}

 

[SimpleAdvice.java]

package aoptest;

 

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

 

public class SimpleAdvice implements MethodInterceptor {

           public Object invoke(MethodInvocation invocation) throws Throwable {

                      System.out.println(invocation.getMethod().getName());

                     

                      Object o = invocation.proceed();

                      System.out.println("... SimpleAdvice의 충고가 적용됨 ...");

                     

                      return o;

           }

}

 

[AnnotationMatchingPointcutTest.java]

package aoptest;

 

public class AnnotationMatchingPointcutTest {

           public static void main(String[] args) {

                      First target = new First();

 

                      //Advisor, 메소드를 호출하면서 지정한 어노테이션이 적용된 모든 메소드에 어드바이스를 적용

                      AnnotationMatchingPointcut pc =                     AnnotationMatchingPointcut.forMethodAnnotation(AdviceRequired.class);

                      Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAdvice());

 

                      //Proxy

                      ProxyFactory pf = new ProxyFactory();

                      pf.setTarget(target);

                      pf.addAdvisor(advisor);

 

                      First f = (First)pf.getProxy();

 

                      f.hello();

                      f.sayHello();

           }

}                                                                

 

[결과]

hello

First hello ...

... SimpleAdvice의 충고가 적용됨 ...

First sayHello ...

 

------------------------------- XML 기반으로 다시 작성

 

[annotation.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"

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

 

       <!-- target -->

       <bean id="first" class="aoptest.First" />

 

       <!-- Advice -->

       <bean id="myAdvice" class="aoptest.SimpleAdvice" />

 

       <!-- pointcut -->

       <bean id="myPointcut"

       class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut"

             factory-method="forMethodAnnotation">

             <constructor-arg value="aoptest.AdviceRequired" />

       </bean>

 

       <bean id="myAdvisor"

              class="org.springframework.aop.support.DefaultPointcutAdvisor">

             <property name="pointcut" ref="myPointcut" />

             <property name="advice" ref="myAdvice" />

       </bean>

 

       <!-- Proxy -->

       <bean id="myProxy"

              class="org.springframework.aop.framework.ProxyFactoryBean">

             <property name="target">

                    <ref bean="first" />

             </property>

             <property name="interceptorNames">

                    <list>

                           <value>myAdvisor</value>

                    </list>

             </property>

       </bean>

 

</beans>

[AnnotationMatchingPointcutTest2.java]

package aoptest;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class AnnotationMatchingPointcutTest2 {

           public static void main(String[] args) {

                      ApplicationContext ctx = new ClassPathXmlApplicationContext("annotation.xml");

                     

                      First f = (First)ctx.getBean("myProxy");

                     

                      //프록시를 통한 호출

                      f.hello();            

                      f.sayHello();

           }

}

 

[실행결과]

hello

First hello ...

... SimpleAdvice의 충고가 적용됨 ...

First sayHello ...

 

 

 

 



#AnnotationMatchingPointcut, #스프링포인트컷, #스프링충고, #포인트컷, #스프링pointcut, #스프링AOP, #스프링advice, #SpringAOP, #스프링DI, #스프링IoC, #SpringDI, #SpringIoC, #자바스프링, #Spring동영상, #Spring강의, #스프링프레임워크, #스프링교육, #스프링학원, #스프링강좌, #스프링강의, #자바학원, #자바, #스프링동영상, #자바동영상, #스프링프레임워크교육, #스프링프레임워크강의, #스프링프레임워크학원

, , , , , , , , , , , , , , , , , , , , , , , ,

Comments