1.名称匹配方法切入点顾问

接口:ISomeService

public interface ISomeService {
public void doSome(); public void doSecont(); }

public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log=========="); }
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} public void doSecont() {
System.out.println("=================Secont================"); } }

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--前置通知 名称匹配方法切入点顾问-->
<!--.目标对象-->
<bean id="someService" class="cn.happy.spring08aop_Advice.SomeService"></bean>
<!--.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring08aop_Advice.MyBeforeAdvise"></bean> <!--.前置增强(顾问)-->
<bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="mappedNames" value="beforeAdvisor"></property>
</bean>
<!--.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property>
<property name="proxyTargetClass" value="true"></property>
</bean>
</beans>

单测

//1.前置通知 名称匹配方法切入点顾问
@Test
public void test01(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext05_aop05_Advice.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
service.doSecont();
}

2.正则表达式 匹配方法切入点顾问

接口:ISomeService

public interface ISomeService {
public void doSome(); public void doSecont();
}

public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log=========="); }
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} public void doSecont() {
System.out.println("++===================Secont====================++"); } }

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--正则表达式 匹配方法切入点顾问-->
<!--.目标对象-->
<bean id="someService" class="cn.happy.spring09aop_AdviceZenz.SomeService"></bean>
<!--.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring09aop_AdviceZenz.MyBeforeAdvise"></bean> <!--.前置增强(顾问)-->
<bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="pattern" value=".*d.*"></property>
</bean>
<!--.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvisor"></property> </bean>
</beans>

单测

 //2.正则表达式 匹配方法切入点顾问
@Test
public void test02(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext06_aop06_Zenz.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
service.doSecont();
}

Spring 顾问的更多相关文章

  1. Spring顾问、IOC注解和注解增强

    一.顾问 通知的一种表现方式(顾问包装通知/增强) Advisor: 名称匹配方法: NameMecthMethodPointcutAdvisor 1.定义了一个业务类 package cn.spri ...

  2. Spring——顾问封装通知

    通知(advice)是Spring中的一种比较简单的切面,只能将切面织入到目标类的所有方法中,而无法对指定方法进行增强 顾问(advisor)是Spring提供的另外一种切面,可以织入到指定的方法中  ...

  3. spring顾问包装通知

    前边说到了顾问就是通知,没有实践,这里就实践一下,证明一下. 虽然可以说顾问就是通知,但是他们还是有的一定的区别的,通知是将目标类中所有的方法都进行的增强,而顾问却可以指定到特定的方法上,也就是说顾问 ...

  4. spring增强

    1.前置增强 接口:ISomeService public interface ISomeService { public void doSome(); } 类 public class MyBefo ...

  5. Spring 通知(Advice)和顾问(Advisor)

    AOP ( Aspect  Oriented Programming  面向切面编程)  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...

  6. Spring中的通知(Advice)和顾问(Advisor)

    在Spring中,目前我学习了几种增强的方式,和大家分享一下 之前的话: 1.AOP  (Aspect  Oriented Programming  面向切面编程) 在软件业,AOP为Aspect O ...

  7. SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- advice 是通知advisor 是顾问 顾问(Advisor) 通知Advice是Spring提供的一种切 ...

  8. Spring 通知和顾问进行增强

    使用顾问增加前置增强和后置增强 <bean id="1" class="目标对象"></bean> <bean id=" ...

  9. Spring笔记07(Spring AOP的通知advice和顾问advisor)

    1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...

随机推荐

  1. 【Matlab】调试基础

    1.matlab 调试子程序 在主程序进入子程序前一句加断点,然后step in,可以进入子程序. 但是直接在子程序里设置断点,运行主程序是不能进入子程序的.

  2. 【Lintcode】159.Find Minimum in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  3. 消息队列:快速上手ActiveMQ消息队列的JMS方式使用(两种模式:Topic和Queue的消息推送和订阅)

    1.实现功能 希望使用一套API,实现两种模式下的消息发送和接收功能,方便业务程序调用 1.发送Topic 2.发送Queue 3.接收Topic 4.接收Queue 2.接口设计 根据功能设计公共调 ...

  4. [转]BFC 神奇背后的原理

    BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...

  5. spring-data-cassanra的简单使用

    之前写了JAVA操作cassandra驱动包,现在来看看spring-data对cassandra的支持.这里是spring-data-cassandra的官方文档:http://docs.sprin ...

  6. Poco 编译mysql

    POCO mysql需要自己添加connecter的header和lib MySQL Client: For the MySQL connector, the MySQL client librari ...

  7. Java类加载原理解析(转)

    1 基本信息 每个开发人员对java.lang.ClassNotFoundExcetpion这个异常肯定都不陌生,这背后就涉及到了java技术体系中的类加载.Java的类加载机制是技术体系中比较核心的 ...

  8. ubuntu12.04+virtualbox+winxp的关于摄像头无法使用,声音出不来的问题

    前天在ubuntu上安装了个virtualbox的虚拟机.以前在windows下面是用的vmware.结果到了ubuntu下面折腾半天用不了,于是就装了个virtualbox,在virtualbox里 ...

  9. Git的使用 强制放弃本地所有修改,获取master中最新版本更新本地

    git fetch --all git reset --hard origin/master git fetch --all 的意思是,下载远程库的所有内容,但不与本地做任何合并 git reset ...

  10. L2-013. 红色警报 (并查集)

    战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...