Spring教程 - Spring內(nèi)部Bean注入

2018-01-09 19:06 更新

Spring教程 - Spring內(nèi)部Bean注入


以下部分顯示如何使用Spring內(nèi)部bean。

內(nèi)在Java豆

在下面我們有Customer Bean。 在客戶對象中我們有一個自定義對象Person。

package com.www.o2fo.common;
public class Customer 
{
  private Person person;
  public Customer() {
  }
  public Customer(Person person) {
    this.person = person;
  }
  public void setPerson(Person person) {
    this.person = person;
  }
  @Override
  public String toString() {
    return "Customer [person=" + person + "]";
  }
}

這里是Person類的定義。

package com.www.o2fo.common;
public class Person 
{
  private String name;
  private String address;
  private int age;
  
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "Person [address=" + address + ",age=" + age + ", name=" + name + "]";
  }  
}


參考內(nèi)部Java Bean

將Person Java bean注入Customer bean的一種方法是使用ref屬性,如下所示。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.www.o2fo.common.Customer">
    <property name="person" ref="PersonBean" />
  </bean>
  <bean id="PersonBean" class="com.www.o2fo.common.Person">
    <property name="name" value="java2s" />
    <property name="address" value="address1" />
    <property name="age" value="28" />
  </bean>
</beans>

Download Java2s_Spring_Ref_Attr.zip

Person bean在Customer bean中定義在同一個級別,我們使用ref屬性引用Person bean。

如果Person bean只用在Customer bean中,我們可以嵌套Person的定義bean里面的Customer bean。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.www.o2fo.common.Customer">
    <property name="person">
      <bean class="com.www.o2fo.common.Person">
        <property name="name" value="java2s" />
        <property name="address" value="address1" />
        <property name="age" value="28" />
      </bean>
    </property>
  </bean>
</beans>

Download Java_Spring_Nest_Bean.zip

以下xml代碼顯示了如何使用內(nèi)部bean進(jìn)行構(gòu)造函數(shù)注入。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.www.o2fo.common.Customer">
    <constructor-arg>
      <bean class="com.www.o2fo.common.Person">
        <property name="name" value="java2s" />
        <property name="address" value="address1" />
        <property name="age" value="28" />
      </bean>
    </constructor-arg>
  </bean>
</beans>

Download Java2s_Spring_Inner_Bean_Constructor.zip

以下代碼顯示了如何運(yùn)行上面的配置。

package com.www.o2fo.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = 
        new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

輸出



以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號