重温Spring in action3篇一依赖注入

/ Java技术 / 没有评论 / 1863浏览

Spring基本知识

alt

  1. Spring核心容器
  1. Spring的AOP模块
  1. 数据集成与访问 -Spring的DAO和JDBC封装了不少模板代码,可使数据库操作代码简单明了还可避免数据库资源泄露等问题。
  1. Web和远程调用多种流行的MVC
  1. Instrumentation模块
  1. 测试

Spring bean注入知识

注入bean的两种方式

  1. 构造器注入
    <bean id="weixinUtil" class="com.air.tqb.utils.weixin.WeixinUtil">
        <constructor-arg name="interfaceUrlPrefix" value="${wxb.interface.url}">
        <constructor-arg name="configuration" ref="wxbConfiguration">
    </bean>
  1. setter方法注入
    <bean id="weixinUtil" class="com.air.tqb.utils.weixin.WeixinUtil">
        <property name="interfaceUrlPrefix" value="${wxb.interface.url}"/>
        <property name="configuration" ref="wxbConfiguration"/>
    </bean>

所有的Spring bean默认都是单列,如果需要每次获取bean都是新产生或者是唯一的话可以使用scope=prototype例如:

    <bean id="weixinUtil" class="com.air.tqb.utils.weixin.WeixinUtil" scope="prototype">
        <property name="interfaceUrlPrefix" value="${wxb.interface.url}"/>
        <property name="configuration" ref="wxbConfiguration"/>
    </bean>

注入内部bean

    <bean id="weixinUtil" class="com.air.tqb.utils.weixin.WeixinUtil" scope="prototype">
        <property name="interfaceUrlPrefix" value="${wxb.interface.url}"/>
        <property name="configuration">
            <bean class="com.air.tqb.utils.weixin.TestInnerBean"/>
        </property>
    </bean>

使用p标签简化<property>

    <bean id="weixinUtil" class="com.air.tqb.utils.weixin.WeixinUtil"
        p:interfaceUrlPrefix="${wxb.interface.url}"
        p:configuration-ref="wxbConfiguration"/>

装配集合

 5. 装配空值
 
  ```xml
  <property name="interfaceUrlPrefix"><null/></property>
  1. 使用SpEL表达式装配bean
    • 装配字面值
<property name="message" value="This value is #{5}"/>
  <!--一般引用bean的方法-->
  <property name="instrument" ref="saxophone"/>
  <!--使用SpEL表达式引用-->
  <property name="instrument" value="#{saxophone}"/>
  <bean id="zhanga" class="com.zealzhang.Test123">
      <property name="song" value="#{kenny.song}"/>
  </bean>
  <property name="song" value="#{songSelector.selectSong()}">
  <!--避免抛出空指针-->
  <property name="song" value="#{songSelector.selectSong()?.toUpperCase()}">
  <!--这里的?运算符会确保左边的值不null-->
    <property name="multiplier" value="#{T(java.lang.Matn).PI}">
    <!--得到0-1之间的一个随机数-->
    <property name="randomNumber" value="#{T(java.lang.Matn).random()}">
  <!--一个条件运算的例子-->
  <property name="song" value="#{songSelector.selectSong() != null ?       songSelector.selectSong() : 'other'}">
  <!--和上面的例子等价-->
  <property name="song" value="#{songSelector.selectSong()?: 'other'}">

最小化Spring XML配置

自动装配Bean属性

  1. byName:把与bean的属性具有相同的名称的bean自动装配到bean对应的属性中
       <!--正常装配-->
       <bean id="saxophone" class="com.air.tqb.aop.MyInstrument"/>

       <bean id="zhanga" class="com.zealzhang.Test123">
           <property name="song" value="#{kenny.song}"/>
           <property name="instrument" ref="saxophone"/>
       </bean>

       <!--自动装配-->
       <bean id="instrument" class="com.air.tqb.aop.MyInstrument"/>
       <bean id="zhanga" class="com.zealzhang.Test123" autowire="byName">
           <property name="song" value="#{kenny.song}"/>
       </bean>
  1. byType自动装配:把与bean属性具有相同类型的的其他bean装配到bean的属性中
<bean id="saxophone" primary="false" class="com.air.tqb.aop.MyInstrument"/>
- 在自动装配时我们希望排除某些Bean可设置`autowire-candidate="false"`

```xml
  <bean id="saxophone" autowire-candidate="false"  class="com.air.tqb.aop.MyInstrument"/>
```
  1. constructor自动装配:把与Bean的构造器入参相同的类型的掐Bean自动装配到Bean构造器的对应的入参中。

    • constructor自动装配与byType自动装配有着相同的局限性当发现多个Bean匹配某个构造器的入参时Spring不会猜测哪个Bean更适合自动装配
      <bean id="saxophone" autowire="constructor"  class="com.air.tqb.aop.MyInstrument"/> 
    
  2. autodetect装配:首先使用constructor自动装配。如果失败再次尝试byType自动装配

      <bean id="saxophone" autowire="autodetect"  class="com.air.tqb.aop.MyInstrument"/>
    
  1. 我们所需要做的仅仅是在根元素增加一个default-autowire="byType"
     <?xml version="1.0" encoding="UTF-8" ?>
     <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
     default-autowire="byType">
      </beans>
  1. 默认情况下default-autowire="none"
  1. 对某个Bean选择了自动装配的同时也能显示的使用<property>装配想要装配的属性,这种情况显示装配优先级较高。
    <bean id="saxophone" class="com.air.tqb.aop.MyInstrument"/>
    <bean id="zhanga" class="com.zealzhang.Test123" autowire="byName">
         <property name="song" value="#{kenny.song}"/>
         <property name="instrument" ref="saxophone"/>
    </bean>
  1. 使用注解装配与在XML中使用autowire属性装配并没有太大区别
  2. Spring容器默认是禁止注解装配的,我们需要显示的在xml中启用它,配置Spring的context命名空间<context:annotation-config>,,最简单的启用方式如下:
    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        <context:annotation-config/>
  </beans> 
  1. 假设我们需要使用@Autowired让Spring自动装配Instrumentalist的instrument属性,则可对setInstrument()方法进行标注
      //当Spring发现我们对setInstrument使用@Autowired注解时Spring就会对该方法执行byType自动装配
      @Autowired
      public void setInstrument(Instrument instrument) {
          this.instrument = instrument;
      }
  1. @Autowired有趣的地方在于他不仅能装配setter方法,还可标注需要自动装配Bean引用的任意方法:
    @Autowired
    public void hereYoueInstrument(Instrument instrument) {
    this.instrument = instrument;
    }
  1. @Autowired注解甚至可以注解构造器
      @Autowired
      public Instrumentalist(Instrument instrument) {
          this.instrument = instrument;
      }
  1. @Autowired可直接标注属性甚至不会受限于private关键词,并删除setter方法,也是我们最常见的装配方法
      @Autowired
      private Instrument instrument;