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的更多相关文章

  1. Spring的声明式事务管理<tx:advice/>

    <tx:advice/> 有关的设置 这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传 ...

  2. Spring的声明式事务管理<tx:advice/> 有关的设置

    <tx:advice/> 有关的设置 这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传 ...

  3. Spring声明式事务管理基于tx/aop命名空间

    目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...

  4. 框架源码系列十一:事务管理(Spring事务管理的特点、事务概念学习、Spring事务使用学习、Spring事务管理API学习、Spring事务源码学习)

    一.Spring事务管理的特点 Spring框架为事务管理提供一套统一的抽象,带来的好处有:1. 跨不同事务API的统一的编程模型,无论你使用的是jdbc.jta.jpa.hibernate.2. 支 ...

  5. spring tx:advice 和 aop:config 配置事务

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. [转]spring tx:advice 和 aop:config 配置事务

      <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www. ...

  7. spring.net tx:advice 和 aop:config 配置事务 匹配名字的方法管理事务

    在网上找到的都是java里的配置方式,后来认真读了下spring.net的帮助文档,解决了这个问题:现在把我的server层的配置文件copy出来: <?xml version="1. ...

  8. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  9. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

随机推荐

  1. 【基础知识】.Net基础加强07 天

    一. 自定义泛型 1. 泛型的目的:提高代码的复用性. 2. T:一般叫做“类型参数”,把数据类型作为参数传递 .一般用T类表示或者以大写T开头的比如:TKey,TValue.TOutput.TRes ...

  2. Oracle数据创建表空间

    一.直接在服务器端通过sqlplus命令行创建: 如果您用的是Linux系统,那么Oracle用户名为oracle.同时,您是在oracle服务器上操作. 如果是在Windows系统下, 请先点击“开 ...

  3. Nginx运行Mono Web (ASP.NET)

    Mono Web除了可以使用Apache/mod_mono方式承载运行外,还可以使用Nginx/FastCGI方式运行. Nginx配置asp.net更简单方便,用处也多,可以通过FastCGI运行a ...

  4. HTML5 Canvas实现黑客帝国文字掉落效果

    效果: 原理: 用canvas逐行输出文字,然后让背景颜色逐渐加深,再随机中断某些列. 代码: HTML: <canvas id="c"></canvas> ...

  5. [自制简单操作系统] 7、多任务(二)——任务管理自动化&任务休眠

    前言 >_<" 这里仿照窗口管理的方式将任务管理也修改成相应的管理模式,这样可以灵活的添加多个任务,而不必每次都要修改任务切换函数:此外还在任务休眠做了尝试,通过将任务挂起和唤醒 ...

  6. ClassLoader.getSystemResourceAsStream()

    一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件config.properties 那么,应该有如下代码: //前面没有" ...

  7. python数据采集与多线程效率分析

    以前一直使用PHP写爬虫,用Snoopy配合simple_html_dom用起来也挺好的,至少能够解决问题. PHP一直没有一个好用的多线程机制,虽然可以使用一些trick的手段来实现并行的效果(例如 ...

  8. mobilebone.js使用笔记

    mobilebone.js主要用来是网页呈现单页效果,添加类似native app的页面切换效果.原理是:当打开a链接里的页面时,不再以传统的新页面打开,而是以ajax-html的方式,将新页面的内容 ...

  9. paip.互联网产品要成功的要素

    paip.互联网产品要成功的要素 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/atti ...

  10. Liferay7 BPM门户开发之28: Portlet文件上传,及实体类同步更新上传

    抓住核心 . Liferay文件上传的核心就是使用UploadPortletRequest类 继承关系java.lang.Object extended byjavax.servlet.Servlet ...