spring-基于xml的aop开发-快速入门
2024-09-05 22:51:32
1.导入aop的相关坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
如果使用spring原生的aop进行织入,则导入spring-context就够了(因为spirng-context包含spring-aop),但是spring官方推荐使用第三方小框架aspectjweaver进行织入(spring不排斥任何优秀的框架)
2.创建目标接口和目标类
public interface TargetInterface {
public void save();
}
public class Target implements TargetInterface{
public void save() {
System.out.println("save running....");
}
}
3.创建切面类
public class MyAspect {
public void before(){
System.out.println("前置增强。。。");
}
}
4.将目标和切面类的对象创建权交给spring
注意:这一步只是把目标对象类和切面对象类交给spring管理,现在spring还只认为你这只是普通类,所有引入了第五步操作。
<?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="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
</beans>
5.在applicationContext.xml中配置织入关系
aop:aspect标签配置切面,那切面是谁呢,使用ref引入切面的唯一标识id,此时这一步配置之后,第二个bean标签引入的类就真正成为了切面类
aop:before:通知的类型,method引入切面中的增强方法;ponintcut:切点表达式(最后讲)
<?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="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<aop:before method="before" pointcut="execution(public void com.hao.aop.Target.save())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
6.测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
@Qualifier("target")
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}
结果:
execution
public:修饰符(可以省略)
void 返回值类型
com.hao.aop:包名
Target:类名
save(参数):方法名
execution(void com.hao.aop.Target.*(…)) :表示Target类下所有无返回值的方法都会被增强
execution( * com.hao.aop.. (. .):表示任意类的任意方法都可以
execution(* com.hao.aop. . *. *(. .):表示aop包下的任意类方法或者任意子包下的任意类方法
最新文章
- Redis_redis分布式锁-SETNX
- 【转】JavaScript中的原型和继承
- php加密解密功能类
- CodeForces 559C Gerald and Giant Chess
- 包装BufferedReader的readLine()输出行号
- CTG
- [Java基础]Java通配符
- js设计模式--鸭子类型
- 软工UML学习札记
- 使用 VMAccess 扩展程序重置 Linux 虚拟机的登录凭据
- ASP.NET中的路径(path) 详解
- 事件绑定之.bind()
- jquery.post用法补充(type设置问题)
- STM32F207 两路ADC连续转换及GPIO模拟I2C给MT9V024初始化参数
- 使用Redux DevTools浏览器插件调试redux
- 基本排序算法[python实现]
- CentOS6.6 双网卡双网关配置
- feginclient和hystrix的配置
- 2017 年 PHP 程序员未来路在何方?
- js(jQuery)tips