Spring AOP使用整理:各种通知类型的介绍
2、PersonImpl类的源码
public class PersonImpl implements Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void info() {
System.out.println("\t我叫" + name + ",今年" + age + "岁。");
}
public void show(String message) {
System.out.println(message);
}
}
3、bean的配置
<!-- 目标对象 -->
<bean id="personTarget" class="com.cjm.aop.PersonImpl">
<property name="name" value="Raymond.chen"/>
<property name="age" value="30"/>
</bean>
二、Spring AOP支持的通知类型
一)环绕通知(Around advice)
实现环绕通知需要实现org.aopalliance.intercept.MethodInterceptor接口。
1、PersonAroundAdvice类的源码
public class PersonAroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("AroundAdvice:方法调用前");
//不要忘记调用invocation的proceed方法哦
Object result = invocation.proceed();
System.out.println("AroundAdvice:方法调用后");
return result;
}
}
2、bean配置
<bean id="personAroundAdvice" class="com.cjm.aop.PersonAroundAdvice"/> <!-- 代理工厂bean -->
<bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.cjm.aop.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>personAroundAdvice</value>
</list>
</property>
</bean>
3、测试代码
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:com/cjm/aop/beans.xml");
Person p = (Person)context.getBean("person"); //注意这里是代理工厂Bean的ID
p.info();
二)前置通知(Before advice)
实现前置通知需要实现org.springframework.aop.MethodBeforeAdvice接口。
1、PersonBeforeAdvice类的源码
public class PersonBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("BeforeAdvice:方法调用前");
}
}
2、bean配置
<bean id="personBeforeAdvice" class="com.cjm.aop.PersonBeforeAdvice"/> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.cjm.aop.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>personBeforeAdvice</value>
</list>
</property>
</bean>
三)返回后通知(After Returning advice)
实现返回后通知需要实现org.springframework.aop.AfterReturningAdvice接口。
1、PersonAfterReturningAdvice类的源码
public class PersonAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("AfterReturningAdvice:方法调用后");
}
}
2、bean配置
<bean id="personAfterReturningAdvice" class="com.cjm.aop.PersonAfterReturningAdvice"/> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.cjm.aop.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>personAfterReturningAdvice</value>
</list>
</property>
</bean>
3、以上的配置中,通知对目标对象的所有方法都会起作用。如果需要过滤掉一部分方法,可以用正则表达式切入点配置器或者方法名匹配切入点配置器实现。
<!-- 通知与正则表达式切入点一起配置 -->
<!-- Advisor等于切入点加通知 -->
<!-- 方法名匹配切入点配置器:org.springframework.aop.support.NameMatchMethodPointcutAdvisor -->
<bean id="personPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="personAfterReturningAdvice"/>
<property name="patterns">
<list>
<value>.*info.*</value>
</list>
</property>
</bean> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.cjm.aop.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>personPointcutAdvisor</value>
</list>
</property>
</bean>
四)异常通知(Throws advice)
当连接点抛出异常时,异常通知被调用。实现异常通知需要实现org.springframework.aop.ThrowsAdvice接口,该接口不包含任何方法,但在实现该接口时必须实现如下形式的方法: afterThrowing([Method], [args], [target], Throwable subclass) 可以实现一个或多个这样的方法。在这些方法中,只有第四个参数是必需的,前三个参数可选。
1、PersonThrowsAdvice类的源码
public class PersonThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(FileNotFoundException ex){
System.out.println("ThrowsAdvice >> FileNotFoundException:" + ex.toString());
}
public void afterThrowing(Object[] args, Exception ex){
System.out.println("ThrowsAdvice >> Exception:" + ex.getMessage());
}
public void afterThrowing(Method method, Object[] args, Object target, Throwable ex){
System.out.println("ThrowsAdvice >> Throwable:" + ex.getMessage());
}
}
2、bean配置
<bean id="personThrowsAdvice" class="com.cjm.aop.PersonThrowsAdvice"/> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.cjm.aop.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>personThrowsAdvice</value>
</list>
</property>
</bean>
五)引入通知(Introduction advice)
引入通知是一种特殊的通知,它能将新的成员变量、成员方法引入到目标类中。它不能作用于任何切入点,因为它只作用于类层次,而不是方法层次。实现引入通知需要实现IntroductionAdvisor和IntroductionInterceptor接口。
引入通知不能调用proceed方法。Advisor必须针对每个实例,并且是有状态的。
引入通知的效果类似于设计模式中的访问者模式(Visitor Pattern)。

