Spring 自动装配 byName

自动装配 byName,这种模式由属性名称(方法名)指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

例如,在配置文件中,如果一个 bean 定义设置为自动装配 byName,并且它包含 spellChecker 属性(即,它有一个 setSpellChecker(...) 方法),那么 Spring 就会查找定义名为 spellChecker 的 bean,并且用它来设置这个属性。你仍然可以使用 标签连接其余的属性。

一个示例

  • 新建一个Spring项目

  • 创建 Java 类 TextEditor,SpellChecker 和 MainApp

这里是 TextEditor.java 文件的内容:

package hello;

public class TextEditor {
    private SpellChecker spellChecker;
    private String name;
    // a setter method to inject the dependency.
    public void setSpellChecker(SpellChecker spellChecker){
        System.out.println("Inside setSpellChecker.");
        this.spellChecker = spellChecker;
    }
    // a getter method to return spellChecker
    public SpellChecker getSpellChecker(){
        return spellChecker;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }

    public void spellCheck(){
        spellChecker.checkSpelling();
    }
}

下面是另一个依赖类文件 SpellChecker.java 的内容:

package hello;

public class SpellChecker {
    public SpellChecker(){
        System.out.println("Inside SpellChecker constructor");
    }
    public void checkSpelling(){
        System.out.println("Inside checkSpelling.");
    }
}

下面是 MainApp.java 文件的内容:

package hello;
//import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
         ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");
         TextEditor te = (TextEditor) context.getBean("textEditor");
         te.spellCheck();
    }
}

下面是在正常情况下的配置文件 Beans.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Definition for textEditor bean-->
    <bean id="textEditor" class="hello.TextEditor" >
        <property name="spellChecker" ref="spellChecker"/>
        <property name="name" value="Generic Text Editor"/>
    </bean>

    <!-- Definition for spellChecker bean -->
    <bean id="spellChecker" class="hello.SpellChecker">
    </bean>

</beans>

如果使用自动装配 “byName”,那么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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Definition for textEditor bean-->
    <bean id="textEditor" class="hello.TextEditor" autowire="byName">
        <property name="name" value="Generic Text Editor"/>
    </bean>

    <!-- Definition for spellChecker bean -->
    <bean id="spellChecker" class="hello.SpellChecker">
    </bean>

</beans>

运行结果如下:

Inside SpellChecker constructor
Inside setSpellChecker.
Inside checkSpelling.

每天学习一点点,每天进步一点点。

原文地址:https://www.cnblogs.com/youcoding/p/12775315.html