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 ...
随机推荐
- iOS 直播(一)
由于业务需求,需要从腾讯直播sdk要迅速转移到自主开发(先让我默哀三分钟).不多说,直接开始唠嗑! 这个项目超级简单,简单到只能一个推流,一个拉流的功能.需求多的再另谈. 前期准备:推流用优酷开源的L ...
- 在jsp中常用的内置对象(5个)小总结和两种页面跳转方式(服务器端调转、客户端跳转)的区别
jsp中常用的几个内置对象: 一.request对象 主要作用: (1)获取请求页面的信息 比如:request.getParameter("参数名"); (2)获取客户端 ...
- pm2使用介绍
https://segmentfault.com/a/1190000002539204
- sql把一个表数据插入到另一张表
把一个表数据插入到另一张表 insert into tableB (field1,field2,field3,field4) select field1,field2,field3,'val4' fr ...
- 【Python之路Day12】网络篇之Paramiko
paramiko模块,基于SSH用于连接远程服务器并执行相关操作. 一.安装 pip3 install paramiko 二.使用 1. SSHClient 用于连接远程服务器并执行基本命令 基于用户 ...
- IP协议
因特网协议(Internet Protocol, IP)是Internet的核心协议之一, 工作在网络层.IP协议提供节点间的寻址,路由以及顺序控制, 流量控制等服务. IP协议分为IPv4和IPv6 ...
- UML建模语言入门 -- 用例视图详解 用例视图建模实战
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 一. 用例视图概述 用例视图表述哪些 ...
- 使用FEST-Swing测试GUI
在Java中最常用的单元测试工具要算JUnit了.FEST-Swing是一个能够与JUnit集成的GUI测试框架.使用FEST-Swing可以更方便的对Swing进行一系列的测试.下面就说 ...
- 我的fckeditor实践
一开始我不懂这个ConnectorServlet是何用处,后来发现是专门用于文件上传的,因为fckeditor默认是不支持这个功能的. ConnectorServlet: /* * FCKeditor ...
- 深入了解javascript事件流
摘要:事件流这个东西是比较重要的,为了让自己更加理解js中的事件流,必须整理整理,梳理一下事件流的各种东西啊.本文大部分内容参考<javascript高级程序设计第三版> 先来一段书里的原 ...