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.
每天学习一点点,每天进步一点点。
Spring 自动装配 byName的更多相关文章
- spring 自动装配 default-autowire="byName/byType"
<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType" ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring点滴十:Spring自动装配(Autowire)
在基于XML配置元数据,在bean的配置信息中我们可以使用<constructor-arg/>和<property/>属性来实现Spring的依赖注入.Spring 容器也可以 ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring系列七:Spring 自动装配
相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...
- Spring自动装配(二)
为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...
- Spring自动装配----注解装配----Spring自带的@Autowired注解
Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...
- Spring自动装配歧义性笔记
Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...
- Spring按名称自动装配--byName
在Spring中,“按名称自动装配”是指,如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它. 例如,如果“customer” bean公开一个“address”属性,Sprin ...
随机推荐
- 【集群实战】NFS网络文件共享服务
1. NFS介绍 1.1 什么是NFS? NFS是Network File System的缩写,中文意思是网络文件系统. 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. ...
- js的call方法
obj1.method.call(obj2,arg1,arg2,arg3...) call方法的作用就是 把obj1的方法放到obj2对象上使用 arg1,arg2....是参数,传给mehtod的哟 ...
- Nginx重写请求后将url?后的参数去除
2019独角兽企业重金招聘Python工程师标准>>> 使用?结尾 注意,关键点就在于"?"这个尾缀.重定向的目标地址结尾处如果加了?号,则不会再转发传递 ...
- angularJS中$http.get( ).success( )报错原因及解决方案
一.问题描述: 电脑安装的angular1.6.7版本,项目中使用了$http.get( ).success( ),控制台报错: $http.get(...).success is not a fun ...
- tarjan 算法应用
主要讲证明,流程倒是也有 然后发现自己并不会严谨证明 其实后面一些部分流程还是挺详细 本来这篇blog叫做"图论部分算法证明",然后发现OI中的图论想完全用数学上的方法证明完全超出 ...
- bzoj4318 OSU!和bzoj 3450 Tyvj1952 Easy
这俩题太像了 bzoj 3450 Tyvj1952 Easy Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点 ...
- Arduino编程基础1.1
Arduino编程语言是建立在C/C++语言基础上的,即以C/C++语言为基础,把AVR单片机(微控制器)相关的一些寄存器参数设置等进行函数化,以利于开发者更加快速地使用.其主要使用的函数包括数字I/ ...
- 组队训练 K K - The Stream of Corning 2
K - The Stream of Corning 2 这个题目不是很难,因为给你的这个S是单调递增的,所以就用优先队列+权值线段树就可以很快的解决了. 这个+读入挂可以优化,不过不用也没关系. #i ...
- Re模块的 三个方法
import re s1 = '绿茶白茶黄茶青茶红茶黑茶' s2 = '中国绿茶白茶黄茶青茶红茶黑茶' ret = re.findall(".茶", s1) print(ret) ...
- WCF学习(二)
WCF通道模型 绑定的本质是一个配置好的通道栈,为了方便程序员专著与业务逻辑,WCF提高了一系列常用绑定.随后会有相应的自定义通道栈代码 无论交互的另一方具体位置在哪里,WCF都会为消息的发送和接收建 ...