码迷,mamicode.com
首页 > 编程语言 > 详细

Spring实战第四章

时间:2017-03-11 19:38:26      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:ttl   ssl   key   是什么   处理   如何   来讲   哪些   操作符   

,引言

在软件开发中,散布于应用中多处的功能被称为横切关注点(crosscuttingconcern)。通常来讲,这些横切关注点从概念上是与应用的业务逻辑相分离的

DI有助于应用对象之间的解耦,而AOP可以实现横切关注点与它们所影响的对象之间的解耦。

技术分享

一、面向切面编程

面向切面编程时,仍然在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处应用,而无需修改受影响的类。横切关注点可以被模块化为特殊的类,这些类被称为切面(aspect)。这样做有两个好处:首先,现在每个关注点都集中于一个地方,而不是分散到多处代码中;其次,服务模块更简洁,因为它们只包含主要关注点(或核心功能)的代码,而次要关注点的代码被转移到切面中了。

1.1AOP的相关术语

 1.1.1通知(Advice)

通知定义了切面是什么(要完成的工作)以及何时使用。

 Spring切面可以应用5种类型的通知:

  前置通知(Before):在目标方法被调用之前调用通知功能;

  后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么;

  返回通知(After-returning):在目标方法成功执行之后调用通知;

  异常通知(After-throwing):在目标方法抛出异常后调用通知;

  环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。

1.1.2连接点

(我们的应用可能也有数以千计的时机应用通知。这些时机被称为连接点。)

连接点是在应用执行过程中能够插入切面的一个点。这个点可以是调用方法时、抛出异常时、甚至修改一个字段时。切面代码可以利用这些点插入到应用的正常流程之中,并添加新的行为

1.1.3切点

一个切面并不需要通知应用的所有连接点。切点有助于缩小切面所通知的连接点的范围。

切点的定义会匹配通知所要织入的一个或多个连接点。 通常使用明确的类和方法名称,或是利用正则表达式定义所匹配的类和方法名称来指定这些切点。

1.1.4切面

切面是通知和切点的结合。通知和切点共同定义了切面的全部内容——它是什么,在何时和何处完成其功能。

1.1.5引入

引入允许我们向现有的类添加新方法或属性。这个新方法和实例变量就可以被引入到现有的类中,从而可以在无需修改这些现有的类的情况下,让它们具有新的行为和状态。

1.1.6织入

织入是把切面应用到目标对象并创建新的代理对象的过程。切面在指定的连接点被织入到目标对象中。在目标对象的生命周期里有多个点可以进行织入:

 编译期:切面在目标类编译时被织入。这种方式需要特殊的编译器。AspectJ的织入编译器就是以这种方式织入切面的。

 类加载期:切面在目标类加载到JVM时被织入。这种方式需要特殊的类加载器(ClassLoader),它可以在目标类被引入应用之前增强该目标类的字节码。AspectJ 5的加载时织入(load-timeweaving,LTW)就支持以这种方式织入切面。

 运行期:切面在应用运行的某个时刻被织入。一般情况下,在织入切面时,AOP容器会为目标对象动态地创建一个代理对象。Spring AOP就是以这种方式织入切面的。

总之:通知包含了需要用于多个应用对象的横切行为;连接点是程序执行过程中能够应用通知的所有点;切点定义了通知被应用的具体位置(在哪些连接点)。其中关键的概念是切点定义了哪些连接点会得到通知。

技术分享

1.2 Spring对AOP的支持

Spring提供了4种类型的AOP支持:
  基于代理的经典Spring AOP;
  纯POJO切面;
  @AspectJ注解驱动的切面;
  注入式AspectJ切面(适用于Spring各版本)

前三种是Spring AOP实现的变体,Spring AOP构建在动态代理基础之上,因此,Spring对AOP的支持局限于方法拦截

  第二种:借助Spring的aop命名空间,我们可以将纯POJO转换为切面。实际上,这些POJO只是提供了满足切点条件时所要调用的方法。这种技术需要XML配置,但这的确是声明式地将对象转换为切面的简便方式。

  第三种Spring借鉴了AspectJ的切面,以提供注解驱动的AOP。本质上,它依然是Spring基于代理的AOP,但是编程模型几乎与编写成熟的AspectJ注解切面完全一致。

  第四种类型能够帮助你将值注入到AspectJ驱动的切面中

 Spring所创建的通知都是用标准的Java类编写的。定义通知所应用的切点通常会使用注解或在Spring配置文件里采用XML来编写。

 

