`

Spring的注入方式

阅读更多
spring对其管理的bean的属性注入方式有两种,构造器注入和setter注入。
1 构造器注入

      构造器参数解析根据参数类型进行匹配,如果bean的构造器参数类型定义非常明确,那么在bean被实例化的时候,bean定义中构造器参数的定义顺序就是这些参数的顺序,依次进行匹配,比如下面的代码

package x.y;
public class Foo {
    public Foo(Bar bar, Baz baz) {
        // ...
    }
}
上述例子中由于构造参数非常明确(这里我们假定 Bar和 Baz之间不存在继承关系)。因此下面的配置即使没有明确指定构造参数顺序(和类型),也会工作的很好。


<beans>
    <bean name="foo" class="x.y.Foo">
        <constructor-arg>
            <bean class="x.y.Bar"/>
        </constructor-arg>
        <constructor-arg>
            <bean class="x.y.Baz"/>
        </constructor-arg>
    </bean>
</beans>
下面是基本数据类型的注入


public class ExampleBean {
    private int years;
    private String ultimateAnswer;
    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }
}
<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg type="int" value="7500000"/>
  <constructor-arg type="java.lang.String" value="42"/>
</bean>
我们还可以通过index属性来显式指定构造参数的索引,比如下面的例子:


<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg index="0" value="7500000"/>
  <constructor-arg index="1" value="42"/>
</bean>
通过使用索引属性不但可以解决多个简单属性的混淆问题,还可以解决有可能有相同类型的2个构造参数的混淆问题了,注意index是从0开始







2 setter注入
      通过调用无参构造器或无参static工厂方法实例化bean之后,调用该bean的setter方法,即可实现基于setter的DI。

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }

    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }

    public void setIntegerProperty(int i) {
        this.i = i;
    }   
}
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
<bean id="exampleBean" class="examples.ExampleBean">
  <property name="beanOne"><ref bean="anotherExampleBean"/></property>
  <property name="beanTwo" ref="yetAnotherBean"/>
  <property name="integerProperty" value="1"/>
</bean>
      由于大量的构造器参数可能使程序变得笨拙,特别是当某些属性是可选的时候。因此通常情况下,Spring开发团队提倡使用setter注入。而且setter DI在以后的某个时候还可将实例重新配置(或重新注入)

      尽管如此,构造器注入还是得到很多纯化论者(也有很好的理由)的青睐。一次性将所有依赖注入的做法意味着,在未完全初始化的状态下,此对象不会返回给客户代码(或被调用),此外对象也不需要再次被重新配置           对于注入类型的选择并没硬性的规定。只要能适合你的应用,无论使用何种类型的DI都可以。对于那些没有源代码的第三方类,或者没有提供setter方法的遗留代码,我们则别无选择--构造器注入将是你唯一的选择。


       bean的属性有简单类型(基本数据类型和String)和复杂类型(其他类的实例)两种。分别对应不同的方式。
       简单类型的xml写法如下
<!--第一种方式  推荐使用-->
<property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
</property>
<!--第二种方式-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--第三种方式-->
<property name="properties">
      <value>
         jdbc.driver.className=com.mysql.jdbc.Driver
         jdbcjdbcjdbc.url=jdbc:mysql://localhost:3306/mydb
      </value>
</property>

       复杂类型有两种方式,一种是直接引入别的bean,另一种是用内置bean


<bean id="userDao" class="dao.impl.UserDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="showUser" class="action.ShowUserAction">
   <!--引用其他的bean-->
   <property name="userDao" ref="userDao"/>
</bean>
<bean id="outer" class="...">
  <property name="target">
    <!--这个就是内置bean,不需要指定name的属性-->
    <bean class="com.example.Person">
      <property name="name" value="Fiona Apple"/>
      <property name="age" value="25"/>
    </bean>
  </property>
</bean>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics