Spring核心原理-bean 按名称自动装配

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(673)   2024-04-17 12:31:29
在 Spring 框架中,按名称自动装配 bean 允许对属性进行自动装配,这样它将检查容器并查找名称与需要自动装配的属性完全相同的 bean。

例如,如果您有一个按名称设置为自动装配的 bean 定义,并且它包含一个“ departmentBean”属性(即它有一个 setDepartmentBean(..) 方法),容器将查找一个名为 的 bean 定义departmentBean,如果找到,使用它来设置属性。
 

按名称自动装配示例

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">
 
    <context:component-scan base-package="com.leftso" />    
     
   <bean id="employee" class="com.leftso.demo.beans.EmployeeBean" autowire="byName">
        <property name="fullName" value="Lokesh Gupta"/>
    </bean>
   
    <bean id="departmentBean" class="com.leftso.demo.beans.DepartmentBean" >
        <property name="name" value="Human Resource" />
    </bean>
     
</beans>

 

使用 autowire="byName" 按名称自动装配依赖

在上面的配置中,我为“employee”bean 启用了按名称自动装配。它已使用autowire="byName".

EmployeeBean.java:
public class EmployeeBean
{
    private String fullName;
      
    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;
 
import com.howtodoinjava.demo.beans.EmployeeBean;
 
public class TestAutowire  {
  public static void main(String[] args) {
 
        ApplicationContext context =
                  new ClassPathXmlApplicationContext(new String[] {"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

显然,依赖项是按名称成功注入的。

 
地址:https://www.leftso.com/article/996.html

相关阅读

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