Spring5核心原理-bean由构造函数自动装配

Spring5核心原理-bean由构造函数自动装配

byType,但适用于构造函数参数。在启用自动装配的 bean 中,它查找构造函数参数的类类型,然后按类型对所有构造函数参数执行自动装配。

请注意,如果容器中没有一个构造函数参数类型的 bean,则会引发致命错误。

构造函数的 Bean 自动装配

如何启用构造函数自动装配

autowire="constructor"通过使用配置文件中的 bean 定义(即)启用构造函数自动装配
application-context.xml

一个典型的 bean 配置文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
  http://www.springframework.org/schema/context/
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <context:annotation-config />
     
  <bean id="employee" class="com.note234.autowire.constructor.EmployeeBean" autowire="constructor">
    <property name="fullName" value="Lokesh Gupta"/> 
  </bean>
  
  <bean id="department" class="com.note234.autowire.constructor.DepartmentBean" >
    <property name="name" value="Human Resource" />
  </bean>
 
</beans>

创建构造函数依赖

在上面的配置中,我已经为 ‘ ‘ bean启用了构造函数的自动装配。
employee它是通过传递构造函数参数来完成的。

EmployeeBean.java:

public class EmployeeBean
{
  private String fullName;
   
  public EmployeeBean(DepartmentBean departmentBean)
  {
    this.departmentBean = departmentBean;
  }
   
  private DepartmentBean departmentBean;
 
  public DepartmentBean getDepartmentBean() {
    return departmentBean;
  }
 
  public void setDepartmentBean(DepartmentBean departmentBean) {
    this.departmentBean = departmentBean;
  }
 
  public String getFullName() {
    return fullName;
  }
 
  public void setFullName(String fullName) {
    this.fullName = fullName;
  }
}

DepartmentBean.java

public class DepartmentBean{
  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

测试依赖

要使用基于构造函数的自动装配测试该 bean 是否已正确设置,请运行以下代码:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestAutowire {
  public static void main(String[] args) {
    ApplicationContext context = 
            new ClassPathXmlApplicationContext(new String[] {"com/howtodoinjava/autowire/constructor/application-context.xml"});
      
          EmployeeBean employee = (EmployeeBean)context.getBean("employee");
 
          System.out.println(employee.getFullName());
 
          System.out.println(employee.getDepartmentBean().getName());
  }
}
 

Output:

Lokesh Gupta
Human Resource

显然,构造函数成功注入了依赖项。

 

使用构造函数参数

如果您没有
autowire="constructor"在 bean 定义中使用,那么您必须通过
constructor-arg以下方式在 bean 中注入
departmentbean 
employee

<bean id="employee" class="com.note234.autowire.constructor.EmployeeBean">
  <property name="fullName" value="Lokesh Gupta"/> 
  <constructor-arg>
    <ref bean="department" />
  </constructor-arg>
</bean>
Spring5核心原理-bean由构造函数自动装配

相关文章:

你感兴趣的文章:

标签云: