自动装配 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.

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

Spring 自动装配 byName的更多相关文章

  1. spring 自动装配 default-autowire=&quot;byName/byType&quot;

    <PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"   ...

  2. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  3. Spring点滴十:Spring自动装配(Autowire)

    在基于XML配置元数据,在bean的配置信息中我们可以使用<constructor-arg/>和<property/>属性来实现Spring的依赖注入.Spring 容器也可以 ...

  4. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  5. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  6. Spring自动装配(二)

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...

  7. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

  8. Spring自动装配歧义性笔记

    Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...

  9. Spring按名称自动装配--byName

    在Spring中,“按名称自动装配”是指,如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它. 例如,如果“customer” bean公开一个“address”属性,Sprin ...

随机推荐

  1. jdk 的 安装以及环境变量配置

    第一步:下载jdk 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 第二步:安装jdk 第三步:配置环 ...

  2. 【集群实战】inotify

    1. inotify简介 Inotify是一种强大的,细粒度的,异步的文件系统事件监控机制(软件). linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加 ...

  3. 更改 vsftpd 的端口号

    2019独角兽企业重金招聘Python工程师标准>>> vsftpd启动后,默认的ftp端口是21,现在我想把ftp端口改成 801 ,修改后能保证用户上传下载不受影响 1.编辑 / ...

  4. Airtest常见的素定位不到

    一.为什么发这博客 前几天业务需要需要操作云手机进行爬取没办法只有混进airtest官方群边学习边进行开发,蛮简单的东西(可能是我之前会selenium,appuim关系吧),但是群里会有很多问题,关 ...

  5. GCD-Euclidean Algorithm

    求解两个正整数的最大公约数(Greatest Common Devisor),可以采用循环进行遍历,不过效率很低.所以引入欧几里得算法(Euclid's algorithm). 欧几里得算法基于GCD ...

  6. CF #632 (Div. 2) 对应题号CF1333

    1333A Little Artem 在一个\(n\)行\(m\)列的格子上染色,每个格子能染黑白两种 构造一种方案,使得四个方向有至少一个白色格子的黑色格子的数量,比四个方向有至少一个黑色格子的白色 ...

  7. Ajax各参数介绍及使用

    Ajax各参数介绍及使用 1. url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2. type: 要求为String类型的参数,请求方式(post或get)默认为get. ...

  8. spark系列-3、缓存、共享变量

    一.persist  和 unpersist 1.1.persist() : 用来设置RDD的存储级别 存储级别 意义 MEMORY_ONLY 将RDD作为反序列化的的对象存储JVM中.如果RDD不能 ...

  9. 编程坑太多,Map 集合怎么也有这么多坑?一不小心又踩了好几个!

    点赞再看,养成习惯,微信搜索『程序通事』,关注就完事了! 点击查看更多历史文章 上一篇 List 踩坑文章中,我们提到几个比较容易踩坑的点.作为 List 集合好兄弟 Map,我们也是天天都在使用,一 ...

  10. JavaWeb----Servler

    Servlet简介 Servlet就是sun公司开发动态web的一门技术 Sun在这些API中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤: 编写一个类, ...