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

编程教程 > Java > Spring (1059) 2024-11-26 14:39:04
在 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.xqlee.autowire.constructor.EmployeeBean" autowire="constructor">
    <property name="fullName" value="Lokesh Gupta"/> 
  </bean>
  
  <bean id="department" class="com.xqlee.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.xqlee.autowire.constructor.EmployeeBean">
  <property name="fullName" value="Lokesh Gupta"/> 
  <constructor-arg>
    <ref bean="department" />
  </constructor-arg>
</bean>

评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
Spring框架Spring IoC容器的核心原理,前三篇已经从历史的角度和大家一起探讨了为什么会有Spring,Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的...
了解将资源或文件(例如文本文件、XML 文件、属性文件或图像文件)加载到 Spring 应用程序上下文中的不同方法。Spring ResourceLoader为我们通过资源路径getResour...
FactoryBean 是用作在IoC 容器中创建其他 bean 的工厂bean 。从概念上讲,FactoryBean 与factory method非常相似,但它是 Spring 特定的 be...
在 Spring 框架中,按类型自动装配 bean 允许自动装配属性 -如果容器中只有一个属性类型的 bean。如果有多个,则会抛出一个致命异常,这表明您可能不会byType对该 bean 使用...
在 Spring 框架中,通过构造函数自动装配 bean类似于byType,但适用于构造函数参数。在启用自动装配的 bean 中,它查找构造函数参数的类类型,然后按类型对所有构造函数参数执行自动...
在 Spring 框架中,如果您想通过调用静态工厂方法来创建 bean ,其目的是将对象创建过程封装在static方法中,那么您可以使用factory-method属性。静态工厂方法示例如果你想...
了解Spring bean 生命周期。我们将了解 bean生命周期阶段、初始化和销毁​​回调方法。我们将学习使用 XML 配置以及 Java 注释配置来自定义 bean 生命周期事件。1. 什么...
在 Spring 框架中,我们可以在 6 个内置的spring bean 作用域内创建 bean ,您也可以定义自定义 bean 作用域。在这六个作用域中,只有在您使用 Web 感知的Appli...
在 Spring 框架中,在配置文件中声明 bean 依赖项是一个很好的做法,因此 Spring 容器能够自动装配协作 bean 之间的关系。这意味着可以通过检查BeanFactory的内容让 ...
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
在 Spring 框架中,按名称自动装配 bean 允许对属性进行自动装配,这样它将检查容器并查找名称与需要自动装配的属性完全相同的 bean。例如,如果您有一个按名称设置为自动装配的 bean...
Spring Boot 2.0,Spring框架的Spring Boot 中的Spring Boot Actuator变化讲解。并且了解如何在Spring Boot 2.0中使用Actuator...
spring boot入门,spring boot是一个崭新的spring框架分支项目,本文讲解其属性配置相关
spring boot 入门之整合spring session实现session共享。一直以来Java编程中web项目中的session共享问题都是一个很难解决的问题。接下来将讲解通过sprin...
spring boot 入门之spring session实现restful apis。通过spring boot或者spring mvc整合spring session的方式来实现sessio...