spring杂记
AOP(Aspect-Oriented Programming,面向切面编程)是一种编程思想,并不是一种具体的实现,谈到实现一般有Filter和代理模式两种常见的使用方式。Java中常见的AOP技术有两个,分别是Filter和代理模式(也可以称为过滤器和拦截器),Filter是基于回调函数(请看《Java回调机制解析》),代理模式是基于Java反射技术,代理模式又分为静态代理和动态代理,动态代理就是拦截器的简单实现。(过滤器和拦截器的区别可参见《过滤器和拦截器的区别》)他们各自实现的功能不同,原理如出一辙。
Spring AOP’s approach to AOP differs from that of most other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable); it is rather to provide a close integration between AOP implementation and Spring IoC to help solve common problems in enterprise applications.
Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container.
spring AOP是为结合IoC容器设计的,不是为了提供全面的AOP功能。
Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!
If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework.
spring AOP不能用于“私有/保护”成员函数以及“构造函数”。
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}
@Pointcut("within(com.xyz.someapp.trading..*)")
private void inTrading() {}
@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}
It is a best practice to build more complex pointcut expressions out of smaller named components as shown above. When referring to pointcuts by name, normal Java visibility rules apply (you can see private pointcuts in the same type, protected pointcuts in the hierarchy, public pointcuts anywhere and so on). Visibility does not affect pointcut matching.
建议复杂的pointcut使用简单的pointcut的逻辑组合。pointcut的private/protected/public影响pointcut的可见性(pointcut调用另一个pointcut),但不影响匹配时的可见性。
AspectJ follows Java’s rule that annotations on interfaces are not inherited.
注解一定要放在实现类上,因为在接口上的注解不会被实现类继承。
使用@Transaction注解事务管理需要特别注意以下几点:
- 如果在接口、实现类或方法上都指定了@Transactional 注解,则优先级顺序为方法>实现类>接口;
- 建议只在实现类或实现类的方法上使用@Transactional,而不要在接口上使用,这是因为如果使用JDK代理机制是没问题,因为其使用基于接口的代理;而使用使用CGLIB代理机制时就会遇到问题,因为其使用基于类的代理而不是接口,这是因为接口上的@Transactional注解是“不能继承的”;
- 在JDK代理机制下,“自我调用”同样不会应用相应的事务属性,其语义和<tx:tags>中一样;
- 默认只对RuntimeException异常回滚;
- 在使用Spring代理时,默认只有在public可见度的方法的@Transactional 注解才是有效的,其它可见度(protected、private、包可见)的方法上即使有@Transactional 注解也不会应用这些事务属性的,Spring也不会报错,如果你非要使用非公共方法注解事务管理的话,可考虑使用AspectJ。
spring杂记的更多相关文章
- Spring杂记BeanFactory之getBean方法
1.(BeanFactory) getBean(beanName) 2.(AbstractBeanFactory) doGetBean 3.(AbstractBeanFactory) transfor ...
- 【Spring】Spring,我的零散使用杂记
通过Java类设置配置信息,JavaConfig Spring常用的通过XML或者@Controller.@Servoce.@Repository.@Component等注解注册Bean,最近看Spr ...
- Spring Boot 学习杂记
使用IntelliJ IDEA构建Spring Initializr
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- 玩转spring boot——快速开始
开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...
- Spring基于AOP的事务管理
Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...
- [Spring]IoC容器之进击的注解
先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...
- 学习AOP之透过Spring的Ioc理解Advisor
花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
随机推荐
- Josephus问题的不同实现方法与总结
/************************************************************************/ /* Josephus问题--数组实现 */ /* ...
- android 物理按键 监听
android连接了一个4x4的矩阵键盘,linux内核中注册了按键,在app中监听键盘事件. package com.example.tony.keydemo; import android.sup ...
- Debian安装 ss-qt5
Kali Linux 基于 Debian ,安装时得参考 Debian 安装方法,用 Ubuntu 的方法装不上.官方安装指南这样说: Debian安装指南 然而在执行 sudo apt-get in ...
- spring boot maven 插件
spirng boot 需要使用专用maven插件打包,才能打包.引入如下. <build> <plugins> <plugin> ...
- winServer2008添加IIS服务
右键我的电脑,选择管理,打开服务器管理器 点击左边菜单栏角色调出角色窗口 接着点击添加角色,弹出添加角色向导 点击下一步进入服务器角色选项 勾选Web服务器(IIS),点击下一步 出现 点击下一步,出 ...
- [转]numpy中的matrix矩阵处理
今天看文档发现numpy并不推荐使用matrix类型.主要是因为array才是numpy的标准类型,并且基本上各种函数都有队array类型的处理,而matrix只是一部分支持而已. 这个转载还是先放着 ...
- 最近一段OI学习计划
1.在寒假的时间里尽量吧图论和DP的基础先学一下: 图论:数,二叉树,DFS.BFS遍历,然后最短路径(Floyd.dijkstra.SPFA),然后再最小生成树吧,如果还有时间的话(kruskal( ...
- Defraggler(磁盘整理软件) V2.21.993 绿色版
软件名称: Defraggler(磁盘整理软件) 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 5.0MB 图片预览: 软件简介: Defraggler ...
- centos7配置开启无线网卡,重启防火墙
centos7配置无线网卡: 在虚拟机为nat的网络连接下(就是默认的那个),centos7默认网卡未激活. 可以设置 文件 /etc/sysconfig/network-scripts/ifcfg- ...
- Redis 数据序列化方法 serialize, msgpack, json, hprose 比较
最近弄 Redis ,涉及数据序列化存储的问题,对比了:JSON, Serialize, Msgpack, Hprose 四种方式 1. 对序列化后的字符串长度对比: 测试代码: $arr = [0, ...