示例:

  aop,即面向切面编程,面向切面编程的目标就是分离关注点。

比如:小明(一位孩子)想吃苹果,首先得要有苹果,其次才能吃。那么妈妈负责去买水果,孩子负责吃,这样,既分离了关注点,也减低了代码的复杂程度

示例:

孩子类:

@Component
public class Child { public void eat(){
System.out.println("小孩子吃苹果");
} }

妈妈类(切面类):

public class Mom {

    public void buy(){//前置通知
System.out.println("买水果");
} public void clear(){//后置通知
System.out.println("收拾果核");
} }

aop2.xml配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--目标对象 孩子类-->
<bean id="child" class="com.oukele.learning.aop0.Child"/> <!--切面类-->
<bean id="mom" class="com.oukele.learning.aop0.Mom"/>
<!--面向切面编程-->
<aop:config>
<!--定义切面-->
<aop:aspect ref="mom">
<!--定义切点-->
<aop:pointcut id="action" expression="execution(* *.*(..))"/>
<!-- 声明前置通知 (在切点方法被执行前调用)-->
<aop:before method="buy" pointcut-ref="action"/>
<!-- 声明后置通知 (在切点方法被执行后调用)-->
<aop:after method="clear" pointcut-ref="action"/>
</aop:aspect>
</aop:config> </beans>

测试类:

public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aop2.xml");
Child child = (Child) context.getBean("child");
child.eat();
}
}

结果:

 买水果
小孩子吃苹果
收拾果核

案例示例下载地址:https://github.com/oukele/Spring-aop-xml

Spring中 aop的 xml配置(简单示例)的更多相关文章

  1. spring中aop以xml配置方式

    1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...

  2. Spring中配置文件applicationContext.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  3. Spring的配置文件ApplicationContext.xml配置头文件解析

    Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...

  4. Spring中基于java的配置

    Spring中为了减少XML配置,可以声明一个配置类类对bean进行配置,主要用到两个注解@Configuration和@bean 例子: 首先,XML中进行少量的配置来启动java配置: <? ...

  5. 【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件

    Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  6. Spring中AOP相关的API及源码解析

    Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...

  7. spring之aop概念和配置

    面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...

  8. Spring中AOP简介与切面编程的使用

    Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...

  9. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

随机推荐

  1. 思科设备自动退出配置界面、打断命令输入、禁用DNS查询

    1.自动退出配置界面 问题及原因:当设备没有被操作,空闲一段时间后,控制台回到初始化界面.控制台默认会话时间为10分钟,过期后跳转初始化界面 解决办法:配置控制台永不超时 Switch(config) ...

  2. 关于.Net Core+Angular+Ueditor富文本编辑器的使用方式

    博客:https://www.cnblogs.com/24klr/ 资料:https://www.jianshu.com/p/0b21a1324d47 GitHub:https://github.co ...

  3. 建立EF访问数据库架构时,出现One or more validation errors were detected during model generation

    原因是因为我在写实体类的时候没有为实体类中的属性声明一个主键,即用[key]特性标注在属性上,这样DbContext才能为我们在数据库上找到对应的主键 using System.ComponentMo ...

  4. 获取JSON中所有的KEY

    采用递归的方式,遍历JSON中所有的KEY. JSON格式如下: {"username":"tom","age":18,"addr ...

  5. elasticsearch 7.x 如何满足mysql中的模糊查询功能(like)

    业务场景:筛选项原功能是用mysql左模糊进行过滤查询,现业务要用es,怎么样才能满足原功能,又不损性能. elasticsearch中有关于模糊查询的操作:wildcard 文档:https://b ...

  6. Java小知识---Java请求一个URL。获取网站返回的数据

    对url发送请求,获得返回值: public static String SendGET(String url,String param){ String result="";// ...

  7. spring简单crud配置文件说明

    字体设置:代码  14px 文字 幼圆 15px 1.在pom.xml下导入依赖包 (1)Spring四个核心依赖包 <dependency> <groupId>org.spr ...

  8. MYSQL 优化--inner buffer 与关联查询变等值查询

    转自:https://www.2cto.com/database/201312/262376.html 在数据库的应用中,我们经常需要对多表进行连表操作来获得关系型的数据,因此,应该更加的掌握好Mid ...

  9. Ubuntu19.04系统SSH连接CentOS7虚拟机

    一.为CentOS7配置静态IP 注意查看宿主机(Ubuntu19.04)所在局域网段,当前为172.18.25.108 修改当前系统下virtual box的网络设置 [控制]-->[设置]- ...

  10. 客户端实现WebService服务接口

    首先,要获得搭建好的WebService服务的WSDL,如要实现国内手机号码归属地查询WEB服务,其WSDL为:http://ws.webxml.com.cn/WebServices/MobileCo ...