스프링프레임워크, AOP 포인트컷(pointcut),JdkRegexpMethodPointcut이론실습/포인트컷/Advisor/타겟클래스/위빙(자바동영상/스프링동영상/자바교육/스프링교육/SpringFramework/스프링프레임워크/스프링학원/자바학원/자바/JAVA)
ojc.asia/bbs/board.php?bo_table=LecSpring&wr_id=871
ojc.asia

포인트컷(Pointcut) - JdkRegexpMethodPointcut
n 메소드 이름을 정확히 알지 못하는 경우, 이름대신 패턴을 사용하여 포인트 컷을 생성할 수 있는데 이때 사용되는 포인트 컷이 JdkRegexpMethodPointcut 이다.
n java.util.regex 패키지를 기반으로하는 정규식 포인트 컷.
[First.java]
package onj.edu.aop4;
public class First {
public void hello1() {
System.out.println("hello1 ... ");
}
public void hello2() {
System.out.println("hello2 ... ");
}
public void sayHello() {
System.out.println("sayHello ... ");
}
}
[RegExpExam.java]
public class RegExpExam {
public static void main(String[] args) {
First target = new First();
//Advisor
JdkRegexpMethodPointcut pc = new JdkRegexpMethodPointcut();
//스프링은 비교할 때 패키지.클래스.메소드명을 사용한다.
// hello로 시작하는 메소드면 OK
pc.setPattern("aoptest*.*.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();
}
}
[결과]
hello1
hello1 ...
... SimpleAdvice의 충고가 적용됨 ...
hello2
hello2 ...
... SimpleAdvice의 충고가 적용됨 ...
sayHello ...
Pointcut은 Advice가 적용될 메소드를 골라야 하는데 Method Signature에 일치하는 패턴을 지정하는 방법을 주로 사용하는 포인트 컷이다.
org.springframework.aop.support.JdkregexpMethodPointcut JDK1.4이상 사용
<bean id=“smallMartPointcut” class=“org.springframework.aop.support.JdkregexpMethodPointcut”>
<!-- 어떤클래스든 관계없고 get으로 시작하는 메소드 à
<property name=“pattern” value=“*.get*”/>
</bean>
Pointcut을 정의했으면 이를 Advice에 결합시킬 수 있다.
<bean id=“smallMartAdvisor” class=“org.springframework.aop.support.DefaultPointcutAdvisor”>
<property name=“advice” ref=“myAdvice”/>
<property name=“pointcut” ref= “myPointcut”/>
</bean>
앞에서 pointcut을 만들고 advice와 결합을 시켰는데 하나의 빈에 pointcut과 advice를 동시에 정의할 수 있는 특별한 Advisor가 있다.
(org.springframework.aop.support.RegexpMethodPointcutAdvisor)
<bean id=“smallMartAdvisor” class=“org.springframework.aop.support. RegexpMethodPointcutAdvisor”>
<property name=“advice” ref=“beforeLogging”/>
<property name=“pattern” value=“*.get*”/>
</bean>
------------ 실습2. ProxyFactoryBean을 이용한 XML형식
[regexp.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="aoptest2.First" />
<!-- 충고 클래스 -->
<bean id="myAdvice" class="aoptest2.SimpleAdvice" />
<!-- 포인트컷 -->
<bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<!-- 어떤클래스든 관계없고 get으로 시작하는 메소드 -->
<property name="patterns">
<list>
<value>aoptest*.*</value>
<!-- <value>aoptest*.*.hello*.</value> -->
</list>
</property>
</bean>
<bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="myPointcut" />
<property name="advice" ref="myAdvice" />
</bean>
<!-- 하나의 빈에 pointcut과 advice를 동시에 정의할 수 있는 특별한 Advisor -->
<!-- <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="myAdvice"/> <property name="pattern" value="aoptest.*"/>
대소문자 구분함 </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>
[RegExpExam.java]
package aoptest;
import org.springframework.context.support.GenericXmlApplicationContext;
public class RegExpExam {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("regexp.xml");
ctx.refresh();
//XML에서 만든 Proxy빈 로딩
First f = (First)ctx.getBean("myProxy");
//프록시를 통한 호출
f.hello1();
f.hello2();
f.sayHello();
}
}
-- aoptest패키지의 모든것
aoptest*.*
-- aoptest패키지의 hello로 시작하는 메소드
aoptest*.*.hello*.
#JdkRegexpMethodPointcut, #스프링포인트컷, #스프링충고, #포인트컷, #스프링pointcut, #스프링AOP, #스프링advice, #SpringAOP, #스프링DI, #스프링IoC, #SpringDI, #SpringIoC, #자바스프링, #Spring동영상, #Spring강의, #스프링프레임워크, #스프링교육, #스프링학원, #스프링강좌, #스프링강의, #자바학원, #자바, #스프링동영상, #자바동영상, #스프링프레임워크교육, #스프링프레임워크강의, #스프링프레임워크학원
댓글 없음:
댓글 쓰기