Spring中使用byName实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.html:
此模式通过属性名称来指定自动装配。Spring容器查看XML配置文件中auto-wire属性设置为byName的bean。然后,它尝试将其属性与配置文件中相同名称定义的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自动装配的更多相关文章
- Spring中使用byType实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html: 此 ...
- Spring中使用构造函数实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-by-Constructor ...
- Spring中的applicationContext.xml实现自动装配
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring Beans 自动装配 简介
Beans 自动装配 Spring 容器可以在不使用<constructor-arg>和<property> 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少 ...
- Spring - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...
- Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器
1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set. 体验spring: 1.创建java项目. 2.引入spring ...
- Spring学习03(Bean的自动装配)
6.Bean的自动装配 6.1 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean的三种装配机 ...
- spring框架学习(四)自动装配
set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...
随机推荐
- javaEE(6)_JSP
一.什么是JSP 1.JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术,只用JSP就可以开发动态web资源. 2.为什么J ...
- centos7系统root无法通过su切换到某个普通用户
[root@test ~]# su webappsu: failed to execute /bin/bash: Resource temporarily unavailable [root@test ...
- Linux菜鸟起飞之路【四】绝对路径、相对路径及常用目录
一.绝对路径与相对路径 Linux操作系统中存在着两种路径:绝对路径和相对路径.我们在访问文件或文件夹的时候,其实都是通过路径来操作的.两种路径在实际操作中能起到同等的作用. 在开始具体介绍之前,我们 ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 287: ordinal not in range(128)
python的str默认是ascii编码,和unicode编码冲突,就会报这个错误. import sys reload(sys) sys.setdefaultencoding('utf8')
- python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。
def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...
- python动态添加属性和方法
---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...
- Python9-day3-作业
ascli 字母,数字.特殊字符,1个字节.8位 unicode:16位 两个字节,升级32位,四个字节 utf-8:最少一个字节 8位,英文字母, 1,有变量name = "aleX l ...
- JAVA面向过程VS面向对象
面向过程 面向过程是一种自顶向下的编程,强调行为过程,可扩展性可维护性差. 优点: 性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源. 单片机.嵌入式开发.Linux/Unix等一般 ...
- LOGMNR分析redo log和archive log教程
自Oracle 11g起,无需设置UTL_FILE_DIR就可以使用LOGMNR对本地数据库的日志进行分析,以下是使用LOGMNR的DICT_FROM_ONLINE_CATALOG分析REDO和归档日 ...
- Palindromic Paths(DP)
描述 Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example: A ...