Spring学习笔记—最小化Spring XML配置

来源:互联网 发布:淘宝原单是真的吗 编辑:程序博客网 时间:2024/05/01 19:40

                                         spring学习笔记—最小化Spring XML配置

   自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系。
      自动检测(autodiscovery)比自动装配更进了一步,让Spring能够自动识别哪些类需要被配置成Spring Bean,从而减少对<bean>元素的使用。

1.自动装配属性

     1.1  4种类型的自动装配

      ● byName——把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中。如果没有跟属性的名字相匹配的Bean,则该属性不进行装配。
      ● byType——把与Bean的属性具有相同类型的其他Bean自动装配到Bean的对应属性中。如果没有跟属性的类型相匹配的Bean,则该属性不被装配。
      ● constructor——把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。
      ● autodetect——首先尝试使用constructor进行自动装配。如果失败,在尝试使用byType进行自动装配。
      
      byName自动装配:
[html] view plain copy
 print?
  1. <bean id = "kenny" class = "com.ouc.test.springs.Instruments" autowire = "byName" >  
  2.    <property name = "song" value = "bells" />  
  3. </bean>  
    为属性自动装配ID与该属性的名字相同的Bean。
    
     byType自动装配:
    如果Spring寻找到多个Bean,它们的类型与自动装配的属性类型都相匹配,Spring不会猜测哪一个Bean更适合自动装配,而是会抛出异常。
    可以为自动装配标识一个首选Bean,或者可以取消某个Bean自动装配的候选资格。为了使用primary属性,不得不将非首选Bean的primary属性设置为false。
[html] view plain copy
 print?
  1. <bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" primary = "false" />  
    primary属性仅对标识首选Bean有意义。
    如果希望排除某些Bean,可以设置这些Bean的autowire-candidate属性为false。
[html] view plain copy
 print?
  1. <bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" autowire-candidate = "false" />  
   
    constructor自动装配:
    如果要通过构造器注入来配置Bean,可以移除<constructor-arg>元素,由Spring在应用上下文中自动选择Bean注入到构造器入参中。
[html] view plain copy
 print?
  1. <bean id = "saxophone" class="com.ouc.springs.test.Saxophone"  autowire ="constructor" />  
    
    最佳自动装配:
[html] view plain copy
 print?
  1. <bean id = "saxophone" class="com.ouc.springs.test.Saxophone"  autowire ="autodetect" />  

   1.2 默认自动装配

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.      http://www.springframework.org/schema/beans  
  6.      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.      default-autowire="byType" >  
  8.   </beans><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">  </span>  


2 .使用注解装配

    Spring容器默认禁用注解装配。
    启用注解装配最简单的方式是使用Spring的context命名空间配置中的<context:annotation-config>元素。
    Spring 3 支持几种不同的用于自动装配的注解:
     ● Spring自带的@Autowired注解;
     ● JSR-330的@Inject注解;
     ● JSR-250的@Resource注解。
    

   2.1 使用@Autowired

[html] view plain copy
 print?
  1. @Autowired  
  2. public void setInstrument(Instrument instrument){  
  3.   this.instrument = instrument;  
  4. }  
     Spring会尝试对该方法执行byType自动装配,可以使用@Autowired标注需要自动装配Bean引用的任意方法。
     可以使用@Autowired注解直接标注属性,并删除setter方法:
[html] view plain copy
 print?
  1. @Autowired  
  2. private Instrument instrument;  
    1)可选的自动装配:
        默认情况下,@Autowired所标注的属性或参数必须是可以装配的。如果没有Bean可以装配到@Autowired所标注的属性或参数中,自动装配就会失败(抛出NoSuchBeanDefinitionException).
        可以通过设置@Autowired的required属性为false来配置自动装配是可选的。
[html] view plain copy
 print?
  1. @Autowired(required=false)  
  2. private Instrument instrument;  
     2)限定歧义性的依赖
    @Qualifier注解缩小了自动装配挑选候选Bean的范围,通过指定Bean的ID把选择范围缩小到只剩下一个Bean。
[html] view plain copy
 print?
  1. @Autowired  
  2. @Qualifier("guitar")  
  3. private Instrument instrument;  
     3)创建自定义的限定器(Qualifier)
[html] view plain copy
 print?
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5. import org.springframework.beans.factory.annotation.Qualifier;  
  6.   
  7. @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Qualifier  
  10. public @interface StringedInstrument{}  

  2.2 借助@Inject实现基于标准的自动装配

[html] view plain copy
 print?
  1. @Inject  
  2. private Instrument instrument;  
     @Inject没有required属性。
     限定@Inject所标注的属性。
[html] view plain copy
 print?
  1. @Inject  
  2. @Named("guitar")  
  3. private Instrument instrument;  
    @Named通过Bean的ID来标识可选择的Bean,@Named实际上是一个使用@Qualifier注解所标注的注解。
    创建自定义的JSR-330 Qualifier
[html] view plain copy
 print?
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5. import javax.inject.Qualifier;  
  6.   
  7. @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Qualifier  
  10. public @interface StringedInstrument{}  

   2.3 在注解注入中使用表达式

[html] view plain copy
 print?
  1. @Value("#{systemProperties.myFavoriteSong}")  
  2. private String song;  


3.自动检测Bean


     使用<context:component-scan>元素配置自动检测。
[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.    <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context=="http://www.springframework.org/schema/context"  
  5.      xsi:schemaLocation="  
  6.       http://www.springframework.org/schema/beans  
  7.       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8.       http://www.springframework.org/schema/context  
  9.       http://www.springframework.org/schema/beans/spring-context-3.1.xsd" >  
  10.   
  11.       <context:component-scan base-package="com.ouc.springs.test" >  
  12.       </context:component-scan>  
  13.    </beans>  

    3.1为自动检测标注Bean

     默认情况下,<context:component-scan>查找使用构造型(stereotype)注解所标注的类,这些特殊的注解如下:
       @Component——通用的构造型注解,标识该类为Spring组件。
       @Controller——标识将该类定义为Spring MVC Controller。
       @Repository——标识将该类定义为数据仓库。
       @Service——标识将该类定义为服务。
        使用@Component标注的任意自定义注解。

   3.2 过滤组件扫描

    通过为<context:component-scan>配置<context:include-filter>和<context:exclude-filter>子元素,可以随意调整扫描行为。
[html] view plain copy
 print?
  1. <context:component-scan base-package="com.ouc.springs.test" >  
  2.         <context:include-filter type="assignable" expression="com.ouc.springs.tst.Instrument" />  
  3.         <context:exclude-filter type="annotation" expression="com.ouc.springs.tst.SkipIt" />  
  4. </context:component-scan>  
    

4.使用Spring基于Java的配置

    4.1 定义一个配置类

[html] view plain copy
 print?
  1. import org.springframework.context.annotation.Configuration;  
  2.   
  3. @Configuration  
  4. public class SpringIdolConfig{}  
    @Configuration注解会作为一个标识告知Spring:这个类将包含一个或多个Spring Bean的定义。
    

   4.2 声明一个简单的Bean

[html] view plain copy
 print?
  1. @Bean  
  2. public Performer duke(){  
  3.   return new Juggler();  
  4. }  
      @Bean告知Spring这个方法将返回一个对象,该对象应该被注册为Spring应用上下文中的一个Bean,方法名将作为该Bean的ID。

0 0
原创粉丝点击