面向切面的Spring
来源:互联网 发布:java开源b2b2c商城 编辑:程序博客网 时间:2024/04/30 10:25
- 什么是切面编程
1.定义AOP术语
横切关注点(cross-cuting concern):散布于应用中多处的功能被称为横切关注点。把横切关注点与业务逻辑相分离正是面向切面编程(AOP)所要解决的问题。
切面(Aspect):切面是通知和切点的结合。通知和切点共同定义了切面的全部内容-它是什么,在何时和何处完成其功能。
通知(Advice):切面的工作被称为通知。通知定义了切面是什么时候以及何时调用。除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题。它应该是在方法调用之前? 之后?之前和之后?或者是异常时调用。前置通知(Before)、后置通知(After)、返回通知(After-returning)、异常通知(After-throwing)、环绕通知(Around)应用被调用钱调用后执行自定义的行为。
连接点(Join point):连接点是在应用执行之中能够插入切面的一个点。这个点可以使调用方法时、抛出异常时、甚至修改一个字段时。切面代码可以利用该点插入到应用的正常流程之中,并添加新的行为。
切点(Poincut):定义了切面在何处。切点的定义会匹配通知所要织入的一个或多个连接点。我们通常使用吗,明确的类和方法名称,或者是利用正则表达式所匹配的类和方法名称来指定这些切点。
引入(Introduction):允许向现有的类添加新的方法或者属性。
织入(Weaving):织入是把切面应用到目标对象并创建新的代理对象的过程。切面在指定的连接点被织入到目标对象中。在目标对象的生命周期里有对个点可以进行织入。
编译期:切面在目标类编译时被织入。这种方式需要特殊的编译器。AspectJ的织入编译器就是以这种方式织入切面的。
类加载期:切面在目标类加载到JVM时被织入。这种方式需要特殊的类加载器(CLassLoader),它可以在目标类被引入应用之前增强该目标类的字节码。AspectJ5的
加载时织入(load-time weaving, LTW)就支持一这种方式织入切面。
运行期:切面在应用运行的某个时刻被织入。一般情况下,在织入切面时,AOP容器会为目标动态创建一个代理对象。Spring AOP就是以这种方式织入切面的。
2.Spring对AOP的支持
Spring提供了4种类型的AOP支持
基于代理的经典Sping AOP;
纯POJO切面;
@AspectJ注解驱动的切面;
注入式AspectJ切面(适用于Spring各版本)。
前三种都是Spring AOP实现的变体,Spring AOP构建在动态代理基础之上,因此,Spring对AOP的支持局限于方法拦截。
- 通过切点来选择连接点
1.编写切点
定义接口类
package concert;public interface Performance {public void perform();}使用AspectJ切点表达式来选择Performance的perform()方法
execution(* concert.Performance.perform(..))。
execution表示方法在执行时触发。*表示返回任意类型。“concert.Performance”表示方法所处的类。“perform”方法的名字。(..)使用任意参数。“* concert.Performance.perform(..)”表示指定的方法。
2.在切点中选择bean
execution(* concert.Performance.perform(..) ) and bean('woodstock')。在执行Performance的perform()方法时应用通知,但限定bean的ID为woodstock。
3.使用注解创建切面
package concert;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class Audience {/** * 表演之前 */@Before("execution(** concert.Performance.perform(..))")public void silenceCellPhones() {System.out.println("Silencing cell phones");}/** * 表演之前 */@Before("execution(** concert.Performance.perform(..))")public void takeSeats() {System.out.println("Taking seats");}/** * 表演之后 */@AfterReturning("execution(** concert.Performance.perform(..))")public void applause() {System.out.println("CLAP CLAP CLAP!!!");}/** * 表演失败之后 */@AfterThrowing("execution(** concert.Performance.perform(..))")public void demandRefund() {System.out.println("Demanding a refund");}}
通过@Pointcut注解声明频繁使用的切点表达式
package concert;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class Audience {@Pointcut("execution(** concert.Performance.perform(..))")public void performance(){};/** * 表演之前 */@Before("performance()")public void silenceCellPhones() {System.out.println("Silencing cell phones");}/** * 表演之前 */@Before("performance()")public void takeSeats() {System.out.println("Taking seats");}/** * 表演之后 */@AfterReturning("performance()")public void applause() {System.out.println("CLAP CLAP CLAP!!!");}/** * 表演失败之后 */@AfterThrowing("performance()")public void demandRefund() {System.out.println("Demanding a refund");}}
在JavaConfig中启用代理AspectJ注解的自动代理
package concert;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration//启用AspectJ自动代理@EnableAspectJAutoProxy@ComponentScanpublic class ConcertConfig {/** * 声明Audience bean * @return */@Beanpublic Audience audience() {return new Audience();}}
在xml中,通过Spring的aop命名空间启用AspectJ自动代理
<?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:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="concert" /><!-- 启用Aspect自动代理 --><aop:aspectj-autoproxy /><!-- 声明 Audience bean --><bean class="concert.Audience" /></beans>
AspectJ自动代理都会为使用@Aspect注解的bean创建一个代理,这个代理会围绕着所有该切面的切点所匹配的bean。在这种情况之下,将会为ConcertBean创建一个代理,Audience类中的通知方法将会在perform()调用前后执行。
Spring 的AspectJ自动代理仅仅是使用@AspectJ作为创建切面的指导,切面依然是基于代理的。在本质上,它依然是Spring基于代理的切面。这一点非常重要,因为这意味着尽管使用的是@AspectJ注解,但我们仍然限于代理方法的调用。如果想利用ASpectJ的所有能力,我们必须在运行时使用AspectJ并且不依赖Spring来创建基于代理的切面。
2.创建环绕通知
package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class Audience {@Pointcut("execution(** concert.Performance.perform(..))")public void performance(){};/** * 环绕通知 * 必须接受ProceedingJoinPoint参数。因为要通过该参数来调用被通知的方法。 * */@Around("performance()")public void watchPerformance(ProceedingJoinPoint jp) {try {System.out.println("Silencing cell phone");System.out.println("Taking seats");jp.proceed();System.out.println("CLAP CLAP CLAP !!!");} catch (Throwable e) {System.out.println("demanding a refund");}}}
3.处理通知中的参数
4.通过注解引入新功能
- 在xml中声明切面
1.声明前置通知和后置通知
<?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:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 声明 Audience bean --><bean id="audience" class="concert.Audience" /><!-- 第一种形式:通过xml将无注解的Audience声明为切面 --><aop:config><!-- 引用Audience Bean --><aop:aspect ref="audience"><!-- 表演之前 --><aop:before pointcut="execution(** concert.Performance.perform(..))"method="silenceCellPhones" /><aop:before pointcut="execution(** concert.Performance.perform(..))"method="takeSeats" /><!-- 表演之后 --><aop:after-returning method="applause"pointcut="execution(** concert.Performance.perform(..))" /><!-- 表演失败之后 --><aop:after-throwing pointcut="execution(** concert.Performance.perform(..))"method="demandRefund" /></aop:aspect></aop:config> <!-- 第二种形式:使用<aop:pointcut>定义命名切点 --><aop:config><!-- 引用Audience Bean --><aop:aspect ref="audience"><!-- 定义切点 --><aop:pointcut expression="execution(** concert.Performance.perform(..))"id="performance" /><!-- 表演之前 --><aop:before pointcut-ref="performance" method="silenceCellPhones" /><aop:before pointcut-ref="performance" method="takeSeats" /><!-- 表演之后 --><aop:after-returning method="applause"pointcut-ref="performance" /><!-- 表演失败之后 --><aop:after-throwing pointcut-ref="performance"method="demandRefund" /></aop:aspect></aop:config></beans>
2.声明环绕通知
<aop:config><!-- 引用Audience Bean --><aop:aspect ref="audience"><!-- 定义切点 --><aop:pointcut expression="execution(** concert.Performance.perform(..))"id="performance" /><aop:around pointcut-ref="performance" method="watchPerformance"/></aop:aspect></aop:config>
- 总结
AOP是面向对象编程的一个强大的补充。通过AspectJ,我们现在可以吧之前分散在应用各处的行为放入可重用的模块中。我们显示地声明在何时何地应用该行为。有效的减少了冗余代码,并让我们的类关注自身的主要功能。
Spring提供了一个AOP框架,让我们把切面插入到方法执行的周围。
- 面向切面的Spring
- 面向切面的spring
- 面向切面的Spring
- 面向切面的Spring
- 面向切面的Spring
- Spring面向切面的编程
- Spring的面向切面AOP
- 面向切面的Spring<一>
- 面向切面的 Spring —— 什么是面向切面编程?
- Spring 第四章 面向切面的Spring
- Spring入门之面向切面的Spring
- 面向切面的 Spring —— 如何注入 AspectJ 切面?
- Spring 的AOP(面向切面编程)
- spring的AOP面向切面编程
- spring 面向切面编程AOP的原理
- 第4章 面向切面的Spring
- Spring面向切面(AOP)的例子
- 关于Spring的AOP面向切面编程
- Problem D. Fashion Show
- hdu 4819 二维线段树
- 新装ubuntu系统后工作环境的配置
- hive数据类型
- 爬虫学习日记一:带有搜索框的爬取(百度、搜狗)
- 面向切面的Spring
- 10 tips for making your R graphics look their best
- Java并发包之SynchronousQueue实践学习笔记
- 文件上传控件PlupLoad 的使用
- JSP动作指令
- 1、Activity工作流学习笔记
- ubuntu16.04安装mysql5.7.17
- 判断生日是否过了
- POJ 2635 The Embarrassed Cryptographer 高精度取模 + 线性筛