弹簧教程 - 弹簧汽车线束

2018-01-09 19:06 更新

弹簧教程 - 弹簧汽车线束


过滤以排除...

Java Bean

下面的代码定义了两个Java bean。

package com.www.w3cschool.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.www.w3cschool.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.www.w3cschool.cnmon.JobTitle">
    <property name="action" value="buy" />
    <property name="type" value="1" />
  </bean>

  <bean id="myEmp" class="com.www.w3cschool.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.www.w3cschool.cnmon.JobTitle">
    <property name="action" value="buy" />
    <property name="type" value="1" />
  </bean>

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

</beans>

这里是要运行的代码。

package com.www.w3cschool.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.www.w3cschool.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.www.w3cschool.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.www.w3cschool.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.www.w3cschool.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注释执行依赖性检查以确保自动连接的豆存在。

如果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在字段上执行自动连接。

我们需要@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.www.w3cschool.cnmon.JobTitle">
    <property name="action" value="buy" />
    <property name="type" value="1" />
  </bean>

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

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

</beans>

在上面的xml代码配置文件中,我们有两个com.www.w3cschool.cnmon.Employee实例。 如果我们不指定哪个自动连接到JobTitle,他们都符合自动连线操作。

要选择要使用的Java bean,我们可以使用@Qualifier注释。

package com.www.w3cschool.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

公众号
微信公众号

编程狮公众号