Spring在运行时通知对象

    通过在代理类中包裹切面,Spring在运行期把切面织入到Spring管理的bean中,代理类封装了目标类,并拦截被通知方法的调用,再把调用转发给真正的目标bean。当代理拦截到方法调用时,在调用目标bean方法之前,会执行切面逻辑。

    直到应用需要被代理的bean时,Spring才创建代理对象。如果使用的是ApplicationContext的话,在ApplicationContext从BeanFactory中加载所有bean的时候,Spring才会创建被代理的对象。因为        Spring运行时才创建代理对象,所以我们不需要特殊的编译器来织入Spring AOP的切面。

  Spring只支持方法级别的连接点

 1.2 通过切点来选择连接点

  在Spring AOP中,要使用AspectJ的切点表达式语言来定义切点。Spring是基于代理的,而某些切点表达式是与基于代理的AOP无关的。

AspectJ指示器 描述
arg() 限制连接点匹配参数为指定类型的执行方法
@args() 限制连接点匹配参数由指定注解标注的执行方法
execution() 用于匹配是连接点的执行方法
this() 限制连接点匹配AOP代理的bean引用为指定类型的类
target 限制连接点匹配目标对象为指定类型的类
@target()

限制连接点匹配特定的执行对象,这些对象对应的类要具有指定类
型的注解

within() 限制连接点匹配指定的类型
@within()

限制连接点匹配指定注解所标注的类型(当使用Spring AOP时,方
法定义在由指定的注解所标注的类里

@anntion 限定匹配带有指定注解的连接点

  

只有execution指示器是实际执行匹配的,而其他的指示器都是用来限制匹配的。

1.2.1编写切点

 定义一个Performance接口,以来定义切面的切点

public interface Performance {
    public void perform();

}

 

 实现在performance的perform()方法触发时候的通知,下面是一个切点的表达式:

execution(* concert.Performance.perform(..))

技术分享

方法表达式以“*”号开始,表明了不关心方法返回值的类型。然后,指定了全限定类名和方法名。对于方法参数列表,使用两个点号(..)表明切点要选择任意的perform()方法,无论该方法的入参是什么。

  限制举例:

  1、例如配置的切点仅匹配concert包。使用within()指示器匹配。

    技术分享

    使用了“&&”操作符把execution()和within()指示器连接在一起形成与(and)关系(切点必须匹配所有的指示器)。类似地,我们可以使用“||”作符来标识或(or)关系,而使用“!”操作符来标识非(not)           操作。

  但是在xml中,因为“&”在XML中有特殊含义,所以在Spring的XML配置里面描述切点时,我们可以使用and来代替“&&”。同样,or和not可以分别用来代替“||”和“!”

  2、Spring还引入了一个新的bean()指示器,它允许我们在切点表达式中使用bean的ID来标识bean。bean()使用bean ID或bean名称作为参数来限制切点只匹配特定的bean。

技术分享

1.3使用注解创建切面

 1.3.1定义切面

把看表演的观众定义为切面,因为对于表演来说,观众并不是其核心业务。

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public 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("Demaning a refund");
    }
}

 对于这个界面包含两部分:通知和切点

  通知是:

 @Before
    public void silenceCellPhones(){
        System.out.println("Silencing cell phones");
    }

   切点是:

execution(* concert.Performance.perform(..))

通知中要做什么的方法都使用了通知注解来表明它们应该在什么时候调用。AspectJ提供了五个注解来定义通知

注解 通知
@After 通知方法会在目标方法返回或抛出异常后调用
@AfterReturning 通知方法会在目标方法返回后调用
@AfterThrowing 通知方法会在目标方法抛出异常后调用
@Around 通知方法会将目标方法封装起来
@Before 通知方法会在目标方法调用之前执行

在上述的切面中,所有的这些注解都给定了一个切点表达式作为它的值,同时,这四个方法的切点表达式都是相同的。其实,它们可以设置成不同的切点表达式。

 相同的切点表达式可以用@Pointcut注解能够在一个@AspectJ切面内定义可重用的切点。

  

@Aspect
public 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("Demaning a refund");
    }
}

 

