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

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(592)   2024-04-17 12:31:29
在 Spring 框架中,通过构造函数自动装配 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.leftso.autowire.constructor.EmployeeBean" autowire="constructor">
    <property name="fullName" value="Lokesh Gupta"/> 
  </bean>
  
  <bean id="department" class="com.leftso.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.leftso.autowire.constructor.EmployeeBean">
  <property name="fullName" value="Lokesh Gupta"/> 
  <constructor-arg>
    <ref bean="department" />
  </constructor-arg>
</bean>
地址:https://www.leftso.com/article/997.html

相关阅读

Spring框架Spring IoC容器的核心原理,前三篇已经从历史的角度和大家一起探讨了为什么会有Spring,Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的...
了解将资源或文件(例如文本文件、XML 文件、属性文件或图像文件)加载到 Spring 应用程序上下文中的不同方法。Spring ResourceLoader为我们通过资源路径getResour...
Spring AOP 实现原理基础讲解
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
FactoryBean 是用作在IoC 容器中创建其他 bean 的工厂bean 。从概念上讲,FactoryBean 与factory method非常相似,但它是 Spring 特定的 be...
spring boot 入门之spring session实现restful apis。通过spring boot或者spring mvc整合spring session的方式来实现sessio...
spring boot 入门之整合spring session实现session共享。一直以来Java编程中web项目中的session共享问题都是一个很难解决的问题。接下来将讲解通过sprin...
在 Spring 框架中,我们可以在 6 个内置的spring bean 作用域内创建 bean ,您也可以定义自定义 bean 作用域。在这六个作用域中,只有在您使用 Web 感知的Appli...
了解Spring bean 生命周期。我们将了解 bean生命周期阶段、初始化和销毁​​回调方法。我们将学习使用 XML 配置以及 Java 注释配置来自定义 bean 生命周期事件。1. 什么...
在 Spring 框架中,按类型自动装配 bean 允许自动装配属性 -如果容器中只有一个属性类型的 bean。如果有多个,则会抛出一个致命异常,这表明您可能不会byType对该 bean 使用...