Spring横切面(advice),增强(advisor),切入点(PointCut)(转)
Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解:
1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
这样当定义切面时可以注入会话工厂属性如下:
<!-- 配置事务处理的Bean,定义切面(advice) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
2.对数据库的操作都属于事务的操作,而数据库默认是读已提交 read-only="true"形式
当读写操作时会调用DAO方法,而操作DAO的核心业务是Service,即 Spring,然而Spring
要对调用DAO的方法加以控制,所以就产生了切面(advice) //这个切面相当于一道防护门。
3.切面(advice)有了,Spring自己会找到应该执行哪些DAO里的方法了,(因为DAO已经注入到了Spring中;即service层引用Dao层)
所以自身的防护就变得复杂了,所以需要紧密耦合,自然增强就产生了 。如下:
<!-- 配置AOP -->
<aop:config>
<aop:pointcut id="daoMethod" expression="execution(public * com.dvp.module.*.*.*.dao.impl..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod"/>
</aop:config>
<aop:config>
<aop:pointcut id="daoMethod2" expression="execution(public * com.dvp.base.dao.impl..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod2"/>
</aop:config>
//OK所以一个Spring增强(advisor)=切面(advice)+切入点(PointCut)
Spring横切面(advice),增强(advisor),切入点(PointCut)(转)的更多相关文章
- Spring 通知(Advice)和顾问(Advisor)
AOP ( Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- 基于注解的Spring AOP入门、增强Advice实例
这篇文章简单通过一个例子,介绍几种增强的基本配置,以方便spring框架初学者对aop的代码结构有个清楚的了解认识.首先,spring支持aop编程,支持aspectJ的语法格式来表示切入点,切面,增 ...
- Spring的3种切入点PointCut实现
Pointcut是Join Point的集合,它是程序中需要注入Advice的位置的集合.Spring主要提供了3种切入点的实现: 1.静态切入点: 2.动态切入点: 3.自定义切入点. 静态切入点 ...
- Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut
pointcut(切断点)表达式: execution(public * *(..)) execution(* set*(..)) execution(* com.xyz.service.Accoun ...
- Spring面向切面编程
在使用面向切面编程时,我们可以在一个地方定义通用的共鞥,但是可以通过声明的方式定义这个功能要以何种方式在何处应用,而无需修改受影响的类.横切关注点可以被模块化为特殊的类,这些类被称为切面.这样的优点是 ...
- Spring AOP 切面编程的方法
spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...
- Spring Aop(八)——advisor标签
转发地址:https://www.iteye.com/blog/elim-2396274 8 advisor标签 advisor标签是需要定义在aspect标签里面的,其作用与aspect类似,可以简 ...
- Spring 03 切面编程
简介 AOP(Aspect Oriented Programming),即面向切面编程 这是对面向对象思想的一种补充. 面向切面编程,就是在程序运行时,不改变程序源码的情况下,动态的增强方法的功能. ...
- 利用cglib包实现Spring中aop的<aop:advisor>功能
一:前言 还有<aop:before>/<aop:after>/<aop:around>的没有实现,不过根<aop:advisor>是差不多的,就是要额 ...
随机推荐
- java 正则表达式获得html字符串中<img src>中的src中的url地址
public static Set<String> getImgStr(String htmlStr) { Set<String> pics = new HashSet< ...
- java核心技术36讲
https://time.geekbang.org/column/intro/82?utm_source=website&utm_medium=infoq&utm_campaign=8 ...
- SICP-练习2.17
[问题] 请定义出过程last-pair.它返回仅仅包括给定(非空)表里最后一个元素的表: (last-pair (list 23 72 149 34)) (34) [分析] last-pair须要处 ...
- Discuz常见小问题-如何修改UCenter创始人密码
http://faq.comsenz.com/viewnews-925 参照上面这个帖子,把tools.php下载下来并放到uc_server目录下,打开文件,找到第10行设置一个初始工具箱密码 ht ...
- 使用JSP实现输出(web基础学习笔记二)
Jsp:Java Server Page 服务器端的Java页面,动态网页技术 jsp注释 显式注释:这种注释客户端是允许看见的;<!--html注释--> 隐式注释:这种注释客户端是看不 ...
- EventBus源码分析
一. EventBus简介 1.1.EventBus EventBus 是一个 Android 事件发布/订阅框架,通过解耦发布者和订阅者简化Android 事件传递,这里的事件可 ...
- 自我分析-Spring IOC
Spring IOC容器实现原理大致是容器(Map)+反射(Java反射和cglib).Spring提供丰富的ApplicationContext.以FileSystemXmlApplicationC ...
- SVN diff 笔记
SVN diff命令在实际中经常使用,在此记录使用点滴. #对比工作文件与缓存在.svn的“原始”拷贝: svn diff #显示工作文件和服务器版本2的不同: svn diff -r 2 #显示分支 ...
- JUC-Callable
实现线程的方式有四种: 1,实现runnable接口: 2,继承Thread. 3,也就是本节的Callable接口. 4,使用线程池. 区别: 实现Callable接口的方式,相较于实现Runnab ...
- java 新创建的类要重写的方法
重写toString方法,可以控制println打印的结构. 如果需要往hashSet或者HashMap中存,需要重写hashCode和equals方法,因为hashSet执行添加,以对象为参数删除, ...