基于API 的spring aop (advisor)

1、advisor是pointcut和advice的桥梁
public interface PointcutAdvisor  extends Advisor。spring中每个内建的pointcut都有相对应的pointcutAdvisor。常用的有两种,NameMatchMethodPointcutAdvisor,RegExphMethodPointcutAdvisor。NameMatchMethodPointcutAdvisor 通过匹配target的id值和mappeName来决定joincut。而RegExphMethodPointcutAdvisor是通过pattern来确定target已经joincut。pattern字符为:
.代表任何单一字符,\代表转义。格式为[包名.类名.]方法名
java代码同上一篇,配置文件如下。
<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">
	<bean id="hello" class="cn.com.gan.spring.aop.ImpHello"></bean>
	<bean id="loggera" class="cn.com.gan.spring.aop.LoggerAfter"></bean>
	<bean id="loggerb" class="cn.com.gan.spring.aop.LoggerBefore"></bean>
<bean id="logger" class="cn.com.gan.spring.aop.loggerAdvisor"></bean>
<bean id="advisor"
		class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedName" value="s*n"></property>
		<property name="advice">
			<ref local="logger" />
		</property>
	</bean>
	<bean id="regadvisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="pattern" value=".*Again"></property>
		<property name="advice" ref="logger"></property>
	</bean>

<bean id="proxyhello"     class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>cn.com.gan.spring.aop.IHello</value>
		</property>
		<property name="target">
			<ref local="hello" />
		</property>
		<property name="interceptorNames">
			<list>
			advisor	
			</list>
		</property>
	</bean>
</beans>

你可能感兴趣的:(spring aop)