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 模式导致了应用程序的不同方面(输入逻辑.业 ...
随机推荐
- oracle 循环的一种写法
for v_n in( select bb.temNum, bb.LOANTYPE from (select decode(bns.assignstate, '{016D68F9-719B-4EFC- ...
- 总结JavaScript中浏览器的兼容问题
浅析JavaScript中浏览器的兼容问题 浏览器兼容性问题是在实际开发中容易忽略而又最重要的一部分.我们在讲老版本浏览器兼容问题之前,首先要了解什么是能力检测,它是来检测浏览器有没有这种能力,即判断 ...
- Log4net日志文件自动按月份存放和日志独占问题的解决
让log4net日志文件自动按月份存放 log4net日志文件的作用还真不小,可以保存管理员.用户对数据库的任何操作,保存管理员和用户的登录记录,分析系统运行错误,所以不舍得随便将日志文件Delete ...
- 关系型数据库(七),复杂SQL语句
目录 1.SQL语句由六部分组成 2.Group By 和HAVING 七.复杂SQL语句 1.SQL语句由六部分组成 Select *** From *** Where *** Group B ...
- Nginx配置记录【例2】
B服务器,例: [root@localhost conf.d]# egrep -v "^#|^$" /etc/nginx/nginx.conf user nginx; worker ...
- 「HEOI 2016/TJOI 2016」求和
题目链接 戳我 \(Solution\) 先化简式子: \[f(n)=\sum_{i=0}^n\sum_{j=0}^i\begin{Bmatrix} i \\ j \end {Bmatrix}*2^j ...
- WordPress 主题开发:从入门到精通(必读)
本专栏介绍如何开发设计你自己的 WordPress 主题.如果你希望了解更多如何安装和应用主题的内容,请参阅应用主题文档.本文的内容不同于应用主题,因为所讨论的是编写代码去构建你自己的主题的技术内容, ...
- Laravel 在构造方法中使用session
- leetcode-easy-string- 8 String to Integer (atoi)
mycode 98.26% 易错点: while循环式,and判断的地方先判断下标会不会超出范围 class Solution(object): def myAtoi(self, str): &qu ...
- flutter Waiting for another flutter command to release the startup lock…
flutter安装完成后执行flutter doctor ,一直提示如下: Waiting for another flutter command to release the startup loc ...