스프링프레임워크, AOP 포인트컷(pointcut),AspectJExpressionPointcut이론실습/포인트컷/Advisor/타겟클래스/위빙(자바동영상/스프링동영상/자바교육/스프링교육/SpringFramework/스프링프레임워크/스프링학원/자바학원/자바/JAVA)
http://ojc.asia/bbs/board.php?bo_table=LecSpring&wr_id=872
포인트컷(Pointcut) - AspectJExpressionPointcut
n AspectJ 포인트컷 표현식 언어를 통해 포인트 컷을 선언할 수도 있다. JDK 정규식 보다 많이 사용되며 스프링은 AspectJExpressionPointcut 클래스를 제공하며 실행을 위해 aspectjrt.jar, aspectjweaver.jar 두 라이브러리 파일이 필요하다.
n execution(* onj.spring.aop..*.*(..)) : 리턴형 관계없고 onj.spring.aop 패키지 및 하위 패키지에 있는 모든 클래스, 인자값이 0개 이상인 메서드가 포인트 컷
n execution(* delete*(*)) : 메서드 이름이 delete으로 시작하는 인자 값이 1개인 메서드가 포인트 컷
n execution(* delete*(*,*)) : 메서드 이름이 delete로 시작하는 인자 값이 2개인 메서드가 포인트 컷
n execution(* onj*(Integer, ..)) : 메서드 이름이 onj로 시작하고 첫번째 인자 값의 타입이 Integer, 1개 이상의 매개변수를 갖는 메서드가 포인트 컷
n within(onj.spring.aop.*) : onj.spring.aop 패키지 내의 모든 메소드가 포인트 컷
n within(onj.spring.aop..*) : onj.spring.aop패키지 및 하위 패키지의 모든 메소드가 포인트 컷
n within(onj.spring.aop.SmallMartImpl) : onj.spring.aop패키지의 SmallMartImpl클래스의 모듬 메소드가 포인트 컷
n bean(oraclejava*) : 이름이 oraclejava로 시작되는 모든 빈의 메소드가 포인트 컷
n bean(*dataSource) || bean(*DataSource) : 빈 이름이 “dataSource” 나 “DataSource” 으로 끝나는 모든 빈의 메소드가 포인트 컷
n !bean(oraclejavacommunity) : onjoraclejavacommunity빈을 제외한 모든 빈의 메소드가 포인트컷
n this(onj.aop.SmallMartInterface) : 현재 실행중인 인스턴스가 SmallMartInterface 이름의 빈과 타입이 같은 경우 포인트컷, SmallMart인터페이스를 구현했다면 모든 메소드가 포인트컷
n target(onj.aop.SmallMartInterface) : 타겟 오브젝트가 SmallMartInterface를 구현했다면 모든 메소드가 포인트컷
[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>
[First.java]
package aoptest;
public class First {
public void hello1() {
System.out.println("hello1 ... ");
}
public void hello2() {
System.out.println("hello2 ... ");
}
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;
}
}
[AspectJExam.java]
package aoptest;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
public class AspectJExam {
public static void main(String[] args) {
First target = new First();
//Advisor
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
//인자, 반환 관계없이 hello로 시작하는...
pc.setExpression("execution(* hello*(..))");
Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAdvice());
//Proxy
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
First f = (First)pf.getProxy();
f.hello1();
f.hello2();
f.sayHello();
}
}
XML 기반 사용예
[aspectj.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">
<!-- 타겟 틀래스 -->
<bean id="first" class="aoptest.First" />
<!-- 충고 클래스 -->
<bean id="myAdvice" class="aoptest.SimpleAdvice" />
<!-- Advisor -->
<bean id="myAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
<!-- 어떤클래스든 관계없고 hello로 시작하는 메소드 -->
<property name="expression" value="execution(* aoptest..hello*(..))"/>
<property name="advice" ref="myAdvice" />
</bean>
<!-- 프록시 -->
<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>
[재작성된 main 클래스]
package aoptest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectJExam2 {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aspectj.xml");
//XML에서 만든 Proxy빈 로딩
First f = (First)ctx.getBean("myProxy");
//프록시를 통한 호출
f.hello1();
f.hello2();
f.sayHello();
}
}
#AspectJExpressionPointcut, #스프링포인트컷, #스프링충고, #포인트컷, #스프링pointcut, #스프링AOP, #스프링advice, #SpringAOP, #스프링DI, #스프링IoC, #SpringDI, #SpringIoC, #자바스프링, #Spring동영상, #Spring강의, #스프링프레임워크, #스프링교육, #스프링학원, #스프링강좌, #스프링강의, #자바학원, #자바, #스프링동영상, #자바동영상, #스프링프레임워크교육, #스프링프레임워크강의, #스프링프레임워크학원