和前置通知类似,
通过实现
org.springframework.aop.AfterReturningAdvice来完成后置通知:
public class CarAfterAdvice implements AfterReturningAdvice {
@Override
// returnValue 方法返回值,其它和前置通知相同
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("Welcome to come again");
}
}
Spring配置文件beans-after-advice.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<bean id="car" class="com.john.spring.aop.Car">
<property name="name" value="Superb" />
<property name="price" value="220000" />
</bean>
<bean id="carAfterAdvice" class="com.john.spring.aop.CarAfterAdvice" />
<bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.john.spring.aop.Vehicle</value>
</property>
<property name="interceptorNames">
<list>
<value>carAfterAdvice</value>
</list>
</property>
<property name="target">
<ref bean="car" />
</property>
</bean>
</beans>
测试:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "beans-after-advice.xml" });
Vehicle vehicle = (Vehicle) ctx.getBean("proxyBean");
vehicle.info();
}
输出:
Car name: Superb, price: 220000
Welcome to come again
附:
执行info()时,调用org.springframework.aop.framework.adapter.AfterReturningAdviceAdapter的getInterceptor方法:
public MethodInterceptor getInterceptor(Advisor advisor) {
AfterReturningAdvice advice = (AfterReturningAdvice) advisor.getAdvice(); // 获取后置通知实例
return new AfterReturningAdviceInterceptor(advice);
}
然后调用org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor的invoke方法:
public Object invoke(MethodInvocation mi) throws Throwable {
Object retVal = mi.proceed(); // 执行目标方法
this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis()); // 执行后置通知方法
return retVal;
}
分享到:
相关推荐
spring的Aop中的前置通知,后置通知以及环绕通知简单代码
1、编写切面类,包含权限审核方法和日志记录方法,这两个方法将来会织入到...5、编写案例,运用Spring AOP技术,要求包含前置通知、后置通知、环绕通知、返回通知、异常返回通知。请掌握这五种通知的特点,及应用场景
NULL 博文链接:https://z18022893621.iteye.com/blog/1956146
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
Spring AOP支持不同类型的通知,如前置通知(Before)、后置通知(After)、返回后通知(After-returning)、抛出异常后通知(After-throwing)和环绕通知(Around)。这种机制使得开发者可以在运行时动态地应用这些...
一. 准备工作 二. Spring -Aop入门 三. Spring-Aop 前置通知、后置通知、环绕通知、异常通知实现 四. Spring-Aop 之Pointcut+advice+Advisor 实现 五.Spring-Aop 引入的介绍
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。
Spring spectJ AOP 前置通知 后置通知 返回通知 异常通知 环绕通知
1、spring切入点 2、spring前置织入,传入参数处理 3、spring后置织入,传入参数处理 4、环绕织入,参数及返回值处理 5、返回后织入,返回值处理 6、异常织入,异常处理 maven环境下,测试用例可直接运行
spring的简化aop,前置通知,后置通知的简写
因此, Spring不仅仅能应用于J2EE应用程序之中,也可以应用于桌面应用程序以及小应用程序之中。Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring ...
<aop:after method="after" pointcut-ref="pointCut"/> aop后置通知, <aop:after-throwing method="exception" pointcut-ref="pointCut"/> aop异常通知 以上结合起来意思就是在调用com.spring.service包或子包下的...
springboot配置AOP切面:前置通知、后置通知、环绕通知、返回通知、异常通知
spring框架的aop前置、后置、环绕、异常通知以及自定义切入点实例
AOP程序 Spring框架 前置通知 后置通知 异常通知 环绕通知
NULL 博文链接:https://baobeituping.iteye.com/blog/1208293
基于注解与 XML 配置文件两种形式的 AOP demo。 基于 xml 配置文件的 aop 管理 ... // 在目标方法执行之后之后,相当于后置通知 System.out.println("这是一个后置通知"); return result; } }
Spring AOP 通知种类:设置次要业务与(被监听方法)绑定执行顺序问题 1.前置通知: 1)切面:次要业务方法 2) 执行切入点:被拦截的主要业务方法 2.后置通知: 1)执行切入点:被拦截的主要业务方法 2)切...
本例是一个很好的springAop使用例子,例子举例了spring的前置通知和后置通知的使用方法。用以拦截操作日志。
Spring中Aop的使用!~ 关于前置通知,后置通知,环绕通知,异常通知的使用!~ 简单的demo!~