3、第五个创建方法,配置.xml文件
第五个创建方法和第三个第四个区别还是在与.xml的配置文件,主要区别的是标签上的区别,第五个方法新增了
<aop:aspect ref="my">
<aop:before method="before" pointcut-ref="mpc"></aop:before>
<aop:after method="after" pointcut-ref="mpc"></aop:after>
</aop:aspect>
意思就是调用拦截对象中的指定方法,并且使用pointcut-ref和被代理对象关联起来,完整的配置文件是
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="us" class="com.gaojian.aop5.UserServiceImpl"></bean>
<bean id="my" class="com.gaojian.aop5.MyAspect"></bean>
<aop:config proxy-target-class="true">
<aop:aspect ref="my">
<aop:pointcut id="mpc" expression="execution(* com.gaojian.aop5.*.*(..))"/>
<!-- 选择拦截器对象中的方法,并且引用到mpc,也就是被代理对象上-->
<aop:before method="before" pointcut-ref="mpc"></aop:before>
<aop:after method="after" pointcut-ref="mpc"></aop:after>
</aop:aspect>
</aop:config>
</beans>
测试类为
package com.gaojian.aop5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop3 {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("aop5.xml");
IUserService us = ac.getBean("us", IUserService.class);
us.deleteUser(1);
us.getAllUser();
us.updateUser(new Object());
us.saveUser(new Object());
}
}
执行结果是:

image.png