这是一个Demo,主要介绍前后增强和正则匹配切面
1、Customer.java
package com.cn.pojo; import com.cn.interf.ICustomer; /** * 目标对象 * 买了一个核心功能ipadMini */ public class Customer implements ICustomer{ @Override public void buyIpadMini() { System.out.println("买了部ipadMini..."); } @Override public void buyIphoneX() { System.out.println("买了部iPhoneX..."); } @Override public void buyIpadAir(String number) { System.out.println("买了部ipadAir..."); } }
2、ICustomer.java
package com.cn.interf; public interface ICustomer { public void buyIpadMini(); public void buyIphoneX(); public void buyIpadAir(String number); }
3、后置增强AfterAdvice.java
package com.cn.advice; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; /** * 增强类 * 需要增强目标对象 */ public class AfterAdvice implements AfterReturningAdvice{ @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("填写问卷,送贴膜..."); } }
4、前置增强BeforAdvice.java
package com.cn.advice; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class BeforAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { String number = arg1[0].toString(); if(number.indexOf("421003")==0){ System.out.println("荆州用户送100元购机券"); } } }
5、Test.java
package com.cn.test; import org.springframework.context.ApplicationContext; span style="color:#0000ff;">import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xtkj.interf.ICustomer;
public class Test {
@org.junit.Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ICustomer customerProxy = (ICustomer)applicationContext.getBean("customerProxy");
customerProxy.buyIpadMini();
}
@org.junit.Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ICustomer customerProxy = (ICustomer)applicationContext.getBean("customerProxy");
customerProxy.buyIpadMini();
customerProxy.buyIphoneX();
}
@org.junit.Test
public void test3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ICustomer customerProxy = (ICustomer)applicationContext.getBean("customerProxy2");
customerProxy.buyIpadMini();
customerProxy.buyIphoneX();
}
@org.junit.Test
public void test4(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ICustomer customerProxy = (ICustomer)applicationContext.getBean("customerProxy3");
customerProxy.buyIpadMini();
customerProxy.buyIphoneX();
customerProxy.buyIpadAir("421003");
}
}
6、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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 目标对象 -->
<bean id="customer" class="com.cn.pojo.Customer"></bean>
<!--后置增强类 -->
<bean id="afterAdvice" class="com.cn.advice.AfterAdvice"></bean>
<!--前置增强-->
<bean id="beforeAdvice" class="com.cn.advice.BeforAdvice"></bean>
<!-- 正则表达式增强 -->
<bean id="regexpAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 匹配切入点 -->
<property name="patterns">
<list>
<value>.*IpadMini.*</value>
</list>
</property>
<!-- 指定匹配后的增强类 -->
<property name="advice" ref="afterAdvice">
</property>
</bean>
<!-- 正则表达式增强 -->
<bean id="regexpAdvice1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 匹配切入点 -->
<property name="patterns">
<list>
<value>.*IpadAir.*</value>
</list>
</property>
<!-- 指定匹配后的增强类 -->
<property name="advice" ref="beforeAdvice">
</property>
</bean>
<!-- 代理对象1 -->
<bean id="customerProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 接口 -->
<property name="interfaces" value="com.cn.interf.ICustomer"></property>
<!-- 目标对象 -->
<property name="target" ref="customer"></property>
<!-- 增强类 -->
<property name="interceptorNames">
<list>
<value>afterAdvice</value>
</list>
</property>
</bean>
<!-- 代理对象2 -->
<bean id="customerProxy2" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 接口 -->
<property name="interfaces" value="com.cn.interf.ICustomer"></property>
<!-- 目标对象 -->
<property name="target" ref="customer"></property>
<!-- 增强类 -->
<property name="interceptorNames">
<list>
<value>regexpAdvice</value>
</list>
</property>
</bean>
<!-- 代理对象3 -->
<bean id="customerProxy3" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 接口 -->
<property name="interfaces" value="com.cn.interf.ICustomer"></property>
<!-- 目标对象 -->
<property name="target" ref="customer"></property>
<!-- 增强类 -->
<property name="interceptorNames">
<list>
<value>regexpAdvice</value>
<value>regexpAdvice1</value>
</list>
</property>
</bean>
</beans>