1、Lockable接口的源码
public interface Lockable {
void lock();
void unlock();
boolean locked();
}
2、LockableImpl类的源码
public class LockableImpl extends DelegatingIntroductionInterceptor implements Lockable {
private boolean locked;
public void lock() {
this.locked = true;
}
public void unlock() {
this.locked = false;
}
public boolean locked() {
return this.locked;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if(this.locked){
throw new RuntimeException("加锁,无法执行");
}
//这里不能调用invocation的proceed方法
//通常不需要改写invoke方法,直接调用父类的该方法即可
return super.invoke(invocation);
}
}
3、PersonIntroductionAdvice类的源码
public class PersonIntroductionAdvice extends DefaultIntroductionAdvisor {
public PersonIntroductionAdvice(){
super(new LockableImpl(), Lockable.class);
}
}
4、bean配置
<!-- Advice必须针对每个实例,所以scope要设为prototype -->
<bean id="personIntroductionAdvice" class="com.cjm.aop.introduction.PersonIntroductionAdvice" scope="prototype"/> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.cjm.aop.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>personIntroductionAdvice</value>
</list>
</property>
</bean>
5、测试代码
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:com/cjm/aop/beans.xml");
//获得目标bean的代理bean
Person p = (Person)context.getBean("person");
//执行代理bean的方法,此时并未调用lock方法,可以执行
p.info();
Lockable lockable = (Lockable)p;
lockable.lock();
//目标bean已被锁定,此处将抛出异常
p.info();
Spring AOP使用整理:各种通知类型的介绍的更多相关文章
- spring aop的五种通知类型
昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...
- spring aop 的五种通知类型
本文转自:http://blog.csdn.net/cqabl/article/details/46965197 spring aop通知(advice)分成五类: 前置通知[Before advic ...
- 十五 Spring的AOP的注解的通知类型,切入点的注解
Spring的注解的AOP的通知类型 @Before:前置通知 @AfterReturning:后置通知 @Around:环绕通知 @AfterThrowing:异常抛出通知 @After:最终通知 ...
- Spring AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下让你不知所措,心想着:管不得很多人都和我说AOP多难多难.当我看进去以后, ...
- JAVA框架 Spring AOP--切入点表达式和通知类型
一:AOP的相关术语: 1)Joinpoint(连接点):所谓的连接点是指那些可以被拦截点,在spring中这些点是指方法.因为在spring中支持方法类型的连接点. 2)Pointcut(切入点): ...
- 分享知识-快乐自己:Spring切入点的表达式和通知类型
1.切入点的表达式 表达式格式: execution([修饰符] 返回值类型 包名.类名.方法名(参数)) 其他的代替: <!-- 完全指定一个方法 --> <!-- <aop ...
- Spring AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)(转)
1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下让你不知所措,心想着:管不得很多人都和我说AOP多难多难.当我看进去以后, ...
- Spring AOP 知识整理
通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...
- 7.Spring切入点的表达式和通知类型
1.切入点的表达式 表达式格式: execution([修饰符] 返回值类型 包名.类名.方法名(参数)) 其他的代替: <!-- 完全指定一个方法 --> <!-- <aop ...
随机推荐
- php模式设计之 注册树模式
在前两篇单例模式和工厂模式后,终于迎来了最后一个基础的设计模式--注册树模式. 什么是注册树模式? 注册树模式当然也叫注册模式,注册器模式.之所以我在这里矫情一下它的名称,是因为我感觉注册树这个名称更 ...
- 工作的思考十五:升职前需要做的准备(TeamLeader)
当一个人在公司的工作年限以及经验的积累到达一个程度的时候,升职其实是件高兴的事,但面临角色的转变需要提前做些准备的. 其实如果你对你的职业规划很清楚的话,那么你就应该在升职之前就会开始进行角色的转换. ...
- 琐碎--选择文件夹(路径)+生产txt格式的log+数据库操作方式
记录日常工作常用到的一些方法: 1 选择文件操作,并将文件的路径记录下来: OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = f ...
- xml基本操作
在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...
- AngularJS开发指南7:AngularJS本地化,国际化,以及兼容IE低版本浏览器
AngularJS本地化,国际化 国际化,简写为i18n,指的是使产品快速适应不同语言和文化. 本地化,简称l10n,是指使产品在特定文化和语言市场中可用. 对开发者来说,国际化一个应用意味着将所有的 ...
- VS快速格式化代码
Ctrl+E,D快速对所有文档进行格式化 Ctrl+E,F快速对选中内容进行格式化
- git托管代码随笔--运用ssh传输,不用每次提交频繁输入github账号密码
遇到问题:在使用git bash的时候 每次git push均要输入账号密码. 问题原因:使用的是http传输,需用ssh传输. 解决方法: 1.设置密钥 ssh-keygen -t rsa -C & ...
- Xamarin.Forms——尺寸大小(五 Dealing with sizes)
如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...
- 使用NHibernate实现存储库
ORM框架不是存储库.存储库是一种架构模式,而ORM是将数据模型表示成对象模型的一种手段. 存储库可以使用ORM来协助充当领域模型和数据模型之间的中介. NHibernate允许在不损坏或只少量损坏领 ...
- sql-in/not in和exists/not exists的区别
In和Exists的区别 这两个函数是差不多的,但由于优化方案不同,通常NOT Exists要比NOT IN要快,因为NOT EXISTS可以使用结合算法二NOT IN就不行了,而EXISTS则不如I ...