Spring AOP之Advice
前置通知:即代码被执行之前被调用
后置通知:即代码执行之后进行调用
环绕通知:即代码执行前后进行调用
这两种方式的实现方式用到了动态代理的思想来完成的,
总结不想说废话:
首先是Spring1.x对Advice的支持:
public interface IHello { public void sayHello1() ; public void sayHello2() ; public void sayHello3() ; public void sayHello4() ; }
然后就是类的实现:
package com.spring.test; public class Hello implements IHello{ public void sayHello1() { System.out.println("111111111111"); } public void sayHello2() { System.out.println("222222222222"); } public void sayHello3() { System.out.println("333333333333"); } public void sayHello4() { System.out.println("444444444444"); } }
然后是负责前置通知的类,要实现MethodBeforeAdvice接口:
public class AdviceBeforeHello implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("验证用户。。。。"); } }
然后是负责后置通知的类,要实现AfterReturningAdvice接口:
public class AdviceAfterHello implements AfterReturningAdvice{ @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("方法执行完毕"); } }
配置项: <bean id="beforeHello" class="com.spring.advice.AdviceBeforeHello" /> <bean id="afterHello" class="com.spring.advice.AdviceAfterHello"></bean> <bean id="hello" class="com.spring.test.Hello"></bean> <!-- 注册代理类 --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.spring.test.IHello</value> </property> <!-- 目标对象,即Hello对象 --> <property name="target" ref="hello"></property> <!-- 应用的前置通知,拦截器名称 --> <property name="interceptorNames"> <list> <value>beforeHello</value> <value>afterHello</value> </list> </property> </bean>
测试方法:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); IHello hello = (IHello) context.getBean("proxy"); hello.sayHello1(); hello.sayHello2(); hello.sayHello3(); hello.sayHello4();
结果:
下面是环绕通知:
public class AdviceAroundHello implements MethodInterceptor{ @Override public Object invoke(MethodInvocation arg0) throws Throwable { Object object=null; System.out.println("验证用户"); try { object=arg0.proceed(); } finally{ System.out.println("方法执行完毕"); } return object; } }
然后配置beans.xml:
<bean id="aroundHello" class="com.spring.advice.AdviceAroundHello"></bean> <bean id="hello" class="com.spring.test.Hello"></bean> <!-- 注册代理类 --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.spring.test.IHello</value> </property> <!-- 目标对象,即Hello对象 --> <property name="target" ref="hello"></property> <!-- 应用的前置通知,拦截器名称 --> <property name="interceptorNames"> <list> <value>aroundHello</value> </list> </property> </bean>
以上是Spring1.x对AOP的支持,,Spring2.x除了支持Spring1.x外,还提供了两种实现AOP的方式:
1.基于XML的配置,使用基于Schema的XML配置来完成AOP而且Advice也不用再实现任何其他特定接口。
2.使用JDK5的注释来完成AOP的实现,只需要一个简单的标签就可以完成AOP的整个过程。
基于XML Schema的前置通知配置如下:
编写通知的逻辑代码:
public class AdviceBeforeHello { public void beforeHello(){ System.out.println("验证用户。。。。"); } }
在beans.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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册接口实现类 --> <bean id="hello" class="com.spring.test.Hello"></bean> <aop:config> <!-- --> <aop:pointcut id="beforePointCut" expression="execution(* com.spring.test.IHello.*(..))" /> <aop:aspect id="before" ref="beforeAdvice"> <aop:before method="beforeHello" pointcut-ref="beforePointCut" /> </aop:aspect> </aop:config> <bean id="beforeAdvice" class="com.spring.advice.AdviceBeforeHello"></bean> </beans>
其他不变。。。。
2.基于Annotation的前置通知:
切面类:
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class AdviceBeforeHello { @Before("execution(* com.spring.test.IHello.*(..))") public void beforeHello(){ System.out.println("验证用户。。。。"); } }
beans.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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册接口实现类 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="hello" class="com.spring.test.Hello"></bean> <bean id="beforeAdvice" class="com.spring.advice.AdviceBeforeHello"></bean> </beans>
基于XML和Annotation的后置通知和前置通知类似。下面是基于XML的环绕通知:
import org.aspectj.lang.ProceedingJoinPoint; public class AdviceAroundHello { public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("验证用户"); //执行连接点的方法 Object object = joinPoint.proceed(); System.out.println("方法执行完毕"); return object; } }
beans.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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册接口实现类 --> <bean id="hello" class="com.spring.test.Hello"></bean> <aop:config> <aop:pointcut id="aroundPointcut" expression="execution(* com.spring.test.IHello.*(..))" /> <aop:aspect id="around" ref="aroundAdvice"> <aop:around method="around" pointcut-ref="aroundPointcut" /> </aop:aspect> </aop:config> <bean id="aroundAdvice" class="com.spring.advice.AdviceAroundHello"></bean> </beans>
其他不变。
-------------------------------------------------------------------------------------------------------------------------------
基于Annotation的环绕通知:
package com.spring.advice; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class AdviceAroundHello { @Around("execution(* com.spring.test.IHello.*(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("验证用户"); // 执行连接点的方法 Object object = joinPoint.proceed(); System.out.println("方法执行完毕"); return object; } }
beans.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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册接口实现类 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="hello" class="com.spring.test.Hello"></bean> <bean id="aroundAdvice" class="com.spring.advice.AdviceAroundHello"></bean> </beans>
完成!!
相关推荐
描述一下Spring AOP?...Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介(Introduction)是什么? 连接点(Joint Point)和切入点(Point Cut)是什么? 织入(Weaving)是什么?
Spring AOP简单demo 入门级的 advice
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
Source 博文链接:https://icz.iteye.com/blog/186074
SpringAOP Spring AOP(面向方面的编程)用于模块化“横截面”服务。 用一种简单的方式,我们可以说它是一个旨在拦截某些进程的组件,例如,在执行某个方法时,Spring AOP可以审核该执行方法,并在该方法执行...
演示了spring对aop的before、after、throw、around几种advice的api操作。
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
一. 准备工作 二. Spring -Aop入门 三. Spring-Aop 前置通知、后置通知、环绕通知、异常通知实现 四. Spring-Aop 之Pointcut+advice+Advisor 实现 五.Spring-Aop 引入的介绍
Spring AOP是基于代理模式实现的,主要包括动态代理、通知(Advice)、切点(Pointcut)、切面(Aspect)和连接点(Join point)。动态代理是实现AOP的基础,它通过JDK动态代理或CGLIB代理生成被代理对象的子类。...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Spring framework是很有前途的AOP技术。作为一种非侵略性的,轻型的AOP framework,你无需使用预编译器或其他的元标签,便可以在Java程序中使用它。这意味着开发团队里只需一人要对付AOP framework,其他人还是象...
1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等(基于代理的AOP ) 2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象 3. 通过<aop:...
org.aopalliance.aop.Advice.jar net.springsource.cglib-2.2.0.jar
(3)Advice(通知):AOP在特定的切入点上执行的增强处理,有before,after,afterReturning,afterThrowing,around (4)Pointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式 (5)AOP代理:AOP框架...
网上找了很多例子多数都不能运行。包括after,before,round,throw切入,可对类直接拦截,不需要定义接口文件,自己研究了2天整理出来的。 Spring AOP 的完整例子,可直接运行。带jar包。
上文我们已讲完五种通知Advice注解,所以从本文开始介绍@Pointcut切点表达式,虽然Spring AOP的切点表达式尚未 全部实现 @AspectJ 的切点指示符,但是也已经足够多和灵活,本文主要介绍@Pointcut切点表达式中的@...
Spring AOP 学习记录 AOP(面向切面编程)方面的知识又是看了忘忘了看,今天有空记录下AOP的知识点。主要分为以下几方面: 1、AOP相关术语 2、基础知识及示例 3、增强分类 1、AOP相关术语 连接点...
通过模拟SpringAOP,设置四个Advice实现简单的切面编程