Spring AOP Schema aop:config、tx:advice
Spring AOP Schema aop:config、tx:advice
一、 利用aop:config标签实现AOP
首先看个例子,如下
接口代码:
package com.lei.demo.aop.schema;
public interface IHello {
public void sayHello();
}
接口实现:
package com.lei.demo.aop.schema;
public class HelloService implements IHello {
public void sayHello() {
System.out.println("-----Hello World!-----");
}
}
接下来我们要实现AOP,即调用sayHello方法时切入通知。
1. 第一种方法aop:config中配置aop:pointcut和aop:aspect
定义一个切面支持类HelloAspect.java
package com.lei.demo.aop.schema; /*
* 切面支持类
*/
public class HelloAspect { //前置通知
public void beforeAdvice() {
System.out.println("===========before advice");
} //后置最终通知
public void afterFinallyAdvice() {
System.out.println("===========after finally advice");
}
}
Xml配置:Spring-AOP-Schema.xml
<?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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 目标类 -->
<bean id="helloService" class="com.lei.demo.aop.schema.HelloService" /> <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" /> <!-- 配置切面 -->
<!-- aop:advisor,是有顺序的,必须放在aop:pointcut之后 -->
<aop:config>
<aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" /> <aop:aspect ref="helloAspect">
<!—以下使用了两种方法定义切入点 pointcut-ref和pointcut-->
<aop:before pointcut-ref="helloPointcut" method="beforeAdvice" />
<aop:after pointcut="execution(* com.lei.demo.aop.schema..*.*(..))" method="afterFinallyAdvice" />
</aop:aspect> </aop:config>
</beans>
以上配置中method="beforeAdvice"、method="afterFinallyAdvice"对应helloAspect中的方法
测试,App.java
package com.lei.demo.aop.schema; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private static ApplicationContext ctx; public static void main(String[] args) { ctx = new ClassPathXmlApplicationContext("Spring-AOP-Schema.xml"); IHello helloService = ctx.getBean("helloService",IHello.class); helloService.sayHello();
} }
运行App.java,结果如下:
===========before advice
-----Hello World!-----
===========after finally advice
2. 第二种方法aop:config中配置aop:pointcut和aop:advisor
实现org.aopalliance.intercept.MethodInvocation接口,
HelloAroundAdvice.java如下:
package com.lei.demo.aop.schema; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class HelloAroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("++++++before advice");
arg0.proceed();
System.out.println("++++++after advice"); return null;
} }
Xml配置:Spring-AOP-Schema.xml
<?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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 目标类 -->
<bean id="helloService" class="com.lei.demo.aop.schema.HelloService" /> <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" /> <bean id="helloArroundAdvice" class="com.lei.demo.aop.schema.HelloAroundAdvice" /> <!-- 配置切面 -->
<aop:config>
<aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
<aop:advisor advice-ref="helloArroundAdvice" pointcut-ref="helloPointcut"/> </aop:config>
</beans>
仍然运行App.java,结果如下:
++++++before advice
-----Hello World!-----
++++++after advice
二、 AOP中加入事务,引入tx:advice
集成Hibernate时,需要引入事务,以下配置引用Spring 4.0.3和Hibernate 4.3.5
Spring-hibernate.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
"> <!-- Hibernate4 -->
<!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
<context:property-placeholder location="classpath:persistence-mysql.properties" /> <bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<!-- 可以加多个包 -->
<value>com.lei.demo.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean> <!-- 数据库映射 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean> <!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice> <aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.lei.demo.service..*.*(..))" />
<!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/> </aop:config> </beans>
重点看一下tx:advice的配置
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
意思是说,这个事务advice的管理者为transactionManager,而这个advice中规定了匹配add*、get*方法的传输方式为required,也就是说没有sessionFactory的时候会自动建立,有的时候就不会建立了。当然这只是规定了一个advice,你还需要指定一个aop:pointcut去引用它,例如下边的aop:config配置节。这样,这个advice就被织入到txPointcut这个pointcut上了。
Spring AOP Schema aop:config、tx:advice的更多相关文章
- Spring的声明式事务管理<tx:advice/>
<tx:advice/> 有关的设置 这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传 ...
- Spring的声明式事务管理<tx:advice/> 有关的设置
<tx:advice/> 有关的设置 这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传 ...
- Spring声明式事务管理基于tx/aop命名空间
目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...
- 框架源码系列十一:事务管理(Spring事务管理的特点、事务概念学习、Spring事务使用学习、Spring事务管理API学习、Spring事务源码学习)
一.Spring事务管理的特点 Spring框架为事务管理提供一套统一的抽象,带来的好处有:1. 跨不同事务API的统一的编程模型,无论你使用的是jdbc.jta.jpa.hibernate.2. 支 ...
- spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- [转]spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www. ...
- spring.net tx:advice 和 aop:config 配置事务 匹配名字的方法管理事务
在网上找到的都是java里的配置方式,后来认真读了下spring.net的帮助文档,解决了这个问题:现在把我的server层的配置文件copy出来: <?xml version="1. ...
- Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容
© 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
随机推荐
- 黑马程序员+Winform基础(上)
黑马程序员+Winform基础 ---------------<a href="http://edu.csdn.net"target="blank"> ...
- Mozilla Firefox 24.0 Beta 5 发布
Mozilla今天将Firefox 24.0 Beta 5版本放到了FTP的release目录,新版开始全面支持OS X 10.7全新的滚动条样式,禁止网站插件运行的功能出现在任务栏左侧,调整了界面U ...
- [ThingWorx] Install Guideline
环境 硬件 软件 安装过程 PostgreSQL Tomcat ThingWorx
- Apache CXF Webservice入门
1.步骤一览 关于CXF的介绍请移步官网.百科,这里仅供初次使用者入门. 2.步骤详情 2.1.环境准备 apache-cxf-3.0.0.zip下载 jdk1.7.0_51 Eclipse4.3.0 ...
- H5 Canvas刮刮乐
玩游戏的人 很多时候都会遇到翻牌子 开宝箱. 总有人傻傻的在哪里还纠结很久到底点哪一个! 纠结 指不定翻哪一个会多一点,你明明看到那个卡片的奖项多 . 那我就告诉你好了 其实很多时候在你点开那个 ...
- Atitit. 构造ast 语法树的总结attilax v2 q0f
Atitit. 构造ast 语法树的总结attilax v2 q0f 1. Ast结构树形1 2. ast view (自是个160k的jar )2 2.1. 多条语句ast结构2 2.2. 变量定义 ...
- paip.utf-8,unicode编码的本质输出unicode文件原理 python
paip.utf-8,unicode编码的本质输出unicode文件原理 python #别的语言,java php都是unicode,走十python不一样. #enddef #t ...
- 如何安装、配置Apache
Apache的安装流程网上版本很多,但很多版本有错误,或者缺漏,如果初学者按照那样安装的话会遇到各种各样的问题.我整理了自己安装.配置Apache的过程,亲测有效,分享给大家. 基本过程如下: Dow ...
- php读取大文件的方法
1.使用file 函数直接读取 $starttime = microtime_float(); ini_set('memory_limit','-1'); $file = "testfile ...
- iOS开发中使用CocoaPods来管理第三方的依赖程序
之前也碰到类似的问题,怎样去管理这些第三方的文件,虽然手动添加也不算麻烦. 写这篇文章主要参考了唐巧的博文,链接如下: http://blog.devtang.com/blog/2012/12/02/ ...