Spring AOP的一个简单实现
针对学习笔记(六)中的购买以及退货代码,我们加入AOP框架,实现同样一个功能。
首先配置XML:service采用和之前一样的代码,只是没有通过实现接口来实现,而是直接一个实现类。transactionManager依旧为之前的事务管理器。
<?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="service" class="aop_part.Demo2.GodService">
</bean> <!-- 切面类 -->
<bean id="transactionManager" class="aop_part.Demo1.TransactionManager">
</bean> <!-- 切入点 -->
<aop:config>
<aop:aspect id="transactionAspect" ref="transactionManager">
<aop:before method="transaction_start"
pointcut="execution(* aop_part.Demo2.*Service.*(..))"/>
<aop:after-returning method="transaction_submit"
pointcut="execution(* aop_part.Demo2.*Service.*(..))"/>
<aop:after-throwing method="transaction_rollback"
pointcut="execution(* aop_part.Demo2.*Service.*(..))"/>
</aop:aspect>
</aop:config> </beans>
我们通过再xml中配置aop参数,实现了将事务操作插入到service的前中后中。
写一个Text类,来观察输出的结果:
package aop_part.Demo2; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Richard on 2017/7/28.
*/
public class test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("aop_part/Demo2/aop_Context.xml");
GodService godService= (GodService) context.getBean("service");
System.out.println(godService.getClass().getName());
godService.buy("rekent","AOP_Study");
godService.returnGod(1100);
}
}
结果和预期一样,与之前是一致的:
aop_part.Demo2.GodService$$EnhancerBySpringCGLIB$$3f2fc81
【事务开始】用户rekent购买了AOP_Study
【事务提交】
【事务开始】订单1100申请退回
【事务提交】 Process finished with exit code 0
与此同时,Spring 框架通过Java SE动态代理和cglib来实现AOP功能:
当明确指定目标类实现的业务接口时,Spring采用动态代理,也可以强制使用cglib
当没有指定目标类的接口时,Spring使用cglib进行字节码增强。
此处由于没有申明接口,所以Spring采用cglib来实现AOP,我们通过反射获取到了cglib动态生成的代理对象的类名,即aop_part.Demo2.GodService$$EnhancerBySpringCGLIB$$3f2fc81
Spring AOP的一个简单实现的更多相关文章
- 运用Spring Aop,一个注解实现日志记录
运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...
- Spring AOP就是这么简单啦
前言 只有光头才能变强 上一篇已经讲解了Spring IOC知识点一网打尽!,这篇主要是讲解Spring的AOP模块~ 之前我已经写过一篇关于AOP的文章了,那篇把比较重要的知识点都讲解过了一篇啦:S ...
- Spring AOP注解形式简单实现
实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...
- Spring Boot -- Spring AOP原理及简单实现
一.AOP基本概念 什么是AOP,AOP英语全名就是Aspect oriented programming,字面意思就是面向切面编程.面向切面的编程是对面向对象编程的补充,面向对象的编程核心模块是类, ...
- spring cloud 创建一个简单Eureka Server
在Spring Cloud实现一个Eureka Server是一件非常简单的事情.下面我们来写一个Eureka Server DEMO. 编码 父项目pom.xml <?xml version= ...
- spring aop 的一个思考
问题: spring aop 默认使用jdk代理织入. 也就是我们常这样配置:<aop:aspectj-autoproxy /> 通过aop命名空间的<aop:aspectj-au ...
- [Java定时器]用Spring Task实现一个简单的定时器.
今天做一个项目的的时候需要用到定时器功能.具体需求是: 每个月一号触发一次某个类中的方法去拉取别人的接口获取上一个月份车险过期的用户.如若转载请附上原文链接:http://www.cnblogs.co ...
- spring aop 的一个demo(未完,待完善)
假设我们有这样的一个场景 : 对于一个类的众多方法,有些方法需要从缓存读取数据,有些则需要直接从数据库读取数据.怎样实现呢? 实现方案有多种.下面我说下常见的几种实现方案 : 1.直接采用spring ...
- Spring AOP的一个比喻和IOC的作用
aop切面编程就是在常规的执行java类中方法前或执行后加入自定义的方法.比如你本来每天都去打酱油,去,打酱油,回.现在我每天在你打酱油路上等着,你去打酱油的时候我打你一顿,回来的时候给你点糖果吃.你 ...
随机推荐
- JS将unicode码转中文方法
原理,将unicode的 \u 先转为 %u,然后使用unescape方法转换为中文. ? 1 2 3 4 <script type="text/javascript"> ...
- B1954 The xor-longest Path
爆炸入口 给定一颗带权值的,节点数为n的树,求树上路径最大异或和. solution: 先dfs将所有点到根的异或和算出来.然后放进tire树中贪心. #include<cstdio> # ...
- PHP下载远程文件到指定目录
PHP用curl可以轻松实现下载远程文件到指定目录: <?php class Download { public static function get($url, $file) { retur ...
- 【linux】【指令】systemctl 指令部分解读
systemctl [OPTIONS...] {COMMAND} ... Query or send control commands to the systemd manager. -h --hel ...
- 对文件 I/O,标准 I/O 的缓冲的理解
1.标准I/O缓冲区 要理解标准I/O,就要先知道文件I/O的业务逻辑. 下面图示为文件I/O 如执行下面的代码: write(fd, buf2, sizeof(buf2)); 图中 buf:就是bu ...
- 猜数字问题 python
猜数字问题,要求如下: ① 随机生成一个整数 ② 猜一个数字并输入 ③ 判断是大是小,直到猜正确 ④ 判断时间提示:需要用time模块.random模块该题目不需要创建函数 import random ...
- python基础之多态与多态性、绑定方法和非绑定方法
多态与多态性 多态 多态并不是一个新的知识 多态是指一类事物有多种形态,在类里就是指一个抽象类有多个子类,因而多态的概念依赖于继承 举个栗子:动物有多种形态,人.狗.猫.猪等,python的序列数据类 ...
- html_parser.py
coding=UTF-8 # HTML解释器 import re from bs4 import BeautifulSoup class htmlParser(): def parse(self, u ...
- python读取文件
请参考:http://www.cnblogs.com/sysuoyj/archive/2012/03/14/2395789.html
- android Intent onNewIntent 什么时候调用
1.activity A 的lanch model 为singleTop 此刻,A在activity 栈顶,那么就会调用A 的onNewIntent 如果A不在栈顶,则不会调用. 2.activity ...