最小化Spring XML配置

@Inject
  和@Autowired同样,@Inject能够用来自动装配属性、方法和构造器;与@Autowired不一样的是,@Inject没有required属性。
 所以,@Inject注解所标注的关系必须存在,若是不存在,则会抛出异常。
  @Inject还提供了另外一种技巧。与其直接注入一个应用,不如要求@Inject注入一个Provider。Provider接口能够实现Bean应用的
 延迟注入以及Bean的多个实例等功能。
  例如,咱们有一个KnifeJuggler类须要注入一个或多个Knife的实例。假设Knife Bean的做用域声明为prototype,下面的
 KnifeJuggler的构造器将得到5个KnifeBean:
  private Set<Knife> knives;
  
  public KnifeJuggler(Provider<Knife> knifeProvider){
   knives = newHashSet<>();
   for(int i = 0;i<5;i++){
    knives.add(knifeProvider.get());
   }
  }
  
  KnifeJuggler将得到一个Provider<Knife>,而不是在构造器中得到以个Knife实例。这个时候,只有provider被注入进去
 在调用provider的get()方法以前,实际的knife对象并无被注入。在这个示例中,get()方法被调用了5次。由于Knife name的做用域
 为prototype,因此knife的Set集合将被赋予5个不一样的Knife对象
 
在注解注入中使用表达式@Value
 @Value直接标注某个属性、方法或者方法参数,并传入一个String类型的表达式来装配属性,
 例如:
  @Value("Eruption")
  private String song;
  
 实际上,装配简单的值并非@Value所擅长的,不过,借助SpEL表达式,@Value被赋予了魔力。
 例如: 
  @Value("#{sytemProperties.myFavoriteSong}")
  private String song;
  
自动检测Bean:
 一、<contxt:componenet-scan>元素除了完成与<context:annotation-config>同样的工做,还容许Spring自动检测Bean和定义Bean
 这意味着Spring应用中的大多数(或者全部)Bean都可以实现定义和装配.
 
 二、过滤组件扫描
    事实上,能够经过为<context:componenet-scan>配置<context:include-filter>和/或者<context:exclude-filter>
 子元素,咱们能够随意调整扫描行为
 
  <context:componenet-scan base-package = "com.springinaction.springidol">
   <context:include-filter type="assignable" expression="com.springinaction.springidol.Instruments"/>
  </context:componenet-scan>
  
  过滤器类型                     描述
  annotation                    过滤器扫描使用指定注解所标注的哪些类,经过expression属性指定要扫描的注解
  assignable                    过滤器扫描派生与expression属性所指定雷丁的那些类
  aspectj                       过滤器扫描与expression属性所指定的AspectJ表达式所匹配的那些类
  custom                        使用自定义的org.springframework.core.type.TypeFilter实现类,该类由expression属性指定
  regex                         过滤器扫描类的名称与expression属性所指定的正则表达式所匹配的那些类
  
  除了使用<context:include-filter>告知<context:componenet-scan>哪些类须要注册为Spring Bean之外,咱们还能够使用
 <context:exclude-filter>来告知<context:componenet-scan>哪些类不须要注册为Spring Bean。
     例如,除了自定义的@SkipIt注解的类,其余全部的Instruments实现都须要注册为Spring Bean。
  
  <context:componenet-scan base-package = "com.springinaction.springidol">
   <context:include-filter type="assignable" expression="com.springinaction.springidol.Instruments"/>
   <context:exclude-filter type="annotation" expression="com.springinaction.springidol.SkipIt"/>
  </context:componenet-scan>
  
  
定义个一个配置类
    在基于java的配置里使用@Configuration注解的java类,就等价于xml配置中的<beans>元素元素。
 例如: 
 package com.springinaction.springidol
 import org.springframework.context.annotation.Configuration;
 
 @Configuration
 public class SpringIdoIConfig{
 
  //Bean declaration methods go here
 }
 
  @Configuration注解会做为一个标识告诉Spring:这个类将包含一个或多个Spring Bean的定义。这些Bean的定义是使用
 @Bean注解所标注的方法。让咱们看一下如何使用@Bean来装配使用spring基于java的配置所声明的Bean。
 
  @Bean
  public Performer duke(){
   return new Juggler();
  }
  
  这个简单方法就是java配置,他等价于咱们以前使用xml所配置的<bean>元素。@Bean告知Spring这个方法将返回一个对象,
 这个对象应该被注册为Spring应用上下文中的一个Bean。方法名将做为该Bean的Id。在该方法中所实现的全部逻辑本质上都是为了建立Beanjava

相关文章
相关标签/搜索