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();
}

    各位亲,这个办法你想到了吗?!接下来的我还会继续更新的哦!

最新文章

  1. Symbols
  2. ibeacon的使用和应用场景简单示例
  3. Ext.GridPanel 用法总结(一)—— Grid基本用法
  4. 08_Queue(队列UVa 10128)
  5. hiho_1055_刷油漆
  6. bug_ _java.lang.IllegalArgumentException: View not attached to window manager
  7. Unity Flow distort of screen
  8. sqlmap新手注入
  9. nginx 支持pathinfo
  10. PyQt4 的部件 -- CheckBox 单选框
  11. Valgrind与内存问题
  12. python中base64编码与解码
  13. 《剑指offer》第五十九题(队列的最大值)
  14. delphi Drag and Drop sample 鼠标拖放操作实例
  15. 八大排序算法的Java代码实现
  16. 项目报错:Caused by: java.lang.ClassNotFoundException: Didn&#39;t find class &quot;...&quot;on path: DexPathList
  17. WPF ICommandSource Implementations Leak Memory!
  18. IEEEXtreme 极限编程大赛题解
  19. Codeforces441A_Valera and Antique Items(水题)
  20. hdfs源码分析第一弹

热门文章

  1. JS 全局作用域和局部作用域
  2. 在angular中使用ng-repeat时数组中有重复元素,要用item in items track by $index
  3. C语言 - strcmp和strncmp的编程实现及总结
  4. 暑假集训 #3div2 C Sequence 数字找规律
  5. 暑假集训 div1 B Derangement 交换数字 思维死角
  6. CentOS7 服务器上如何安装python3
  7. netty实现客户端服务端心跳重连
  8. CodeForces 352C Jeff and Rounding
  9. 13.Python字符串详解(包含长字符串和原始字符串)
  10. nginx+uWSGI+django+virtualenv+supervisor发布web服务器流程