SpringAOP中的PointcutAdvisor

  • Post author:
  • Post category:其他





RegexpMethodPointcutAdvisor和NameMatchMethodPointcutAdvisor,它们都在org.springframework.aop.support包中,都可以过滤要拦截的方法,配置方法也大致相同,其中一个最主要的区别:

RegexpMethodPointcutAdvisor:需要加上完整的类名和方法名,例如:com.package.classname.methodname或com.*.methodname或.*methodname。

NameMatchMethodPointcutAdvisor:只需要方法名,不用加类名,例如:*methodname。


注意:需要spring-aop-2.5.1.jar、aopalliance-1.0.jar



1、RegexpMethodPointcutAdvisor:


接口:

package com.yourcompany.spring.spring1;

public interface Sleepable {
	public void sleep();  
}


实现:

package com.yourcompany.spring.spring1;

public class Human implements Sleepable {
	public void sleep() {
		System.out.println("人类睡觉...");  
	}
}


消息:

package com.yourcompany.spring.spring1;

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice, AfterReturningAdvice {
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {  
		System.out.println("睡觉前要脱衣服!");  
	}  
	public void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {  
		System.out.println("起床后要穿衣服!");  
	}  
}


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

	<bean id="human" class="com.yourcompany.spring.spring1.Human"/>
	<bean id="advice" class="com.yourcompany.spring.spring1.SleepHelper"/>
	
	<bean id="sleepAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice" ref="advice" />
		<property name="pattern" value=".*sleep" />
	</bean>

	<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="human"/>
		<property name="interceptorNames">
			<list>
				<value>sleepAdvisor</value>
			</list>
		</property>
	</bean>
</beans>


程序主入口:

package com.yourcompany.spring.spring1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	         Sleepable sleeper = (Sleepable)context.getBean("humanProxy");
	         sleeper.sleep();
	}
}



2、NameMatchMethodPointcutAdvisor:



接口:

package com.yourcompany.spring.spring2;

public interface Sleepable {
	public void sleep();  
}



实现:

package com.yourcompany.spring.spring2;

public class Human implements Sleepable {
	public void sleep() {
		System.out.println("人类睡觉...");  
	}
}



消息:

package com.yourcompany.spring.spring1;

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice, AfterReturningAdvice {
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {  
		System.out.println("睡觉前要脱衣服!");  
	}  
	public void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {  
		System.out.println("起床后要穿衣服!");  
	}  
}



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

	<bean id="advice" class="com.yourcompany.spring.spring2.SleepHelper"/>
	
	<bean id="sleepAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedName" value="*sleep" />
		<property name="advice" ref="advice" />
	</bean>

	<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target">
			<bean id="human" class="com.yourcompany.spring.spring2.Human"></bean>
		</property>
		<property name="interceptorNames">
			<list>
				<value>sleepAdvisor</value>
			</list>
		</property>
	</bean>
</beans>



程序主入口:

package com.yourcompany.spring.spring1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	         Sleepable sleeper = (Sleepable)context.getBean("humanProxy");
	         sleeper.sleep();
	}
}




版权声明:本文为lzghxjt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。