ITEEDU

Spring中Bean的配制详解

Spring的bean相关配制都要放到<beans>标签中。每个bean对应一个<bean>标签

<beans> 
      <bean .../>
      ...
</beans>

 

Bean的别名

在定义bean时就指定所有的别名并不是总是恰当的。有时我们期望能在当前位置为那些在别处定义的bean引入别名。在XML配置文件中,可用单独的<alias/> 元素来完成bean别名的定义。如:

<alias name="fromName" alias="toName"/>

这里如果在容器中存在名为fromName的bean定义,在增加别名定义之后,也可以用toName来引用。

最简单的bean

<bean id="exampleBean"/>
<bean name="anotherExample"/>

用构造器注入

public class ExampleBean {
      private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public ExampleBean(
        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }
}

 

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg>
       <ref bean="anotherExampleBean"/>
  </constructor-arg>
  <constructor-arg ref="yetAnotherBean"/>
  <!—对于JAVA基础类型要指定type -->
  <constructor-arg type="int" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  1.  id:bean的名字,在程序中引用时用。
  2. class:bean类的全路径。
  3. constructor-arg:bean的构造器参数。
  4. type:构造器参数类型。
  5. value:参数值。

其它

通过使用index属性可以显式的指定构造器参数出现顺序。使用index属性除了可以解决多个简单类型构造参数造成的模棱两可的问题之外,还可以用来解决两个构造参数类型相同造成的麻烦。注意:index属性值从0开始。

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg index="0" value="7500000"/>
  <constructor-arg index="1" value="42"/>
</bean>

 

使用Setter注入

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="exampleBean" class="examples.ExampleBean">
  <property name="beanOne">
     <ref bean="anotherExampleBean"/>
  </property>
  <property name="beanTwo" ref="yetAnotherBean"/>
  <property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

property:bean的属性设置

工厂方法实例化

使用静态工厂方法实例化

public class ExampleBean {
      // a private constructor
    private ExampleBean(...) {
      ...
    }

    public static ExampleBean createInstance (
            AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        ExampleBean eb = new ExampleBean (...);
        // some other operations
        ...
        return eb;
    }
}

 

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

factory-method:创建bean的静态工厂方法。

使用实例工厂方法实例化

为使用此机制,class属性必须为空,而factory-bean属性必须指定为当前(或其祖先)容器中包含工厂方法的bean的名称,而该工厂bean的工厂方法本身必须通过factory-method属性来设定(参看以下的例子)。

<!—包含createInstance() 方法的工厂bean-->
<bean id="myFactoryBean" class="...">  ...</bean>
  <!—用来调用工厂的bean -->
<bean id="exampleBean"
      factory-bean="myFactoryBean"
      factory-method="createInstance"/>

和静态工厂相比,只是将class属性改为factory-bean