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.xsdhttp://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.xsdhttp://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;@Testpublic 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包下的任意类方法或者任意子包下的任意类方法

spring-基于xml的aop开发-快速入门相关推荐

  1. Java spring基于XML的aop配置实现

    1.依赖包 2.文件结构 3.接口类ISomeService package com.buckwheats.test;public interface ISomeService {public voi ...

  2. spring中基于XML的AOP配置步骤

    spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...

  3. Spring框架----Spring的基于XML的AOP的实现

    导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-co ...

  4. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

  5. spring基于XML的AOP-编写必要的代码

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  6. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  7. ​HealthKit开发快速入门教程之HealthKit数据的操作

    ​HealthKit开发快速入门教程之HealthKit数据的操作 数据的表示 在HealthKit中,数据是最核心的元素.通过分析数据,人们可以看到相关的健康信息.例如,通过统计步数数据,人们可以知 ...

  8. OUYA游戏开发快速入门教程第1章了解OUYA及其设备

    OUYA游戏开发快速入门教程第1章了解OUYA及其设备 OUYA是基于Andorid系统的游戏主机.围绕OUYA游戏机,已经形成一个完整的生态圈.在国外,OUYA已经成为知名的游戏平台.本章会站在玩家 ...

  9. OUYA游戏开发快速入门教程

     OUYA游戏开发快速入门教程 试读地址:http://pan.baidu.com/s/1o63a3W2 本教程是国内唯一OUYA游戏开发教程.本教程基于Unity全面讲解OUYA游戏开发方式.内容包 ...

最新文章

  1. Java运行时动态加载类之ClassLoader
  2. preg_match_all用法
  3. java环境变量的配置步骤,实现原理分析
  4. 【水】uva10037 过桥
  5. element ui表单校验prop的链式写法----源码分析
  6. power of love
  7. Editor GUI 的 Gamma Correction
  8. php中session销毁,PHP中session变量的销毁
  9. 电商项目——初识电商——第一章——上篇
  10. 创意分析及优化技巧 — 百度推广
  11. 忆我的大学老师----(一)
  12. php 系统管理和监控软件
  13. robots文件的优化
  14. [英语语法]词法之动词
  15. P3287 [SCOI2014]方伯伯的玉米田
  16. Taro下拉刷新,上拉加载更多
  17. MFC隐藏窗口时解决窗口闪烁问题
  18. linux电脑关机命令是什么问题,linux下正常关机之命令详解 -电脑资料
  19. Express搭建服务器
  20. ONVIF 事件--能力集

热门文章

  1. HADOOP都升级到2.5啦~~~
  2. 集体智慧编程 - 构建价格模型
  3. oracle xmltype使用,oracle的xmltype基本使用有哪些呢?
  4. java中方法不调用会执行_java[新手]类里的方法没有调用为什么实现了?
  5. ant构建项目迁移到gradle_自动化构建、自动化部署发布一览
  6. linux 播放器系统,在Linux上安装和使用开源视频播放器MPlayer
  7. gunicorn 配置日志
  8. 程序设计囚犯与灯泡 C语言代码,100个囚犯和灯泡的那些事儿(下)
  9. java 内存溢出 内存泄露_java 内存泄露、内存溢出、内存不足
  10. 深度学习和目标检测系列教程 15-300:在 Python 中使用 OpenCV 执行 YOLOv3 对象检测