注意:performance()方法的实际内容并不重要,在这里它实际上应该是空的。其实该方法本身只是一个标识,供@Pointcut注解依附。通过在performance()方法上添加@Pointcut注解,我们实际上扩展了切点表          达式语言,这样就可以在任何的切点表达式中使用performance()了。

在定义切面之后,Audience类依然是一个POJO。能够像使用其他的Java类那样调用它的方法,它的方法也能够独立地进行单元测试,这与其他的Java类并没有什么区别。Audience只是一个Java类,只不过它通过注解表明会作为切面使用而已。

使用了AspectJ注解,它并不会被视为切面,这些注解不会解析,也不会创建将其转换为切面的代理。

因此可使用JavaConfig,在配置类的类级别上通过使用EnableAspectJ-AutoProxy注解启用自动代理功能。

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
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
    @Bean
    public Audience audience(){
        return new Audience();
    }
}

 

或在xml中使用Springaop命名空间中的<aop:aspectj-autoproxy>元素。

<?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:context="http://www.springframework.org/schema/context"
 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
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd">
     
     <context:component-scan base-package="com.wang.four" />
     <aop:aspectj-autoproxy />
     <bean  class="com.wang.four.Audience"/>
     </beans>

 AspectJ自动代理都会为使用@Aspect注解的bean创建一个代理,这个代理会围绕着所有该切面的切点所匹配的bean。在这种情况下,将会为com.wang.four bean创建一个代理,Audience类中的通知方法将会在perform()调用前后执行。

Spring的AspectJ自动代理仅仅使用@AspectJ作为创建切面的指导,切面依然是基于代理的。在本质上,它依然是Spring基于代理的切面。这一点非常重要,因为这意味着尽管使用的是@AspectJ注解,但我们仍然限于代理方法的调用。

1.3.2环绕通知

环绕通知:能够让你所编写的逻辑将被通知的目标方法完全包装起来。实际上就像在一个通知方法中同时编写前置通知和后置通知。

@Aspect
public class Audience {
    
    @Pointcut("execution(* concert.Performance.perform(..))")
    public void performance(){}
    
    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinPoint){
        try{
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            joinPoint.proceed();
            System.out.println("CLAP CLAP CLAP!!!");
        }catch(Throwable e){
            System.out.println("Demaning a refund");
        }
    }
}

 新的通知方法,它接受ProceedingJoinPoint作为参数。这个对象是必须要有的,要在通知中通过它来调用被通知的方法。通知方法中可以做任何的事情,当要将控制权交给被通知的方法时,它需要调用ProceedingJoinPoint的proceed()方法。如果不调这个方法的话,那么你的通知实际上会阻塞对被通知方法的调用。

可以在通知中对它进行多次调用。要这样做的一个场景就是实现重试逻辑,也就是在被通知方法失败后,进行重复尝试。

1.3.3处理通知中的参数

当切面所通知的方法有参数的时候。

切点声明了要提供给通知方法的参数:

技术分享

切点表达式中的args(trackNumber)限定符。它表明传递给playTrack()方法的int类型参数也会传递到通知中去。参数的名称trackNumber也与切点方法签名中的参数相匹配

这个参数会传递到通知方法中,这个通知方法是通过@Before注解和命名切点trackPlayed(trackNumber)定义的。切点定义中的参数与切点方法中的参数名称是一样的,这样就完成了从命名切点到通知方法的参数转移。

例如:

@Aspect
public class TrackCounter {
    
    private Map<Integer,Integer> trackCounts = new HashMap<Integer,Integer>();
    
    @Pointcut("execution(* com.wang.second.CompactDisc.playTrack(int))" +
    "&& args(trackNumber)")
    public void trackPlayer(int trackNumber){}
    
    @Before("trackPlayer()")
    public void countTrack(int trackNumber){
        int currentCount = getPlayCount(trackNumber);
        trackCounts.put(trackNumber, currentCount);
    }
    
    public int getPlayCount(int trackNumber){
        return trackCounts.containsKey(trackNumber)?trackCounts.get(trackNumber):0;
    }
}

1.3.4  通过注解引入新功能

在Spring中,切面只是实现了它们所包装bean相同接口的代理。如果除了实现这些接口,代理也能暴露新接口的话,会怎么样呢?那样的话,切面所通知的bean看起来像是实现了新的接口,即便底层实现类并没有实现这些接口也无所谓。

