SpringAOP中的aop:config标签
我们使用Spring的AOP功能的时候发现,我们使用普通的配置方式的时候,我们无法精确的确定将切面类中的哪个方法切入到哪个切入点上,
所以我们可以使用aop的专用标签来完成相关的配置.其中主要表现是使用AspectJ的expression的操作
aop:config标签
使用aop的专用标签来完成相关的配置.其中主要表现是使用AspectJ的expression的操作:
修改模式 类型 说明式 名称模式(参数模式)
execution(modifiers-pattern ret-type-pattern declaring-type-pattern name-pattern(param-pattern)
抛出模式
throws-pattern)除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的。
返回类型模式决定了方法的返回类型必须依次匹配一个连接点。
你会使用的最频繁的返回类型模式是 *,它代表了匹配任意的返回类型。
一个全称限定的类型名将只会匹配返回给定类型的方法。
名字模式匹配的是方法名。 你可以使用 * 通配符作为所有或者部分命名模式。
参数模式稍微有点复杂:() 匹配了一个不接受任何参数的方法, 而 (..) 匹配了一个接受任意数量参数的方法(零或者更多)。
模式 (*) 匹配了一个接受一个任何类型的参数的方法。
模式 (*,String) 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是String类型
注意在使用之前需要在xml文件的beans标签中加入新的schame文件:并在Eclipse中进行关联配置
- <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"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
下面给出一些常见切入点表达式的例子。
1)任意包下的任意类中的公共方法的执行:
execution(public * *(..))
2)任何一个以“set”开始的方法的执行:
execution(* set*(..))
3)AccountService 接口的任意方法的执行:
execution(* com.briup.service.AccountService.*(..))
4)定义在service包里的任意方法的执行:
execution(* com.briup.service.*.*(..))
5)定义在service包以及子包里的任意方法的执行:
execution(* com.briup.service..*.*(..))
execution(* com.briup.service..*.*(..)) or execution(* com.briup.dao..*.*(..))
注意:
1.从spring容器中拿代理对象的时候也是要用目标对象的名字来拿。
2.没有实现任何接口的目标对象也能产生代理对象。
<!-- 配置aop的代理 -->
<!--注意:<aop:config proxy-target-class="true"> 如果这样配置则是强制使用CGLIB方式进行代理-->
<aop:config>
<!-- 定义一个切入点 并给切入点起名为myPointCut -->
<!-- 切入点是一组连接点的集合 -->
<aop:pointcut expression="execution(public * com.briup.aop.service.*.*(..))" id="myPointCut"/>
<!-- 定义哪一个advice在哪一个切入点上面起作用 -->
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="myPointCut" />
</aop:config>
<!--
expression="execution(public * com.briup.aop.service.*.*(..))"
这个引号""里面就是用表达式的方式来定义切入点,只要是符合我们这个表达式要求的
方法就是我们的连接点,连接点的集合就是我们要定义的切入点。
表达式中从左到右的*号:
第一个* 表示方法的返回类型不限。
第二个* 表示包中的任意一个类
第三个* 表示类中的任意一个方法
同时方法的参数也没有限制.
-->
在一个切面类中定个多个方法,根据xml文件的配置每个方法都可以织入到切入点的不同位置,并且advice是在aop的标签中进行配置,不需要再写对应的advice类了
例如:
这个类相当于我们之前的切面类
只不过这个切面类中有很多方法都可以织入到切入点上面
我们可以控制把这里的任何一个方法织入到任何一个切入点上面
- public class XmlHandler {
- public void beforeTest(JoinPoint p){
- System.out.println(p.getSignature().getName()+" before...");
- }
- public void afterTest(JoinPoint p){
- System.out.println(p.getSignature().getName()+" after...");
- }
- public void afterReturningTest(JoinPoint p){
- System.out.println(p.getSignature().getName()+" afterReturning");
- }
- //在和aroundAdvice结合的时候,这个方法一定要加上这个ProceedingJoinPoint类型的参数
- public Object aroundTest(ProceedingJoinPoint pjp)throws Throwable{
- //JoinPoint对象不能调用连接点所表示的方法
- //ProceedingJoinPoint能调用连接点所表示的方法 pjp.proceed()
- System.out.println(pjp.getSignature().getName()+" is start..");
- //调用到连接点方法
- Object obj = pjp.proceed();
- System.out.println(pjp.getSignature().getName()+" is end..");
- return obj;
- }
- public void throwingTest(JoinPoint p,Exception ex){
- System.out.println(p.getSignature().getName()+" is throwing..."+ex.getMessage());
- }
- }
xml文件配置:
- <!-- 配置dao层对象 -->
- <bean id="dao"
- class="com.briup.aop.dao.AccountDaoImpl"/>
- <!-- 配置目标对象 -->
- <bean name="target" class="com.briup.aop.service.AccountServiceImpl">
- <property name="accountDao" ref="dao"></property>
- </bean>
- <!-- 配置切面类 -->
- <bean name="handler" class="com.briup.aop.xml.XmlHandler"></bean>
- <!-- 配置aop的代理 -->
- <aop:config>
- <!-- 定义切入点名为myPointCut -->
- <aop:pointcut expression="execution(public * com.briup.aop.service.*.*(..))" id="myPointCut"/>
<!-- 定义切面类 以及需要使用的advice -->- <aop:aspect id="aspect" ref="handler">
- <!-- 表示beforeAdvice会把切面类handler中的beforeTest方法织入到名字叫myPointCut的切入点上面 -->
- <aop:before method="beforeTest" pointcut-ref="myPointCut"/>
- <!-- after表示不管方法是否正常结束都会起作用 -->
- <aop:after method="afterTest" pointcut-ref="myPointCut"/>
- <!-- after-returning表示方法正常结束才会起作用(抛异常时候不起作用) -->
- <aop:after-returning method="afterReturningTest" pointcut-ref="myPointCut"/>
- <aop:around method="aroundTest" pointcut-ref="myPointCut"/>
- <!-- throwing="ex"表示throwingTest方法中接收异常对象的名字一定要是ex -->
- <aop:after-throwing method="throwingTest" pointcut-ref="myPointCut" throwing="ex"/>
- </aop:aspect>
- </aop:config>
注意:<aop:config proxy-target-class="true"> 如果这样配置则是强制使用CGLIB方式进行代理
SpringAOP中的aop:config标签的更多相关文章
- Spring AOP Schema aop:config、tx:advice
Spring AOP Schema aop:config.tx:advice 一. 利用aop:config标签实现AOP 首先看个例子,如下 接口代码: package com.lei. ...
- spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- Spring中基于AOP的XML架构
以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/xml-schema-based-aop-wi ...
- spring5 源码深度解析----- AOP的使用及AOP自定义标签
我们知道在面向对象OOP编程存在一些弊端,当需要为多个不具有继承关系的对象引入同一个公共行为时,例如日志,安全检测等,我们只有在每个对象里引入公共行为,这样程序中就产生了大量的重复代码,所以有了面向对 ...
- Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...
- Spring 中基于 AOP 的 XML架构
Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...
- .Net中的AOP系列之《方法执行前后——边界切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 边界切面 PostSharp方法边界 方法边界 VS 方法拦截 ASP.NET HttpModule边界 真实案例--检查是否为移动端用 ...
- 转-Spring Framework中的AOP之around通知
Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...
随机推荐
- 60 cuda全局性能优化
0 引言 cuda线程模型涉及grid的块划分和线程配置,直接影响到全局运算速度.根据文档<CUDA_C_Programming_Guide>,性能优化有三个方面的基本策略. (1)最大化 ...
- [JZOJ 5861] 失意
思路: 求交集最大老套路,排序之后用堆维护即可. #include <bits/stdc++.h> using namespace std; const int mod = 1e9+7; ...
- 关于“回归自然”onepage的总结
(1)消除li 前面的点 使用 ul {list-style:none; } 并且ul之外会有一个容器,nav等 利用margin值保持和其他元素的等高度. (2) <h1>回归自然< ...
- STM32F103
memory map • Four masters: – Cortex® -M3 core DCode bus (D-bus) and System bus (S-bus)– GP-DMA1 & ...
- 剑指offer——05重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- Day 7 :一句话Python(匿名函数-lambda,三元运算,列表表达式,生成器表达式)
注意: 1.所有的列表表达式都可以转换成生成器表达式 2.经量让标傲世简化你得操作,增加代码可读性 3.如果代码过于复杂,应该转换成普通代码 4.再代码中尽可能多使用生成器表达式. 三元运算符:简化代 ...
- java 8 lambda函数
1 为什么要引进lambda函数 可以简化编码,将事情更多的交给编译器,让编译器帮我们推断我们写的代码的完整形式. 2 lambda函数的语法 2.1 -> (arg1, arg2) -> ...
- IntelliJ IDEA创建springboot项目
1.创建新项目. 2. 3.Group 是包名,Artifact是项目名. 4.springboot版本尽量选择最高版本,且不要选择SNAPSHOP版本. 5.路径可自定义,默认为D://IDEA/M ...
- RHEL7中网卡绑定team和bond的区别
red hat 官方给出的team和bond特性对比 A Comparison of Features in Bonding and Team Feature Bonding Team broadca ...
- 2018北京网络赛 G The Mole /// 分块暴力 点线距离
题目大意: 给定n段线段 编号为1~n 接下来m个询问 给定一点 输出离该点最近的线段的最小编号(距离相等时取编号小的) 题解 大致就是 1.坐标范围为(0,2^16-1) 将坐标系划分为2^8*2^ ...