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保存一个对象到数据库为例,因为 ...
随机推荐
- ubuntu安装mysql 并对外暴露3306端口
安装 sudo apt-get install mysql-client mysql-server vi /etc/mysql/mysql.conf.d/mysqld.cnf bind 127注掉 m ...
- bootstrap 的 datetimepicker,同时有日期和时间, 且开始时间要早于结束时间
首先,引入jquery, bootstrap 和 bootstrap-datetimepicker datetimepicker的下载地址: http://www.bootcss.com/p/boot ...
- Java-Class-@I:org.springframework.web.bind.annotation.PostMapping
ylbtech-Java-Class-@I:org.springframework.web.bind.annotation.PostMapping 1.返回顶部 2.返回顶部 1. package ...
- PHP面试 PHP基础知识 三(运算符)
PHP运算符 PHP的运算符的错误控制符@ PHP支持一个错误运算符:@.当将其放在一个PHP表达式之前,该表达式可能产生的任何错误信息都将会被忽略掉. PHP运算符 运算符的优先级 着重记忆运算符 ...
- Unity通过Jar包调用Android
Unity通过Jar包调用Android 我们会需要面对下面几个问题,我们一个一个来解决: 怎样在Android Studio中打Jar包 怎样打一个Unity使用的Jar包 怎样在Unity工程中使 ...
- 利用mysql数据库日志文件获得webshell
查看配置 show variables like '%general%'; 开启日志功能 set GLOBAL general_log='ON'; 设置日志存储路径 SET GLOBAL genera ...
- Stack,ArrayDeque,LinkedList的区别
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog 这段时间把疯狂JAVA再看了一遍,发现Stac ...
- 最小生成树--Prim及Kruskal
//prim算法#include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
- Linux基本知识点
压缩和解压类 7.8.1 gzip/gunzip 压缩 1.基本语法 gzip 文件 (功能描述:压缩文件,只能将文件压缩为*.gz文件) gunzip 文件.gz (功能描述:解压缩文件命令) 2. ...
- <人工智能>人工智能基础
问题1:扔下圆球的位置(feature特征变量)变化,最终掉落奖项(label结果标签)的变化 feature ----输入 f(x) ----模型,算法 label ----输出 大量已知的数据,训 ...