Spring AOP之xml 配置实现
首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西。不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的xml配置学起、然后再进一步简化:最开始也是在xml文件里面配置很多的bean,每个model都得配置一个bean标签,直到后来的只要一句话
<context:component-scan base-package="com.lxk.hello" />;
就可以搞定spring的依赖注入,也就是使用注解相关的配置了。
这个AOP配置也是这么个道理。所以先从这个麻烦但是基础的配置模式看起。好理解这个AOP。
对很多相同的操作提取到切面上去操作。这个只是把切面内部的方法写好,下面的配置文件说明这个切面切哪里也就是切入点在哪,怎么切,是前置,环绕,后置,还是异常等等。
package com.lxk.spring.aop; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import java.util.ArrayList;
import java.util.List; /**
* 切面(spring aop 就不需要拦截器啦)
* (模拟hibernate里面保存数据要打开事物,然后各种增删改之后,再提交事物。)
*/
public class Transaction { public void beginTransaction() {//前置通知
//打开事物
System.out.println("begin Transaction");
} /**
* @param joinPoint 通过joinPoint可以得到目标类和目标方法的一些信息
* @param val 目标方法的返回值
* 和<aop:after-returning returning="val"/>中returning的值保质一致
*/
public void commit(JoinPoint joinPoint, Object val) {//后置通知
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName);
System.out.println(joinPoint.getTarget().getClass().getName());
//提交事物
System.out.println("commit");
List<Person> personList = (ArrayList<Person>) val;
for (Person person : personList) {
System.out.println(person.getPname());
}
} public void finalMethod() {
System.out.println("最终通知");
} public void aroundMethod(ProceedingJoinPoint joinPoint) {//环绕通知
try {
System.out.println("around method");
joinPoint.proceed();//调用目标类的目标方法
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 异常通知
*/
public void throwingMethod(Throwable except) {
System.out.println(except.getMessage());
}
}
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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--
1、目标类
2、切面
3、进行aop的配置
(目标接口没有:因为引入到容器是为了实例化对象,接口是不能实现对象的。)
(也没有拦截器的引入,有的只是aop的配置,如上的3、)
-->
<!-- 目标类 -->
<bean id="personDao" class="com.lxk.spring.aop.PersonDaoImpl"/>
<!-- 切面的声明 -->
<bean id="transaction" class="com.lxk.spring.aop.Transaction"/>
<!-- aop配置 -->
<aop:config>
<!--
配置aop的切入点
id 是切入点的标识
expression 为切入点的表达式
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)
throws-pattern?)
modifiers-pattern 修饰符 可选 public private protected
ret-type-pattern 返回类型 必选 * 代表任意类型
declaring-type-pattern 方法的声明类型
name-patterm 方法名称类型
set* 以set开头的所有的方法名称
update* 以update开头的所有的方法名称
param-pattern 参数匹配
(..) 任意多个参数,每个参数任意多个类型
(*,String) 两个参数 第一个是任意类型,第二个是String
(String,*,Integer) 三个参数,第一个是String类型,第二个是任意类型,第三个是Integer类型
throws-pattern 异常的匹配模式
例子:
execution(* cn.itcast.spring.aop.xml.AService.*(..));
cn.itcast.spring.aop.xml.AService下的所有的方法
execution(public * cn.itcast.oa..*.*(..))
返回值为任意类型,修饰符为public,在cn.itcast.oa包及子包下的所有的类的所有的方法
exectuion(* cn.itcast.oa..*.update*(*,String))
返回值是任意类型,在cn.itcast.oa包及子包下所有的以update开头的参数为两个,第一个为任意类型
第二个为String类型的所有类的所有的方法
-->
<aop:pointcut expression="execution(* com.lxk.spring.aop.PersonDaoImpl.*(..))" id="perform"/>
<!-- 配置切面(切面里面配置通知)—— ref 指向声明切面的类 -->
<aop:aspect ref="transaction">
<!-- 前置通知pointcut-ref 引用一个切入点 -->
<aop:before method="beginTransaction" pointcut-ref="perform"/> <!--
后置通知
* returning 目标方法的返回值
* 如果目标方法中有可能存在异常,异常确实发生了,这个时候,后置通知将不再执行
--> <!--<aop:after-returning method="commit" pointcut-ref="perform" returning="val"/>--> <!--
最终通知
* 不能得到目标方法的返回值
* 无论目标方法是否有异常,最终通知都将执行
* 资源的关闭、连接的释放写在最终通知里
-->
<!--<aop:after pointcut-ref="perform" method="finalMethod"/>--> <!--
环绕通知
* ProceedingJoinPoint的proceed方法就是目标对象的目标方法
* 环绕通知可以控制目标对象目标方法执行
-->
<!--
<aop:around method="aroundMethod" pointcut-ref="perform"/>
-->
<!--
异常通知
在异常通知中获取目标方法抛出的异常
-->
<!--<aop:after-throwing method="throwingMethod" pointcut-ref="perform" throwing="except"/>-->
</aop:aspect>
</aop:config>
</beans>
pointcut-ref设置切入点,也就是被选中的bean里面的方法执行的时候,就可以执行对应的切面的方法啦。
也就是说,什么样的地方,会执行切面内部的代码操作。这里设置的是这个impl类里面的所有方法被调用的时候,就会执行切面的方法。
下面有before after around 等等几种类型的切面方法,后面的method 指出这个前置通知还是后置通知,还是什么通知执行的方法的名称。对应于切面类里面的方法。这个是必须存在的,不然关联不到,那就不行啦。
实际开发用的不是这么个配置姿势,实际开发配置页和spring注解实现一样,也就一两行的配置。
Spring AOP 中的 execution切入点指示符。执行表达式格式和实际代码对照,以及参数的理解。
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
?,问号表示可选项。可填可不填。declaring-type是方法全名,只到类,不含方法名,具体就是类路径。看上面的图的对应关系可知。
Spring AOP之xml 配置实现的更多相关文章
- Spring AOP-xml配置
在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...
- Spring AOP基于xml配置实例
SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象.为了更好地理解记忆,这里几下我自己的通俗的理解. 切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法 ...
- spring aop 使用xml方式的简单总结
spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...
- Spring 入门 web.xml配置详解
Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...
- spring之pom.xml配置
spring之pom.xml配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- 这一次搞懂Spring Web零xml配置原理以及父子容器关系
前言 在使用Spring和SpringMVC的老版本进行开发时,我们需要配置很多的xml文件,非常的繁琐,总是让用户自行选择配置也是非常不好的.基于约定大于配置的规定,Spring提供了很多注解帮助我 ...
- Spring IOC-基于XML配置的容器
Spring IOC-基于XML配置的容器 我们先分析一下AbstractXmlApplicationContext这个容器的加载过程. AbstractXmlApplicationContext的老 ...
- Spring Ioc容器xml配置
Spring Ioc容器xml配置基本结构: <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- 【Spring四】AOP之XML配置
AOP:Aspect Oriented Programming 面向切面编程 面向切面编程的核心是动态代理设计模式.请先參见动态代理设计模式笔记. 以Hibernate保存一个对象到数据库为例,因为 ...
随机推荐
- Android中如何搭建一个WebServer
今天终于把老大交代的任务搞完了,感觉收获挺多的,所以就写一篇来记录一下吧,首先还是来看一下,老大们的需求 需求: 希望移动端的用户标识(IMEI)和HTML页面的用户标识(Cookie)连接起来,其中 ...
- h5移动端局部放大效果
首先添加zoom.js (function (root, factory) { if (typeof exports === 'object' && typeof module === ...
- 在delphi中执行javascript代码
有时做项目难免用到代码交叉调用,delphi中执行js就是一种,两种方法可用: 一.使用webbrower,比较麻烦 二.使用ScriptControl,简单方便: 1.首先 uses ComObj; ...
- mongdb 备份还原导入导出
-------------------MongoDB数据导入与导出------------------- 1.导出工具:mongoexport 1.概念: mongoDB中的m ...
- 二叉树总结及部分Lintcode题目分析 1
1. 遍历问题 Preorder / Inorder / Postorder preorder: root left right inorder: left root right postorder: ...
- Apache配置 PHP 支持
1,在服务区安装PHP 解压 php 到纯英文路径目 2,添加 PHP处理模块 LoadModule php7_module C:/ProgramData/php/php7apache2_4.dl 3 ...
- 记录百度编辑器bug(在编辑框输入光标到达页面最底部时,功能区块会悬浮在页面最顶部并且与编辑框分离)
解决方案: //加入这段CSS#edui1_toolbarbox { position: relative !important; }
- 全网最热Python3入门+进阶 更快上手实际开发✍✍✍
全网最热Python3入门+进阶 更快上手实际开发 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问 ...
- add a characteristic in enovia PLM
Problem: add a new Char. name D_COI6 that the description is Injected coloration #7 (COI6) in the D_ ...
- 使用lombok时@Setter @Getter无效
原文链接 : https://blog.csdn.net/marion158/article/details/87893480 lombok是一个帮助简化代码的工具,通过注解的形式例如@Setter ...