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的一些实 ...
随机推荐
- Vim快捷键大全
vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是"visual interfa ...
- 长安大学ACM竞赛部
本博客为长安大学ACM竞赛部的公共博客,记录长大ACMer的成长点滴. 开此博客,诸君共勉.
- win8 or win7安装ubuntu双系统
安装双系统的效果 现在使用win和linux双系统,整个环境相当方便好用,比如在Linux系统上,仍能访问NTFS(win的文件系统格式)中的文件和文档,当然win下的一些像matlab.vs等是不能 ...
- [Android]Android SDk Manager中创建模拟器无法选择CPU问题解析
方法一.正常下载所需sdk包 启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Setti ...
- nginx使用openssl的证书-泛解析
一.需求分析 我们公司测试环境,域名更换,原来的*.dev.devbao.cn 变为 *.dev.vdai.cn ,此处的*表示多个二级域名,导致原来为dev.devbao.cn制作的ssl ...
- FZU 2240 Daxia & Suneast's problem
博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...
- 解锁Oracle数据库用户
Oracle数据库,如何解除数据库用户账户的锁定呢???如何修改口令呢??? 请问各位高手:Oracle数据库,如何解除数据库用户账户的锁定呢???如何修改口令呢???[万分感激各位高手的帮忙] 00 ...
- vector,list,deque
stl提供了三个最基本的容器:vector,list,deque. vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即[]操作符,但由 ...
- vim 常用操作
a 进入INSERT MODE x 删除当前光标下的字符dw 删除光标之后的单词剩余部分.d$ 删除光标之后的该行剩余部分.dd 删除当前行. c 功能和d相同,区别在于完成删除操作后进入INSERT ...
- ® 不需要显示为商标符的做法
若url中有参数reg,则把® 变为 ®