以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.html

此模式通过属性名称来指定自动装配。Spring容器查看XML配置文件中auto-wire属性设置为byNamebean。然后,它尝试将其属性与配置文件中相同名称定义的bean进行匹配并连接。如果找到匹配,它将注入这些bean。否则,bean将不会被连线。

例如,如果一个bean定义在配置文件中设置为autowire="byName",并且它包含一个spellChecker属性(即它有一个setSpellChecker(...)方法),则Spring会查找一个名为spellChecker的bean定义,并使用它设置属性。当然,还可以使用<property>标签连接剩余的属性。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testautowiringbyname</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testautowiringbyname</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

SpellChecker.java:

package com.jsoft.testspring.testautowiringbyname;

public class SpellChecker {
public SpellChecker(){
System.out.println("SpellChecker无参数构造函数初始化");
} public void checkSpelling(){
System.out.println("SpellChecker检查方法");
}
}

TextEditor.java:

package com.jsoft.testspring.testautowiringbyname;

public class TextEditor {
private SpellChecker spellChecker; public void setSpellChecker(SpellChecker spellChecker){
System.out.println("TextEditor通过setter初始化");
this.spellChecker = spellChecker;
} public void spellCheck() {
this.spellChecker.checkSpelling();
} }

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="spellChecker" class="com.jsoft.testspring.testautowiringbyname.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testautowiringbyname.TextEditor" autowire="byName">
</bean> </beans>

变化之前的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="spellChecker" class="com.jsoft.testspring.testautowiringbyname.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testautowiringbyname.TextEditor">
<property name="SpellChecker" ref="spellChecker" />
</bean> </beans>

使用了自动装配类型byName去实现,省略了<property>属性。

App.java:

package com.jsoft.testspring.testautowiringbyname;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");
textEditor.spellCheck();
}
}

运行结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test11/testautowiringbyname

Spring中使用byName实现Beans自动装配的更多相关文章

  1. Spring中使用byType实现Beans自动装配

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html: 此 ...

  2. Spring中使用构造函数实现Beans自动装配

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-by-Constructor ...

  3. Spring中的applicationContext.xml实现自动装配

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. Spring Beans 自动装配 简介

    Beans 自动装配 Spring 容器可以在不使用<constructor-arg>和<property> 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少 ...

  5. Spring - bean的autowire属性(自动装配)

    当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...

  6. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  7. Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器

    1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set. 体验spring: 1.创建java项目. 2.引入spring ...

  8. Spring学习03(Bean的自动装配)

    6.Bean的自动装配 6.1 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean的三种装配机 ...

  9. spring框架学习(四)自动装配

    set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...

随机推荐

  1. MySql压缩版安装及避免1055错误和msvcp120.dll丢失

    MySql压缩版安装及避免1055错误和msvcp120.dll丢失 MySQL压缩版的安装快速方便,5.7及最新的8版本安装方式大致相同. 在使用group by分组时,可能会遇到1055错误. 另 ...

  2. vue-ssr 文档备注

    https://ssr.vuejs.org/zh/universal.html 基本用法 通过vue-server-renderer插件的createRenderer方法创建一个renderer,再调 ...

  3. 利用js实现图片展开与收缩

    1.元素居中放大: 1>除了要改变元素的宽高以外,还要改变元素的定位(left,top),如果图片放大一倍,那么位移放大宽高的一半. 2>元素必须是定位的.所以,在css中设置为浮动布局, ...

  4. 三段式fsm

    1.状态转移的always中CS,同步ouput的always中NS. 2.3段fsm vs 2段fsm:output逻辑是组合逻辑和同步时序逻辑(消除里不稳的和毛刺). 3.3段fsm vs 1段f ...

  5. 一段式fsm

    //1-paragraph method to decribe FSM //Describe state transition, state output, state input condition ...

  6. CVS使用之:先update后commit

    vcs在进行版本管理时: 1.如果添加新文件 cvs add directory_name/filename cvs commit directory_name/filename 2.如果修改文件 c ...

  7. 使用selenium和phantomJS浏览器获取网页内容的小演示

    # 使用selenium和phantomJS浏览器获取网页内容的小演示 # 导入包 from selenium import webdriver # 使用selenium库里的webdriver方法调 ...

  8. python 列表(增删改查)

    列表 :(列表可以嵌套,列表的中的元素可以为任意) 列表的创建:1.   a = [1, 2, 3] 2.   a = list([1, 2, 3]) 1.查: 索引(下标),都是从0开始 切片 .c ...

  9. Codeforces C. Sonya and Problem Wihtout a Legend(DP)

    Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...

  10. 关于网络IP地址的分类

    一.IP地址的分类 众所周知,IP地址都是以点号.分为4段来表示.不同类的IP前几位的表示含义也不尽相同. 1.A类IP [网络地址] 第一位表示网络地址,且第一个字节的第一位必须以0开头.依据此原则 ...