示例:

  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. MathType 6.0中MT Extra(TrueType)问题

    问题 MathType 6.0中MT Extra(TrueType)字体问题在打开MathType6.0时,有时会提示MathType需要安装一个较新版本的MT Extra(TrueType)字体,这 ...

  2. Unity* 实体组件系统 (ECS)、C# 作业系统和突发编译器入门

    Unity* 中的全新 C# 作业系统和实体组件系统不仅可以让您轻松利用以前未使用的 CPU 资源,还可以帮助您更高效地运行所有游戏代码.然后,您可以使用这些额外的 CPU 资源来添加更多场景动态和沉 ...

  3. 二 MyBatis 从入门到进阶 2 Maven 入门

    1 Maven 的使用 1.1 本地仓库与中央仓库 本地仓库:Window \ Preferences \ Maven \ User Settings \ Local Repository 中央仓库: ...

  4. nginx配置反向代理支持session

    Nginx反向代理tomcat,很是方便,但是也有些细节的问题需要注意:今天遇到了这样一个问题,tomcat中路径“host/web1”,nginx中直接“host/”代理,这时候session就无法 ...

  5. LCA模板(数剖实现)

    题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n) ...

  6. Oracle 即时客户点下载以及简单连接数据库的方法

    1. 下载方法 百度 oracle client 第一个即可 2. 下载地址为: https://www.oracle.com/database/technologies/instant-client ...

  7. oracle数据库表恢复到特定时间点

    某一张表被应用软件里误操作把数据都清空了,现在想恢复到清空之间,比如2013年8月13日14点以前,应该怎样操作? 通过这个问题可以引发一系列的知识点串联. 1.如果开启闪回可以使用闪回表. 怎样查看 ...

  8. OpenTSDB在HBase中的底层数据结构设计

    0.时序数据库 时间序列(Time Series):是一组按照时间发生先后顺序进行排列的数据点序列,通常一组时间序列的时间间隔为一恒定值(如1秒,5分钟,1小时等). 时间序列数据可被简称为时序数据. ...

  9. .Net Core 3.0使用Grpc进行远程过程调用

    因为.Net Core3.0已经把Grpc作为一等臣民了,作为爱好新技术的我,当然要尝鲜体验一下了,当然感觉是Grpc作为跨语言的产品做的相当好喽,比起Dubbo这种的,优势和劣势还是比较明显的. 我 ...

  10. C语言&*符号使用及大端法小端法测试

    工具:Microsoft Visual C++ 6.0 例子: int a = 1; int* b = &a; C语言规定a表示存储单元中的数据,&a表示存储单元的地址,b存储的就是a ...