Spring中 aop的 xml配置(简单示例)
示例:
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配置(简单示例)的更多相关文章
- spring中aop以xml配置方式
1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...
- Spring中配置文件applicationContext.xml配置详解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- Spring中基于java的配置
Spring中为了减少XML配置,可以声明一个配置类类对bean进行配置,主要用到两个注解@Configuration和@bean 例子: 首先,XML中进行少量的配置来启动java配置: <? ...
- 【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件
Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring中AOP相关的API及源码解析
Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...
- spring之aop概念和配置
面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...
- Spring中AOP简介与切面编程的使用
Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
随机推荐
- django中聚合aggregate和annotate GROUP BY的使用方法
接触django已经很长时间了,但是使用QuerySet查询集的方式一直比较低端,只会使用filter/Q函数/exclude等方式来查询,数据量比较小的时候还可以,但是如果数据量很大,而且查询比较复 ...
- Vim常用操作集合
基本上 vi/vim 共分为三种模式,分别是一般命令模式(Command mode),编辑模式(Insert mode)和命令行模式(Last line mode). 命令模式: 用户刚刚启动 vi/ ...
- 我和CMS的往事
CMS(内容管理系统)放在web中理解,也就是我们常说的后台了,用于网站的日常管理.又到期末了,我们的课程——web程序设计,需要提交一份期末大作业,最近需要开发出一个基于JSP的简单web,所以呢, ...
- VS附加到进程调试
WIN+R 进入cmd命令 输入 netstat -ano | find "进程端口" 找端口 打开vs alt+d+p选择上图对应的进程
- TCP和SSL TCP应用
TCP和SSL TCP应用 对于普通开发者而言编写TCP应用通讯是一件相对复杂的工作,毕竟需要一系列的bytes操作:如果再针对SSL的安全性处理相信会把很多普通开发者拒之门外.为了简化这一问题Bee ...
- 菜鸟系列Fabric——Fabric 1.2 多机部署(3)
多机部署fabric kafka共识 1. 角色分配 主机1 主机 2 Org1 peer0 1 Org2 peer 0 1 Orderer 0 1 Orderer 2 kafka 0 1 kafka ...
- (5.6)mysql高可用系列——MySQL Utilities 管理工具
关键词:mysql工具集,mysql管理工具,mysql utilities [1]安装mysql utilities cd /download wget https://cdn.mysql.com/ ...
- Java基础(六)
面向对象 概述 生活举例 代码体验 类与对象的关系 类的定义 根据类创建对象 对象的基本使用 练习:手机类与对象 内存图:一个对象 内存图:两个对象 内存图:同一个对象 局部变量与成员变量的区别 pr ...
- C++多线程基础学习笔记(六)
condition_variable.wait.notifiy_one.notify_all的使用方式 condition_variable:条件变量 wait:等待被唤醒 notify_one:随机 ...
- vc_redist x64 或者x86下载地址
https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads 微软的东西,果然还是人 ...