Spring5核心原理-bean 按类型自动装配

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(564)   2023-03-28 11:29:14
在 Spring 框架中,按类型自动装配 bean 允许自动装配属性 -
  1. 如果容器中只有一个属性类型的 bean。
  2. 如果有多个,则会抛出一个致命异常,这表明您可能不会byType对该 bean 使用自动装配。
  3. 如果没有匹配的 bean,则什么也不会发生;该属性未设置。如果这是不可取的,设置dependency-check="objects"属性值指定在这种情况下应该抛出错误。

如果您使用@Autowired注解,则不需要提供autowire属性。默认情况下,@Autowired注释使用byType自动装配。

按类型自动装配 Bean

在上下文文件中定义 Bean

一个典型的 bean 配置文件(例如applicationContext.xml)将如下所示
<?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">
     
  <bean id="employee" class="com.leftso.autowire.byType.EmployeeBean" autowire="byType">
    <property name="fullName" value="Lokesh Gupta"/> 
  </bean>
  
  <bean id="department" class="com.leftso.autowire.byType.DepartmentBean" >
    <property name="name" value="Human Resource" />
  </bean>
 
</beans>

 autowire="byType" 按类型自动装配

Bean classes

在上面的配置中,我为 ' employee' bean 启用了按类型自动装配。它已使用autowire="byType"employee现在bean中的所有属性(例如 )都将使用自动装配departmentBean进行查找。byType
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class EmployeeBean
{
  private DepartmentBean departmentBean;
   
  private String fullName;
 
  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看起来像这样:
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/byType/applicationContext.xml"});
      
          EmployeeBean employee = (EmployeeBean)context.getBean("employee");
 
          System.out.println(employee.getFullName());
 
          System.out.println(employee.getDepartmentBean().getName());
  }
}
Output:
Lokesh Gupta
Human Resource

显然,依赖项departmentBean是按类型成功注入的。





 
地址:https://www.leftso.com/article/995.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 使用...