技术分享

当引入接口的方法被调用时,代理会把此调用委托给实现了新接口的某个其他对象。实际上,一个bean的实现被拆分到了多个类中。

例如:为示例中的所有的Performance实现引入下面的Encoreable接口:

public interface Encoreable {
    void performEncore();
}

 借助于AOP的引入功能,可以不必在设计上妥协或者侵入性地改变现有的实现。为了实现该功能,我们要创建一个新的切面:

通过@DeclareParents注解,将Encoreable接口引入到Performance bean中。

@Aspect
public class EncoreableIntroducer {
    
    @DeclareParents(value="com.wang.Performance+",defaultImpl=DefaultEmcoreable.class)
    public static Encoreable encoreable;

}

 

 @DeclareParents注解由三部分组成:

  value属性指定了哪种类型的bean要引入该接口。在本例中,也就是所有实现Performance的类型。(标记符后面的加号表示是Performance的所有子类型,而不是Performance本身。)

  defaultImpl属性指定了为引入功能提供实现的类。在这里,我们指定的是DefaultEncoreable提供实现。

  @DeclareParents注解所标注的静态属性指明了要引入了接口。在这里,我们所引入的是Encoreable接口。

之后还需:在Spring应用中将EncoreableIntroducer声明为一个bean:技术分享

Spring的自动代理机制将会获取到它的声明,当Spring发现一个bean使用了@Aspect注解时,Spring就会创建一个代理,然后将调用委托给被代理的bean或被引入的实现,这取决于调用的方法属于被代理的bean还是属于被引入的接口。

1.4在xml中声明切面

 在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面。

定义AOP返回通知

AOP配置元素 用途
<aop:advisor> 定义AOP通知器
<aop:after> 定义AOP后置通知(不管被通知的方法是否执行成功)
<aop:after-returning>  定义AOP返回通知
 <aop:after-throwing>  定义AOP异常通知
 <aop:around>  定义AOP环绕通知
 <aop:aspect>  定义一个切面
 <aop:aspectj-autoproxy>  启用@AspectJ注解驱动的切面
 <aop:before>  定义一个AOP前置通知
 <aop:config>  

顶层的AOP配置元素。大多数的<aop:*>元素必须包含
在<aop:config>元素内

 <aop:declare-parents>  以透明的方式为被通知的对象引入额外的接口
 <aop:pointcut>  定义一个切点

举例:声明一个Audience类,通过xml配置使其成为AOP

public class Audience {
    public void silenceCellPhones(){
        System.out.println("Silencing cell phones");
    }
    
    public void takeSeats(){
        System.out.println("Taking seats");
    }
    
    public void applause(){
        System.out.println("CLAP CLAP CLAP!!!");
    }
    
    public void demandRefund(){
        System.out.println("Demaning a refund");
    }
}

 

在xml中声明前置和后置通知。使用Spring aop的命名空间,使得Audience类转化为切面

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:context="http://www.springframework.org/schema/context"
 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
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd">
     <bean id="audience" class="com.wang.four.Audience"/>
     <aop:config>
         <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 
                 pointcut="execution(* concert.Performance.perform(..))"
                 method="applause"/>
             <aop:after-throwing
                 pointcut="execution(* concert.Performance.perform(..))"
                  method="demandRefund"/>
         </aop:aspect>
     
     </aop:config>
     </beans>

 

第一个需要注意的事项是大多数的AOP配置元素必须在<aop:config>元素的上下文内使用。这条规则有几种例外场景,但是把bean声明为一个切面时,我们总是从<aop:config>元素开始配置。

在<aop:config>元素内,我们可以声明一个或多个通知器、切面或者切点。在程序中,使用<aop:aspect>元素声明了一个简单的切面。ref元素引用了一个POJO bean该bean实现了切面的功能——在这里就是audience。ref元素所引用的bean提供了在切面中通知所调用的方法。

下面展示展示了通知逻辑如何织入到业务逻辑中:

  技术分享

当切点重复的时候,基于XML的切面声明中,需要使用<aop:pointcut>元素。

