Spring 专题
专题目录
您的位置:java > Spring专题 > Spring AOP实例(Pointcut,Advisor)
Spring AOP实例(Pointcut,Advisor)
作者:--    发布时间:2019-11-20
在上一个spring aop通知的例子,一个类的整个方法被自动拦截。但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因。它允许你通过它的方法名来拦截方法。另外,一个“切入点”必须具有“advisor' 相关联。
在spring aop中,有三个非常专业术语- advices, h3cut , advisor,把它在非官方的方式...
  • advice – 指示之前或方法执行后采取的行动。
  • h3cut – 指明哪些方法应该拦截,通过方法的名称或正则表达式模式。
  • advisor – 分组"通知"和”切入点“成为一个单元,并把它传递到代理工厂对象。

再次回顾上一个 spring aop通知的例子

file : customerservice.java

package com.h3.customer.services;

public class customerservice
{
	private string name;
	private string url;

	public void setname(string name) {
		this.name = name;
	}

	public void seturl(string url) {
		this.url = url;
	}

	public void printname(){
		system.out.println("customer name : " + this.name);
	}
	
	public void printurl(){
		system.out.println("customer website : " + this.url);
	}
	
	public void printthrowexception(){
		throw new illegalargumentexception();
	}
	
}

file : applicationcontext.xml

<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-2.5.xsd">

	<bean id="customerservice" class="com.h3.customer.services.customerservice">
		<property name="name" value="yong mook kim" />
		<property name="url" value="http://www.h3.com" />
	</bean>

	<bean id="hijackaroundmethodbeanadvice" class="com.h3.aop.hijackaroundmethod" />

	<bean id="customerserviceproxy" 
                class="org.springframework.aop.framework.proxyfactorybean">

		<property name="target" ref="customerservice" />

		<property name="interceptornames">
			<list>
				<value>hijackaroundmethodbeanadvice</value>
			</list>
		</property>
	</bean>
</beans>

file : hijackaroundmethod.java

package com.h3.aop;

import java.util.arrays;
import org.aopalliance.intercept.methodinterceptor;
import org.aopalliance.intercept.methodinvocation;

public class hijackaroundmethod implements methodinterceptor {
	@override
	public object invoke(methodinvocation methodinvocation) throws throwable {

		system.out.println("method name : "
				+ methodinvocation.getmethod().getname());
		system.out.println("method arguments : "
				+ arrays.tostring(methodinvocation.getarguments()));

		system.out.println("hijackaroundmethod : before method hijacked!");

		try {
			object result = methodinvocation.proceed();
			system.out.println("hijackaroundmethod : before after hijacked!");

			return result;

		} catch (illegalargumentexception e) {

			system.out.println("hijackaroundmethod : throw exception hijacked!");
			throw e;
		}
	}
}

执行它

package com.h3.common;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

import com.h3.customer.services.customerservice;

public class app {
	public static void main(string[] args) {
		applicationcontext appcontext = new classpathxmlapplicationcontext(
				new string[] { "applicationcontext.xml" });

		customerservice cust = (customerservice) appcontext
				.getbean("customerserviceproxy");

		system.out.println("*************************");
		cust.printname();
		system.out.println("*************************");
		cust.printurl();
		system.out.println("*************************");
		try {
			cust.printthrowexception();
		} catch (exception e) {
		}
	}
}

输出

*************************
method name : printname
method arguments : []
hijackaroundmethod : before method hijacked!
customer name : h3
hijackaroundmethod : before after hijacked!
*************************
method name : printurl
method arguments : []
hijackaroundmethod : before method hijacked!
customer website : http://www.h3.com
hijackaroundmethod : before after hijacked!
*************************
method name : printthrowexception
method arguments : []
hijackaroundmethod : before method hijacked!
hijackaroundmethod : throw exception hijacked!
客户服务类的全部方法被截获。后来,我们展示如何使用“切入点”只拦截printname()方法。

切入点的例子

可以通过以下两种方式相匹配的方法:
  1. 名称匹配
  2. 正则表达式匹配

1.切入点 - 名称匹配的例子

通过“切入点”和“advisor”拦截printname()方法。创建namematchmethodh3cut切入点bean,并提出要在“mappedname”属性值来拦截方法名。
<bean id="customerh3cut"
        class="org.springframework.aop.support.namematchmethodh3cut">
		<property name="mappedname" value="printname" />
	</bean>
创建 defaulth3cutadvisor 通知 bean,通知和切入点相关联。
<bean id="customeradvisor"
		class="org.springframework.aop.support.defaulth3cutadvisor">
		<property name="pointcut" ref="customerh3cut" />
		<property name="advice" ref="hijackaroundmethodbeanadvice" />
	</bean>
更换代理“interceptornames”到“customeradvisor”(它是“hijackaroundmethodbeanadvice”)。
<bean id="customerserviceproxy"
		class="org.springframework.aop.framework.proxyfactorybean">

		<property name="target" ref="customerservice" />
		
		<property name="interceptornames">
			<list>
				<value>customeradvisor</value>
			</list>
		</property>
	</bean>
全部bean配置文件
<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-2.5.xsd">

	<bean id="customerservice" class="com.h3.customer.services.customerservice">
		<property name="name" value="h3" />
		<property name="url" value="http://www.h3.com" />
	</bean>

	<bean id="hijackaroundmethodbeanadvice" class="com.h3.aop.hijackaroundmethod" />

	<bean id="customerserviceproxy" 
                class="org.springframework.aop.framework.proxyfactorybean">

		<property name="target" ref="customerservice" />

		<property name="interceptornames">
			<list>
				<value>customeradvisor</value>
			</list>
		</property>
	</bean>

	<bean id="customerh3cut" 
                class="org.springframework.aop.support.namematchmethodh3cut">
		<property name="mappedname" value="printname" />
	</bean>

	<bean id="customeradvisor" 
                 class="org.springframework.aop.support.defaulth3cutadvisor">
		<property name="pointcut" ref="customerh3cut" />
		<property name="advice" ref="hijackaroundmethodbeanadvice" />
	</bean>

</beans>
再次运行,输出
*************************
method name : printname
method arguments : []
hijackaroundmethod : before method hijacked!
customer name : h3
hijackaroundmethod : before after hijacked!
*************************
customer website : http://www.h3.com
*************************
现在,只拦截 printname()方法。
h3cutadvisor
spring提供了h3cutadvisor类来保存工作声明advisor和切入点到不同的bean,可以使用 namematchmethodh3cutadvisor 两者结合成一个 bean。
<bean id="customeradvisor"
		class="org.springframework.aop.support.namematchmethodh3cutadvisor">
	
		<property name="mappedname" value="printname" />
		<property name="advice" ref="hijackaroundmethodbeanadvice" />
	
	</bean>

2.切入点 - 正则表达式的例子

也可以通过使用正则表达式匹配切入点方法的名称  – regexpmethodh3cutadvisor.

<bean id="customeradvisor"
		class="org.springframework.aop.support.regexpmethodh3cutadvisor">
		<property name="patterns">
			<list>
				<value>.*url.*</value>
			</list>
		</property>

		<property name="advice" ref="hijackaroundmethodbeanadvice" />
	</bean> 

现在,它拦截方法名称中有“url”的方法。在实践中,可以用它来管理dao层,声明“.*dao.*” 拦截所有的dao类来支持事务。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册