Spring 一二事(9) - xml 形式的 AOP
AOP在spring中是非常重要的一个
在切面类中,有5种通知类型:
aop:before 前置通知
aop:after-returning 后置通知
aop:after 最终通知
aop:after-throwing 异常通知
aop:around 环绕通知
<bean id="personDAO" class="com.lee.spring002.aop.xml.PersonDAOImpl"></bean>
<bean id="transaction" class="com.lee.spring002.aop.xml.Transaction"></bean> <aop:config >
<!-- 切入点表达式,作用:确定目标类 -->
<!-- spring 会自动检测这个表达式下的类是否是切面,如果是,则不会包含进来 -->
<aop:pointcut expression="execution(* com.lee.spring002.aop.xml.PersonDAOImpl.*(..))" id="perform"/>
<!-- ref 指向切面 -->
<aop:aspect ref="transaction">
<!-- 前置通知 -->
<aop:before method="beginTransaction" pointcut-ref="perform"/> <!--
后置通知
可以获取目标方法的返回值(前置方法获取不到)
如果目标方法抛出异常,后置通知则不会继续执行
-->
<aop:after-returning method="commit" pointcut-ref="perform" returning="val"/> <!--
最终通知
无论目标方法是否抛出异常都将执行此方法
-->
<aop:after method="finallyDisplay" pointcut-ref="perform"/> <!--
异常通知
-->
<aop:after-throwing method="exception" pointcut-ref="perform" throwing="content"/> <!--
环绕通知
能控制目标方法能否执行
前置通知和后置通知能在目标方法的前后加代码,但是不能控制方法的执行
-->
<aop:around method="arround" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>
关于切面的表达式简单说一下:
- 任意公共方法的执行:
execution(public * *(..))
- 任何一个名字以“set”开始的方法的执行:
execution(* set*(..))
AccountService接口定义的任意方法的执行:execution(* com.xyz.service.AccountService.*(..))
- 在service包中定义的任意方法的执行:
execution(* com.xyz.service.*.*(..))
- 在service包或其子包中定义的任意方法的执行:
execution(* com.xyz.service..*.*(..))
IPersonDAO.java
package com.lee.spring002.aop.xml;
public interface IPersonDAO {
public String savePerson();
}
PersonDAOImpl.java
package com.lee.spring002.aop.xml;
public class PersonDAOImpl implements IPersonDAO {
@Override
public String savePerson() {
System.out.println("PersonDAOImpl - savePerson()");
// int a = 1 / 0;
return "save successfully";
}
}
Transaction.java
package com.lee.spring002.aop.xml; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 这是个切面
*
* @author leechenxiang
* @date 2016年1月12日
*
*/
public class Transaction { public void beginTransaction(JoinPoint jp) {
System.out.println("连接点名称: " + jp.getSignature().getName());
System.out.println("目标类名称: " + jp.getTarget().getClass());
System.out.println("Begin transaction...");
} /**
*
* @param jp
* @param val 目标方法返回值,要与returning对应
*/
public void commit(JoinPoint jp, Object val) {
System.out.println("Transaction commit..."); System.out.println("returning: " + val.toString());
} public void finallyDisplay() {
System.out.println("finally...");
} public void exception(JoinPoint jp, Throwable content) {
System.out.println("exception: " + content);
System.out.println("exception: " + content.getMessage());
} public void arround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("arround...");
pjp.proceed(); // 调用目标方法,如果这行不写,则目标方法不执行
}
}
测试:
package com.lee.spring002.aop.xml; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { /**
* 原理:
* 1、当spring容器启动的时候,加载两个bean,对两个bean进行实例化
* 2、当spring容器对配置文件解析到<aop:config>的时候
* 3、把切入点表达式解析出来,按照切入点表达式匹配spring容器内容的bean
* 4、如果匹配成功,则为该bean创建代理对象
* 5、当客户端利用context.getBean获取一个对象时,如果该对象有代理对象,则返回代理对象
* 如果没有代理对象,则返回对象本身
*/
@Test
public void testPerson() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IPersonDAO personDAO = (IPersonDAO)context.getBean("personDAO");
personDAO.savePerson();
} }
github地址:https://github.com/leechenxiang/maven-spring002-aop
Spring 一二事(9) - xml 形式的 AOP的更多相关文章
- Spring 一二事(10) - annotation AOP
先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...
- Spring 一二事(8) - annotation 形式的 MVC
<!-- component:把一个类放入到spring容器中,该类就是一个component 在base-package指定的包及子包下扫描所有的类 --> <context:co ...
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring 一二事(7) - annotation
之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式 在企业开发中,主要还是使用的注解来进行开发的 1 <!-- component:把一个类放入到spring容器中,该类就是一个 ...
- Spring 一二事(4) - 单例
spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...
- Spring 一二事(1)
简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新, ...
- Spring 一二事(5) - 依赖注入
<!-- 依赖注入的装配过程 --> <bean id="person" class="com.lee.spring007.di.xml.setter. ...
- Spring 一二事(2)
静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...
- Spring 一二事(6) - IOC MVC 简易搭建
<bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction"> ...
随机推荐
- Linux命令行和Shell高效率使用方法
Ctrl+R快速搜索history Ctrl+P显示上一条命令 快速执行一条history命令:!!/!-number ======================================== ...
- curl_errno错误码说明
http://hi.baidu.com/lifang218c/item/fa80496eb4cf262f68105b50 http://blog.csdn.net/cwj649956781/artic ...
- mac 终端 使用 gnu coreutils 工具 ls 颜色显示
mac 终端默认 ls 命令无颜色显示: 1: 使用 ls -G 可以显示基本颜色 2:使用 gnu coreutils 工具 mac 终端 使用 gnu coreutils 工具 ls 颜色显示 以 ...
- Linux内核中网络数据包的接收-第一部分 概念和框架
与网络数据包的发送不同,网络收包是异步的的.由于你不确定谁会在什么时候突然发一个网络包给你.因此这个网络收包逻辑事实上包括两件事:1.数据包到来后的通知2.收到通知并从数据包中获取数据这两件事发生在协 ...
- 剑指offer(2) - 二维数组中的查找
题目: 在一个二维数组中.每一行都依照从左到右递增的顺序排序,每一列都依照从上往下递增的顺序排序.请写一个函数,输入一个二维数组和一个整数,推断数组中是否含有该整数. 比如以下的二维数组就是每行.每列 ...
- 解决Unable to load component class org.sonar.scanner.report.ActiveRulesPublisher/Unable to load component interface org.sonar.api.batch.rule.ActiveRules: NullPointerException
解决办法 Delete the directory data/es in your SonarQube installation. Restart SonarQube.
- 解决RMI 客户端异常no security manager: RMI class loader disabled
解决方法: 客户端和服务端的Service包名改一致 ok!!
- centos7 开启ftp服务
1.关闭默认防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewa ...
- 微信小程序:bindtap方法传参
1.wxml <view bindtap="pay_again" data-name="{{orderList.jid}}" data-fee=" ...
- 架构-LAMP特级学习(网站服务器监控)
1.服务监控(SNMP配合CACTI监控) Apache Web服务监控 MySQL数据库监控 磁盘空间监控 2.流量监控(SNMP配合MRTG监控) 网站流量监控 3.使用SNMP可以获取被监控服务 ...