<?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:context="http://www.springframework.org/schema/context"
 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
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd">
     <bean id="audience" class="com.wang.four.Audience"/>
     <aop:config>
         <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 
                 pointcut-ref="performance"
                 method="applause"/>
             <aop:after-throwing
                 pointcut-ref="performance"
                  method="demandRefund"/>
         </aop:aspect>
     
     </aop:config>
  </beans>

 

环绕通知的实现:

相对于前置通知和后置通知,环绕通知在这点上有明显的优势。使用环绕通知,我们可以完成前置通知和后置通知所实现的相同功能,而且只需要在一个方法中实现。因为整个通知逻辑是在一个方法内实现的,所以不需要使用成员变量保存状态。

在类中声明环绕通知所用的方法

public class Audience {
    public void watchPerformance(ProceedingJoinPoint jp){
        try{
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            jp.proceed();
            System.out.println("CLAP CLAP CLAP!!!");
        }catch(Throwable e){
            System.out.println("Demaning a refund");
        }
    }
}

 

在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:context="http://www.springframework.org/schema/context"
 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
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd">
     <bean id="audience" class="com.wang.four.Audience"/>
     <aop:config>
         <aop:aspect ref="audience">
         <aop:pointcut expression="execution(* concert.Performance.perform(..))" id="performance"/>
           <aop:around
                pointcut-ref="audience"
                method="watchPerformance"/>
         </aop:aspect>
     
     </aop:config>
  </beans>

 

为通知传递参数

实现一个记录每条轨道播放次数功能。

编辑记录的类的实现

public class TrackCounter {
    
    private Map<Integer,Integer> trackCounts = new HashMap<Integer,Integer>();public void trackPlayer(int trackNumber){}public void countTrack(int trackNumber){
        int currentCount = getPlayCount(trackNumber);
        trackCounts.put(trackNumber, currentCount);
    }
    public int getPlayCount(int trackNumber){
        return trackCounts.containsKey(trackNumber)?trackCounts.get(trackNumber):0;
    }
}

 

通过xml使其成为一个切面

     <bean id="audience" class="com.wang.four.Audience"/>
     
     <bean id="trackCounter" class="com.wang.four.TrackCounter"/>
     <bean id="cd" class="com.wang.second.BlankDisc">
         <property name="title" value="Sgt. Peppers‘s Lonely" />
         <property name="artist" value="The Beatles" />
         <property name="tracks">
             <list>
                 <value>Sgt.Pepper‘s Lonely</value>
                 <value>With a Little Help from My Friends</value>
                 <value>Getting Better</value>
                 <value>Fixing a Hole</value>
             </list>
         </property>
     </bean>
     
     <aop:config>
        <aop:aspect ref="trackCounter">
            <aop:pointcut  id="trackPlayed"
            expression="execution(* com.wang.second.CompactDisc.playTrack(int) and args(trackNumber)"/>        
        
        <aop:before pointcut-ref="trackPlayed" method="trackNumber"/>
        
        </aop:aspect>
     
     </aop:config>
     

  注意:唯一的差别在这里使用and关键字而不是“&&”(因为在XML中,“&”符号会被解析为实体的开始)。

通过切面引入新的功能

  可以使用Spring aop命名空间中的<aop:declare-parents>元素,完成引入新的功能。

     <aop:config>
         <aop:aspect>
             <aop:declare-parents types-matching="com.wang.four.Performance+"
                  implement-interface="com.wang.four.Encoreable"
                  default-impl="com.wang.four.DefaultEncoreable"/>
         </aop:aspect>
     
     </aop:config>

<aop:declare-parents>声明了此切面所通知的bean 要在它的对象层次结构中拥有新的父类型。具体到本例中,类型匹 配Performance接口(由types-matching属性指定)的那些bean 在父类结构中会增加Encoreable接口(由implementinterface 属性指定)

之后是通过default-impl属性用全限定类名来显式指定Encoreable的实现。

或者使用引用:delegate-ref属性引用了一个Spring bean作为引入的委托。这需要在Spring上下文中存在一个ID为encoreableDelegate的bean。

 两者的区别:使用default-impl来直接标识委托和间接使用delegate-ref的区别在于后者是Spring bean,它本身可以被注入、通知或使用其他的Spring配置。

 

Spring实战第四章

标签:ttl   ssl   key   是什么   处理   如何   来讲   哪些   操作符   

原文地址:http://www.cnblogs.com/mswangblog/p/6532944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!