Spring Advisor
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的更多相关文章
- 从动态代理到Spring AOP(中)
一.前言 上一章节主要介绍了JDK动态代理和CGLIB动态代理:https://www.cnblogs.com/GrimMjx/p/11194283.html 这一章主要结合我们之前学习的动态代理的基 ...
- Spring源码分析之AOP
1.AOP简介 AOP即面向切面编程(Aspect Oriented Programming),通过预编译方式及运行期动态代理实现程序功能的统一维护的一种技术.使用aop对业务逻辑的各个部分进行隔离, ...
- Spring顾问、IOC注解和注解增强
一.顾问 通知的一种表现方式(顾问包装通知/增强) Advisor: 名称匹配方法: NameMecthMethodPointcutAdvisor 1.定义了一个业务类 package cn.spri ...
- Spring——顾问封装通知
通知(advice)是Spring中的一种比较简单的切面,只能将切面织入到目标类的所有方法中,而无法对指定方法进行增强 顾问(advisor)是Spring提供的另外一种切面,可以织入到指定的方法中 ...
- 【Spring注解驱动开发】AOP核心类解析,这是最全的一篇了!!
写在前面 昨天二狗子让我给他讲@EnableAspectJAutoProxy注解,讲到AnnotationAwareAspectJAutoProxyCreator类的源码时,二狗子消化不了了.这不,今 ...
- Spring框架之AOP源码完全解析
Spring框架之AOP源码完全解析 Spring可以说是Java企业开发里最重要的技术.Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Orie ...
- Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现
前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...
- MarkDown常用格式
常用格式 ** :加粗 <br> : 换行 > :可以用来引用文章,很漂亮. 可以展开的文件夹格式 <details> <summary>框架</sum ...
- Spring-AOP @AspectJ切点函数之@annotation()
@annotation()概述@annotation表示标注了某个注解的所有方法. 下面通过一个实例说明@annotation()的用法. AnnotationTestAspect定义了一个后置切面增 ...
随机推荐
- js同时获得数组的两个最小值
//数组中找两个最小值,及索引 //例如数组: [2,6,7,4,10,3,5]; 计算得出,min1=2,index1=0,min2=3,index2=5; var min1 = Infinity; ...
- (已解决)#warning:尚未配置[微信]URL Scheme:wx4868b35061f87884, 无法使用进行授权。
#warning:尚未配置[微信]URL Scheme:wx4868b35061f87884, 无法使用进行授权. (说白了就是注册白名单) ” -canOpenURL: failed for URL ...
- RMAN备份策略与异机恢复一例
实验环境: A机器(生产用途):RHEL 6.5 + Oracle 11.2.0.4 + IP Address 192.168.1.11 B机器(备机用途):RHEL 6.5 + Oracle 11. ...
- PHP判断ip地址是否合法
1.获取真正ip地址 function get_ip(){ //判断服务器是否允许$_SERVER if(isset($_SERVER)){ if(isset($_SERVER[HTTP_X_FORW ...
- echo 内容显示颜色
一,字体显示颜色 #字体颜色:30m-37m 黑.红.绿.黄.蓝.紫.青.白str=”要显示的字体“echo -e "\033[30m ${str}\033[0m" ## ...
- bat运行时自己隐藏黑框,而不是用vbs来调用自己
//autoStart.bat @echo off if "%1" == "h" goto begin mshta vbscript:createobject( ...
- cmd 修改文件或目录的执行权限
设置当前目录及子目录(/r)下所有文件(/f *)的所有者为管理员(/a) takeown /f * /a /r 我试用如下: takeown /f C:\test /a /r 设置当前目录及子目录下 ...
- CSU 1862 The Same Game(模拟)
The Same Game [题目链接]The Same Game [题目类型]模拟 &题解: 写这种模拟题要看心态啊,还要有足够的时间,必须仔细读题,一定要写一步,就调试一步. 这题我没想到 ...
- python windows环境下安装
下载python安装包,双击安装后, 在cmd中输入python 若无反应, 在cmd设置环境变量 变量 : set PATH=C:\...\...\...[python的编译器的路径]:%PATH% ...
- windows8安装msi或exe软件提示2503错误的解决办法
windows8以后的版本安装msi软件(比如nodejs.msi.Git.msi.python.msi.T ortoiseSVN.msi)的时候老师出现2503.2502的错误,究其原因还是系统权限 ...