SpringAdvisor 顾问:在通知的基础之上,在细入我们的切面AOP

通知和顾问都是切面的实现方式

通知是顾问的一个属性

顾问会通过我们的设置,将不同的通知,在不同的时间点把切面织入不同的切入点。

PointCutAdvisor接口!

比较常用的两个实现类

1 根据切入点(主业务方法)名称  织入切面NameMatchMethodPointCutAdvisor

2 根据自定义的正则 表达式织入切面 RegexpMethodPointoutAdvisor

接口

public interface UserService {
public void addUser();
public String eat();
}

实现类

public class UserServiceImpl implements UserService {
public void addUser() {
System.out.println("添加用户");
} public String eat() {
System.out.println("eat a little apple");
return "apple";
}
}

前置增强

public class BeforeAdvice implements MethodBeforeAdvice {
/**
*
* @param method 目标方法
* @param objects 目标方法的参数列表
* @param o 目标对象
* @throws Throwable
*/
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强---------------");
}
}

后置增强

public class AfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("后置增强");
}
}

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--目标对象-->
<bean id ="userservice" class="cn.kitty.service.UserServiceImpl"></bean>
<!--配置前置通知-->
<bean id="beforeAdvice" class="cn.kitty.service.BeforeAdvice"></bean>
<!--配置后置通知-->
<!--<bean id="afterAdvice" class="cn.kitty.service.AfterAdvice"></bean>-->
<!--配置顾问 根据名称-->
<!--<bean id="Myadvisors" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="mappedNames" value="addUser"></property>
配置切入点
单个方法使用mappedName 多个方法使用mappedNames
 
</bean>-->  <!--配置顾问 自定义的配置根据正则表达式织入切面-->  <bean id="Myadvisors" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="pattern" value=".*a.*"></property> </bean>

配置切入点
单个表达式使用pattern
多个表达式使用patterns <!-- 配置代理工厂bean 生成代理类 把通知织入到目标对象-->
<bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--注册目标对象-->
<property name="target" ref="userservice"></property>
<!--注册顾问 -->
<property name= "interceptorNames" value="Myadvisors"></property>

</bean> </beans>

test 测试类

public class Test1011 {
@Test
public void aadvisor(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService service= (UserService) context.getBean("userProxy");
service.addUser();
}
}

Spring Advisor的更多相关文章

  1. 从动态代理到Spring AOP(中)

    一.前言 上一章节主要介绍了JDK动态代理和CGLIB动态代理:https://www.cnblogs.com/GrimMjx/p/11194283.html 这一章主要结合我们之前学习的动态代理的基 ...

  2. Spring源码分析之AOP

    1.AOP简介 AOP即面向切面编程(Aspect Oriented Programming),通过预编译方式及运行期动态代理实现程序功能的统一维护的一种技术.使用aop对业务逻辑的各个部分进行隔离, ...

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

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

  4. Spring——顾问封装通知

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

  5. 【Spring注解驱动开发】AOP核心类解析,这是最全的一篇了!!

    写在前面 昨天二狗子让我给他讲@EnableAspectJAutoProxy注解,讲到AnnotationAwareAspectJAutoProxyCreator类的源码时,二狗子消化不了了.这不,今 ...

  6. Spring框架之AOP源码完全解析

    Spring框架之AOP源码完全解析 Spring可以说是Java企业开发里最重要的技术.Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Orie ...

  7. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

  8. MarkDown常用格式

    常用格式 ** :加粗 <br> : 换行 > :可以用来引用文章,很漂亮. 可以展开的文件夹格式 <details> <summary>框架</sum ...

  9. Spring-AOP @AspectJ切点函数之@annotation()

    @annotation()概述@annotation表示标注了某个注解的所有方法. 下面通过一个实例说明@annotation()的用法. AnnotationTestAspect定义了一个后置切面增 ...

随机推荐

  1. vue 常用问题

    缺少style-loader 或者 stylus-loader 等问题 在文件[package.json]分支:[devDependencies]添加 版本号: "stylus": ...

  2. Mysql表中唯一编号的分配机制

    最近遇到一个问题:高并发环境下,如何避免MYSQL一张表里的某些列不要重复. 同其他博友一样 https://blog.csdn.net/jacketinsysu/article/details/51 ...

  3. Spark Worker启动Driver和Executor工作流程

    二:Spark Worker启动Driver源码解析 case LaunchDriver(driverId, driverDesc) => { logInfo(s"Asked to l ...

  4. MySQL数据库之part1

    一.初始数据库 链接:http://www.cnblogs.com/linhaifeng/articles/7126847.html 一.MySQL介绍 1.MySQL是什么 MySQL是一个关系型数 ...

  5. Java 基础 常用API ( 正则表达式,Date类,DateFormat类,Calendar类 )

    正则表达式 正则表达式的概念 正则表达式(英语:Regular Expression,在代码中常简写为regex). 正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个 ...

  6. haproxy json 自定义格式日志 并用rsyslog 收集

    haproxy的配置 日志投送: global log 127.0.0.1 local1 info 默认端口是udp514 默认日志: defaults log global log-format \ ...

  7. 产品设计教程:利用“系列位置效应”优化UI

    任何博得人们喜欢的产品都在一定程度上契合了用户的心理需求.设计和心理学息息相关,掌握一些基本心理学知识,设计师的作品更能在潜意识中抓住用户的心. 系列位置效应 “系列位置效应”(The Serial ...

  8. 隐藏apache服务器信息

    安装完apache一般第一时间都是关闭apache的版本信息,黑客会通过apache暴露出来的信息针对性的入侵,为了服务器的安全这些信息一定要及时关闭. 1.隐藏PHP版本 修改php.ini exp ...

  9. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

  10. 从零开始一起学习SLAM | SLAM有什么用?

    SLAM是 Simultaneous Localization And Mapping的 英文首字母组合,一般翻译为:同时定位与建图.同时定位与地图构建. 「同时定位与地图构建」这几个词,乍一听起来非 ...