一、MethodBeforeAdvice方式
step1、编写接口类和接口实现类
public interface UserDao {
public void addUser();
public void updateUser();
}
import com.woniuxy.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("UserDaoImpl---addUser");
}
@Override
public void updateUser() {
System.out.println("UserDaoImpl---updateUser");
}
}
step2、编写一个类实现MethodBeforeAdvice
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("BeforeAdvice---beforeAdvice");
}
}
step3、xxx.xml文件中配置本文是bean.xml
1.配置业务
2.配置通知
3.配置代理(target:目标业务类 interface:配置接口 inerceptorNames:配置拦截器 配置通知中的的id名)
<?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.xsd">
<bean id="userDaoImpl" class="com.woniuxy.dao.impl.UserDaoImpl"/>
<bean id="beforeAdvice" class="com.woniuxy.advice.BeforeAdvice"/>
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="userDaoImpl"/>
</property>
<property name="interfaces">
<list>
<value>com.woniuxy.dao.UserDao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
</list>
</property>
</bean>
</beans>
4、编写测试类
@Test
public void test01(){
UserDao userDao = (UserDao)ac.getBean("proxyFactoryBean");
userDao.addUser();
}
AfterAdice使用:定义类实现AfterReturningAdvice接口即可
AroundAdvice使用:定义类实现MethodInterceptor接口即可
ThrowAdvice使用:定义类实现ThrowsAdvice接口即可,注意:异常类
step5、测试结果
二、NameMatchMethodPointcutAdvisor方式
step1、step2、step3同上
step4、配置bean.xml文件
<bean id="carImpl" class="com.woniuxy.aop2.CarImpl"/>
<bean id="carAdvice" class="com.woniuxy.aop.CarAdvice"/>
<bean id="nameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<!--mappedName配要执行的方法-->
<property name="mappedName" value="brand"/>
<property name="advice" ref="carAdvice"/>
</bean>
<bean id="carproxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置接口-->
<property name="interfaces">
<list>
<value>com.woniuxy.aop2.Car</value>
</list>
</property>
<!--配置目标对象-->
<property name="target">
<ref bean="carImpl"/>
</property>
<!--配置拦截器-->
<property name="interceptorNames">
<list>
<!-- <value>carAdvice</value>-->
<value>nameMatchMethodPointcutAdvisor</value>
</list>
</property>
当需要配置多个方法时
<?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.xsd">
<bean id="userDaoImpl" class="com.woniuxy.dao.impl.UserDaoImpl"/>
<bean id="beforeAdvice" class="com.woniuxy.advice.BeforeAdvice"/>
<bean id="nameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedNames">
<list>
<value>addUser</value>
<value>updateUser</value>
</list>
</property>
<property name="advice" ref="beforeAdvice"/>
</bean>
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="userDaoImpl"/>
</property>
<property name="interfaces">
<list>
<value>com.woniuxy.dao.UserDao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>nameMatchMethodPointcutAdvisor</value>
</list>
</property>
</bean>
</beans>
step5、编写测试类
@Test
public void shouldAnswerWithTrue()
{
Car carProxy = (Car)ac.getBean("carproxy");
carProxy.brand("红旗车");
carProxy.color("yellow");
}
step6、测试结果
advice 是通知
advisor 是顾问顾问(Advisor)
通知Advice是Spring提供的一种切面(Aspect)。但其功能过于简单,只能将切面织入到目标类的所有目标方法中,无法完成将切面织入到指定目标方法中。
顾问Advisor是Spring提供的另一种切面。其可以完成更为复杂的切面织入功能,能选择性的将增强切面中的部分方法。
PointcutAdvisor是顾问的一种,可以指定具体的切入点。顾问将通知进行了包装,会根据不同的通知类型,在不同的时间点,将切面织入到不同的切入点。
PointcutAdvisor接口有两个较为常用的实现类:
*:NameMatchMethodPointcutAdvisor 名称匹配方法切入点顾问
*: RegexpMethodPointcutAdvisor 正则表达式方法切入点顾问