Purview接口
class="java">package aop; public interface Purview { void checkLogin(); }
Purview接口的实现类PurviesImpl.java
package aop; public class PurviewImpl implements Purview { public void checkLogin() { System.out.println("This is checkLogin method!"); } }
拦截器类PurviewAdvice.java
?
package aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class PurviewAdvice implements MethodBeforeAdvice { public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("This is before method!"); } }
测试类Test.java
package aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { // TODO 自动生成方法存根 ApplicationContext ctx = new ClassPathXmlApplicationContext( "aop/applicationContext.xml"); Purview purview = (Purview) ctx.getBean("purviewImpl"); purview.checkLogin(); } }
配置文件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" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-autowire="autodetect"> <bean id="purviewImpl" class="aop.PurviewImpl"></bean> <bean id="purviewAdvice" class="aop.PurviewAdvice"></bean> <bean id="purviewAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="purviewAdvice" /> </property> <property name="patterns"> <list> <value>.*checkLogin.*</value> </list> </property> </bean> <bean id="autoproxyaop" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <value>purviewImpl</value> </property> <property name="interceptorNames"> <list> <value>purviewAdvisor</value> </list> </property> </bean> </beans>
运行结果:
This is before method! This is checkLogin method!