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. zero to one (4)

    复盘--天下武功唯快不破 There is no martial art is indefectible, while the fastest speed is the only way for lo ...

  2. 「LOJ#6121」「网络流 24 题」孤岛营救问题(BFS

    题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到了迷宫的地形图.迷宫的外形是一个长方形 ...

  3. NodeJS测试实例

    实例一: 先来个简单的实例,把下面的代码保存为main.js,让自己欣喜下: var http = require("http"); function onRequest(requ ...

  4. Thrift之TProcess类体系原理及源码详细解析

    我的新浪微博:http://weibo.com/freshairbrucewoo. 欢迎大家相互交流,共同提高技术. 之前对Thrift自动生成代码的实现细节做了详细的分析,下面进行处理层的实现做详细 ...

  5. RMAN兼容性、控制文件自动备份、保存时间、备份策略、备份脚本(二)

    RMAN 程序的兼容性 RMAN 环境由以下5部分组成:(1) RMAN executable(2) Recovery catalog database(3) Recovery catalog sch ...

  6. poj1325机器工作——二分图最小点覆盖

    题目:http://poj.org/problem?id=1325 二分图求最大匹配,即为最小点覆盖: 一开始我写得较麻烦,求出最大匹配又去搜增广路,打标记求最小点覆盖: 然而两种方法都没写“ans= ...

  7. 洛谷P1220关路灯——区间DP

    题目:https://www.luogu.org/problemnew/show/P1220 区间DP. 代码如下: #include<iostream> #include<cstd ...

  8. mysql错误-修改mysql.sock位置

    在Mysql下有时候会出现mysql.sock位置错误,导致无法链接数据库. mac下报错的时候: 首先修改my.cnf 位置在/etc/my.cnf下,假如没有的话,去/usr/locate/mys ...

  9. POJ 3258 最小值最大化 二分搜索

    题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值. ...

  10. css3 实现运动动画 圆与椭圆

    圆: html <div class="demo4"><div></div></div> css .demo4{ width: 20 ...