由于spring整合了aop,所以不用写原生的aop,直接引入坐标,然后相关配置一个,如下
1、导入AOP相关坐标
2、创建目标接口和目标类(内部有切点)
3、创建切面类(内部有增强方法)
4、将目标类和切面类的对象创建权交给spring
5、在applicationContext.xml中配置织入关系
6、测试代码
具体过程:
第一步: 在pom.xml里面导入如下坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--版本必须大于等于4.12-->
<version>4.12</version>
</dependency>
<!--导入aop需要的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
第二步: 在src/main/java/com.huanf目录下新建aop目录
第三步: 在aop目录里面新建Target类,写入如下
//目标类,该类的save方法会被Advice类增强
public class Target implements TargetInterface {
public void save() {
System.out.println("save方法正在启动 -- xml方式实现aop");
}
}
第四步: 在aop目录下新建TargetInterface接口,写入如下
public interface TargetInterface {
public void save();
}
第五步: 在aop目录下新建MyAspect类,写入如下
public class MyAspect {
public void before_met(){
System.out.println("前置增强 -- xml方式实现aop");
}
}
第六步: 在src/main/resources目录下新建applicationContext.xml,里面写入如下
<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">
<!--配置目标对象,我们会在src/test/java/com.huanf.test/AopTest类里面进行测试target-->
<bean id="target" class="com.huanf.aop.Target"></bean>
<!--配置切面对象-->
<bean id="myAspecT" class="com.huanf.aop.MyAspect"></bean>
<!--配置织入,内部要配置: 哪些方法(其实指的就是切点)需要进行哪些增强(其实指定就是前置增强、后置增强、其它增强)-->
<aop:config>
<!--声明切面,即指定上面的id作为切面,不然上面我们写的id="myAspecT"只是一个普通的bean,当声明了之后myAspecT就不再是普通的bean,而是切面-->
<aop:aspect ref="myAspecT">
<!--切面=切点+通知,通知也称为增强。before表示前置增强,method表示哪个方法(在MyAspect类里面的哪个方法)要进行前置增强。
pointcut表示切点,execution里面表示对Target类的save方法增强。下面那行的execution括号里面的叫'切点表达式'-->
<aop:before method="before_met" pointcut="execution(public void com.huanf.aop.Target.save())"/>
<!--上面那行的意思: 当访问save方法时,save方法会进行前置增强,前置增强的代码在MyAspect类的before_met方法里面-->
</aop:aspect>
</aop:config>
<!--把上面那个<aop:config>标签注释掉,那么我们在com.huanf.test.AopTest类里面测试的就是未增强的save方法-->
<!--如果要增强别的方法的话,就把execution括号里面的全限定名改掉即可-->
</beans>
第七步: 测试。创建目录src/test/java,右键java目录,创建com.huanf.test.AopTest类。首先,在pom.xml里面添加如下
<!--引入测试aop的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
然后,在AopTest类里面写入如下
xxxxxxxxxx
//测试AOP动态代理
SpringJUnit4ClassRunner.class) //指定运行引擎是括号里的 (
"classpath:applicationContext.xml") //指定使用的配置文件是src/main/resources/applicationContext.xml (
public class AopTest {
//我们要测com.huanf.aop.Target类里面的save方法(调用save方法需要去com.huanf.aop.TargetInterface里面,才有具体的调用),
//所以要引入(注入)applicationContext.xml里面id为target的bean
private TargetInterface target_itf;
//写一个测试方法
public void test1(){
target_itf.save();
}
}
这节课讲的是从0到1,下节课会解释其中的细节