Spring(十)--Advisor顾问
Spring之Advisor顾问

1. 创建新的xml文件 advisor.xml
<!--01. 配置目标对象 实际肯定是配置UserServiceImpl-->
<bean id="userDaoImpl" class="com.xdf.dao.UserDaoImpl"/> <!--02.配置前置通知-->
<bean id="beforeAdvice" class="com.xdf.advice.BeforeAdvice"/> <!--03.配置工厂-->
<bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标对象-->
<property name="targetName" value="userDaoImpl"/>
<!--配置顾问-->
<property name="interceptorNames" value="myAdvisor"/>
</bean> <!--04.配置顾问myAdvisor-->
<bean id="myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<!--配置通知 通知只是顾问的一个属性 -->
<property name="advice" ref="beforeAdvice"/>
<!--配置切入点-->
<!-- <property name="mappedName" value="eat"/>-->
<property name="mappedNames" value="eat,sleep"/>
</bean>
2. 创建测试类
**
* 使用顾问 advisor.xml
*/
@Test //前置通知
public void testAdvisor(){
ApplicationContext context=new ClassPathXmlApplicationContext("advisor.xml");
UserDao userDao= context.getBean("userProxy",UserDao.class);
userDao.eat();
userDao.sleep();
}
·可以解决 给指定的主业务方法 增强的问题!
3. 使用正则匹配,创建新的xml文件
在Dao层增加 ea()和e()!便于我们测试
<!--01. 配置目标对象 实际肯定是配置UserServiceImpl-->
<bean id="userDaoImpl" class="com.xdf.dao.UserDaoImpl"/> <!--02.配置前置通知-->
<bean id="beforeAdvice" class="com.xdf.advice.BeforeAdvice"/> <!--03.配置工厂-->
<bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标对象-->
<property name="targetName" value="userDaoImpl"/>
<!--配置顾问-->
<property name="interceptorNames" value="myAdvisor"/>
</bean> <!--04.配置顾问myAdvisor RegexpMethodPointcutAdvisor -->
<bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!--配置通知 通知只是顾问的一个属性 -->
<property name="advice" ref="beforeAdvice"/>
<!--配置切入点 使用正则表达式
com.xdf.dao.UserDaoImpl.eat 务必使用类名+方法名
. 代表任意单个字符
* 代表.字符出现的次数是0-N
?:0 -1
+: 1-N
-->
<property name="patterns">
<array>
<!-- <value>.*e.*</value> 匹配 eat 和sleep-->
<!-- <value>com.xdf.dao.UserDaoImpl.ea.?</value>匹配 eat 和ea-->
<value>com.xdf.dao.UserDaoImpl.*e.*</value> <!--匹配 eat 和ea e-->
</array>
</property>
</bean>
<!--还是一个问题没解决 一个工厂只能操作一个对象-->
4. 创建测试类
/**
* 使用顾问 regex.xml
*/
@Test //前置通知
public void testRegex(){
ApplicationContext context=new ClassPathXmlApplicationContext("regex.xml");
UserDao userDao= context.getBean("userProxy",UserDao.class);
userDao.eat();
userDao.ea();
userDao.e();
userDao.sleep();
}
各位亲,这个办法你想到了吗?!接下来的我还会继续更新的哦!
Spring(十)--Advisor顾问的更多相关文章
- 关于spring aop Advisor排序问题
关于spring aop Advisor排序问题 当我们使用多个Advisor的时候有时候需要排序,这时候可以用注解org.springframework.core.annotation.Order或 ...
- 学习 Spring (十五) Advisor
Spring入门篇 学习笔记 advisor 就像一个小的自包含的方面,只有一个 advice 切面自身通过一个 bean 表示,并且必须实现某个 advice 接口,同时 advisor 也可以很好 ...
- Spring 通知和顾问进行增强
使用顾问增加前置增强和后置增强 <bean id="1" class="目标对象"></bean> <bean id=" ...
- 学习 Spring (十六) AOP API
Spring入门篇 学习笔记 Spring AOP API 是 Spring 1.2 历史用法,现在仍然支持 这是 Spring AOP 基础,现在的用法也是基于历史的,只是更简便了 Pointcut ...
- Spring(十)Spring任务调度
一.计划任务 需要定时执行一些计划(定时更新等),这样的计划称之为计划任务 Spring抽象封装了Java提供的Timer与TimerTask类 也可以使用拥有更多任务计划功能的Quartz 二.Ti ...
- 学习 Spring (十四) Introduction
Spring入门篇 学习笔记 Introduction 允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象 由 中的 元素声明该元素用于声明所匹配的类型拥有一个新的 p ...
- 学习 Spring (十二) AOP 基本概念及特点
Spring入门篇 学习笔记 AOP: Aspect Oriented Programming, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 主要功能是:日志记录.性能统计.安全控 ...
- 学习 Spring (十) 注解之 @Bean, @ImportResource, @Value
Spring入门篇 学习笔记 @Bean @Bean 标识一个用于配置和初始化一个由 Spring IoC 容器管理的新对象的方法,类似于 XML 配置文件的 可以在 Spring 的 @Config ...
- Spring(十六)之MVC框架
MVC 框架教程 Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业 ...
随机推荐
- HGOI20190814 省常中互测7
Problem A 中间值 对于$2$个非严格单增序列$\{A_n\} , \{B_n\}$,维护下列两个操作: 1 x y z: (x=0)时将$A_y = z$ , (x=1)时将$B_y = z ...
- Mybatis 结果映射下划线转驼峰
mybatis 结果映射下划线转驼峰 Spring Boot 配置: #下划线转驼峰 mybatis.configuration.map-underscore-to-camel-case=true m ...
- HUD 1166:敌兵布阵(线段树 or 树状数组)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- R_Studio(关联)使用apriori函数简单查看数据存在多少条关联规则,并按支持度降序排序输出
查看数据menu_orders.txt文件存在多少条关联规则,并按支持度降序排序输出 #导入arules包 install.packages("arules") library ( ...
- less基本用法:持续归纳中
todo 1,嵌套语法:https://www.w3cschool.cn/less/nested_directives_bubbling.html 简单来说就是可以与html一样去写css,并且会继承 ...
- php正则表达式的学习
真的发现什么都需要这个 所以还是来把它学了吧 正则表达式的基本知识汇总 行定位符(^与$) 行定位符是用来描述字符串的边界.“$”表示行结尾“^”表示行开始如"^de",表示以de ...
- 「POI 2010」Bridges
题目链接 戳我 \(Solution\) 看到"最大值最小",就知道应该要二分 二分之后,对于每个\(mid\),只要计算小于\(mid\)的边,然后在剩下的图中判断有无欧拉回路 ...
- ztree复选框
var setting = { check: { enable: true // chkboxType : { "Y" : "", "N" ...
- 20175215 2018-2019-2 第四周Java课程学习总结
第五章学习内容 1.子类的继承性 (1)子类和父类在同一包中的继承性 如果子类和父类在同一个包中,那么,子类自然地继承了其父类中不是private的成员变量作为自己的成员变量,并且也自然地继承了父类中 ...
- python3笔记一:python基础知识
一:学习内容 注释 输入输出 标识符 变量和常量 二:注释 1. 单行注释 #:一个井号,代表我注释了这一行 2.多行注释 ''' ''':注释多行,三个单引号 3.多行注释 "" ...