彈簧教程 - 彈簧汽車線束

2018-01-09 19:06 更新

彈簧教程 - 彈簧汽車線束


過濾以排除...

Java Bean

下面的代碼定義了兩個Java bean。

package com.hgci.cnmon;
public class JobTitle 
{
  @Override
  public String toString() {
    return "JobTitle [person=" + person + ", type=" + type + ", action="
        + action + "]";
  }
  public Employee getPerson() {
    return person;
  }
  public void setPerson(Employee person) {
    this.person = person;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getAction() {
    return action;
  }
  public void setAction(String action) {
    this.action = action;
  }
  private Employee person;
  private int type;
  private String action;
}

員工豆

package com.hgci.cnmon;
public class Employee{
    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 "Employee [name=" + name + ", address=" + address + ", age="
        + age + "]";
  }
}


啟用@Autowired

要啟用@Autowired,我們必須注冊“AutowiredAnnotationBeanPostProcessor"。我們可以通過兩種方式做到這一點。

  • Add Spring context and <context:annotation-config /> in bean configuration file.
  • Include "AutowiredAnnotationBeanPostProcessor" in bean configuration file.

這里是添加Spring上下文和< context:annotation-config />的代碼。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:annotation-config />

  <bean id="myJobTitle" class="com.hgci.cnmon.JobTitle">
    <property name="action" value="buy" />
    <property name="type" value="1" />
  </bean>

  <bean id="myEmp" class="com.hgci.cnmon.Employee">
    <property name="name" value="java2s" />
    <property name="address" value="address ABC" />
    <property name="age" value="29" />
  </bean>

</beans>

以下代碼顯示如何包含“AutowiredAnnotationBeanPostProcessor"在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 
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

  <bean id="myJobTitle" class="com.hgci.cnmon.JobTitle">
    <property name="action" value="buy" />
    <property name="type" value="1" />
  </bean>

  <bean id="myEmp" class="com.hgci.cnmon.Employee">
    <property name="name" value="java2s" />
    <property name="address" value="address ABC" />
    <property name="age" value="29" />
  </bean>

</beans>

這里是要運行的代碼。

package com.hgci.cnmon;

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"});
      JobTitle cust = (JobTitle)context.getBean("myJobTitle");
      System.out.println(cust);
    }
}

Download Java2s_Spring_autowire_1.zip


@Autowired示例

以下代碼顯示如何通過@Autowired在setter方法中自動布線bean

package com.hgci.cnmon;
import org.springframework.beans.factory.annotation.Autowired;

public class JobTitle 
{
  @Override
  public String toString() {
    return "JobTitle [person=" + person + ", type=" + type + ", action="
        + action + "]";
  }
  public Employee getPerson() {
    return person;
  }
  @Autowired
  public void setPerson(Employee person) {
    this.person = person;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getAction() {
    return action;
  }
  public void setAction(String action) {
    this.action = action;
  }
  private Employee person;
  private int type;
  private String action;
}

以下代碼顯示了如何通過@Autowired在構造函數上自動布線bean。

package com.hgci.cnmon;
import org.springframework.beans.factory.annotation.Autowired;

public class JobTitle 
{
  @Autowired
  public JobTitle(Employee person) {
    this.person = person;
  }
  @Override
  public String toString() {
    return "JobTitle [person=" + person + ", type=" + type + ", action="
        + action + "]";
  }
  public Employee getPerson() {
    return person;
  }

  public void setPerson(Employee person) {
    this.person = person;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getAction() {
    return action;
  }
  public void setAction(String action) {
    this.action = action;
  }
  private Employee person;
  private int type;
  private String action;
}

以下代碼顯示了如何通過@Autowired在字段上自動布線bean。

package com.hgci.cnmon;

import org.springframework.beans.factory.annotation.Autowired;

public class JobTitle 
{
  @Autowired
  private Employee person;
  private int type;
  private String action;
}

所有上述三種方法都將自動連接的Employee Java bean轉換為JobTitle的person屬性。

例子

這里是運行上面代碼的代碼。

package com.hgci.cnmon;

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"});

      JobTitle cust = (JobTitle)context.getBean("myJobTitle");
      System.out.println(cust);

    }
}

輸出:


Download Java2s_Spring_Autowire_Setter.zip

依賴性檢查

默認情況下,@Autowired注釋執(zhí)行依賴性檢查以確保自動連接的豆存在。

如果Spring不能找到匹配的bean,它將拋出異常。要禁用@Autowired依賴性檢查,請將@Autowired的“required"屬性設置為false。

import org.springframework.beans.factory.annotation.Autowired;

public class JobTitle 
{
  @Autowired(required=false)
  private Employee person;

}

使用@Qualifier選擇bean

@Qualifier注釋允許我們選擇Java bean在字段上執(zhí)行自動連接。

我們需要@Qualifier注釋,當我們有兩個合格的bean來自動導線。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:annotation-config />

  <bean id="myJobTitle" class="com.hgci.cnmon.JobTitle">
    <property name="action" value="buy" />
    <property name="type" value="1" />
  </bean>

  <bean id="myEmp1" class="com.hgci.cnmon.Employee">
    <property name="name" value="java2s1" />
    <property name="address" value="address 1" />
    <property name="age" value="28" />
  </bean>

  <bean id="myEmp2" class="com.hgci.cnmon.Employee">
    <property name="name" value="java2s2" />
    <property name="address" value="address 2" />
    <property name="age" value="28" />
  </bean>

</beans>

在上面的xml代碼配置文件中,我們有兩個com.hgci.cnmon.Employee實例。 如果我們不指定哪個自動連接到JobTitle,他們都符合自動連線操作。

要選擇要使用的Java bean,我們可以使用@Qualifier注釋。

package com.hgci.cnmon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class JobTitle 
{
  @Autowired
  @Qualifier("myEmp1")
  private Employee person;
}
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號