SpringAOP之动态正则切入点

配置文件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 name="login" class="com.mooza.test.Login"></bean>
<!-- 动态切入点 -->
<bean name="dynaPoint" class="com.mooza.test.DynaPointCut"></bean>
<!-- 通知类 -->
<bean name="beforeAdvice" class="com.mooza.test.LoginBeoreAdvice"></bean>
<!-- 静态切入点
<bean name="sadvice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref bean="beforeAdvice"/>
</property>
<property name="pointcut">
<ref bean="dynaPoint"/>
</property>
</bean>
-->
<!-- 正则切入点 -->
<bean name="zeadvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>com/.mooza/.test/.ILogin/.log.*</value>
</list>
</property>
<property name="advice">
<ref bean="beforeAdvice"/>
</property>
</bean>
<!-- 代理类 -->
<bean name="baProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.mooza.test.ILogin</value>
</property>
<property name="target">
<ref bean="login"/>
</property>
<property name="interceptorNames">
<value>zeadvice</value>
</property>
</bean>
</beans>


测试类 TestTestBeforeAdvice

 

 


package com.mooza.test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestBeforeAdvice {
public static void main(String[] args){
TestBeforeAdvice tba = new TestBeforeAdvice();
tba.TestVoid();
}

public void TestVoid(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//Resource res = new ClassPathResource("applicationContext.xml");
//XmlBeanFactory bean = new XmlBeanFactory(res);
ILogin login = (ILogin)ac.getBean("baProxy");
System.out.println("~~~~~~~~~~~~~~~~~~~~~");
login.login();
System.out.println("~~~~~~~~~~~~~~~~~~~~~");
login.logout();
System.out.println("~~~~~~~~~~~~~~~~~~~~~");
login.update();
System.out.println("~~~~~~~~~~~~~~~~~~~~~");
}
}

接口 ILogin :

 

 


package com.mooza.test;
public interface ILogin {
public void login();

public void update();

public void logout();
}

继承接口的实现类 login :

 



package com.mooza.test;
public class Login implements ILogin {
public void login() {
// TODO Auto-generated method stub
System.out.println("我要登门拜访啦~~~(实现类本身的方法)");
}
public void logout() {
// TODO Auto-generated method stub
System.out.println("我先走了,下次再来~~~(实现类本身的方法)");
}
public void update() {
// TODO Auto-generated method stub
System.out.println("我可以帮你改改衣服什么的~~~(实现类本身的方法)");
}
}

动态切入点类 DynaPointCut :

 

 


package com.mooza.test;
import java.lang.reflect.Method;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;
public class DynaPointCut extends DynamicMethodMatcherPointcut {
public boolean matches(Method method, Class cls) {
if ("login".equals(method.getName())) {
System.out.println("SimpleDynamicPointcut:静态方法匹配器正在尝试匹配方法:"
+ method.getName());
return true;
} else
return false;
}
public boolean matches(Method arg0, Class arg1, Object[] arg2) {
// TODO Auto-generated method stub
return true;
}
public ClassFilter getClassFilter() {
return new ClassFilter() {
public boolean matches(Class cls) {
if (cls == Login.class) {
System.out.println("SimpleDynamicPointcut:切入点类匹配,正在匹配"
+ cls.getName() + "类!");
return true;
} else
return false;
}
};
}
}


通知类 LoginBeoreAdvice :

 

 


package com.mooza.test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class LoginBeoreAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] o1, Object o2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("我是大前锋,冲锋打第一炮!!(这是before通知)");
}
}

你可能感兴趣的:(SpringAOP之动态正则切入点)