Spring中使用byType实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html:
此模式通过属性类型来指定自动装配。Spring容器查看XML配置文件中将autowire属性设置为byType的bean。然后,如果它的类型与配置文件中的一个bean名称匹配,它将尝试匹配和连接一个属性。如果找到匹配项,它将注入这些bean。否则,bean将不会被连线。
例如,如果在配置文件中将bean定义设置为autowire="byType",并且它包含spellChecker类型的spellChecker属性,则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>testautowiringbytype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testautowiringbytype</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.testautowiringbytype; public class SpellChecker {
public SpellChecker(){
System.out.println("SpellChecker无参数构造函数初始化");
} public void checkSpelling(){
System.out.println("SpellChecker检查方法");
}
}
TextEditor.java:
package com.jsoft.testspring.testautowiringbytype; public class TextEditor {
private SpellChecker spellChecker;
private String name; public void setSpellChecker(SpellChecker spellChecker){
System.out.println("TextEditor通过setter初始化");
this.spellChecker = spellChecker;
} public void spellCheck() {
this.spellChecker.checkSpelling();
} public void setName(String name){
this.name = name;
} public void getName(){
System.out.println(this.name);
} }
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.testautowiringbytype.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testautowiringbytype.TextEditor" autowire="byType">
<property name="Name" value="Hello World!"></property>
</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.testautowiringbytype.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testautowiringbytype.TextEditor">
<property name="SpellChecker" ref="spellChecker"></property>
<property name="Name" value="Hello World!"></property>
</bean> </beans>
运行结果:
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test11/testautowiringbytype
Spring中使用byType实现Beans自动装配的更多相关文章
- Spring中使用byName实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.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 - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- Spring Beans 自动装配 简介
Beans 自动装配 Spring 容器可以在不使用<constructor-arg>和<property> 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少 ...
- Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...
- Spring学习03(Bean的自动装配)
6.Bean的自动装配 6.1 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean的三种装配机 ...
- spring框架学习(四)自动装配
set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...
- Spring XML配置里的Bean自动装配
Spring自动装配 这段是我们之前编写的代码,代码中我们使用了P命名空间 并且使用手动装配的方式将car <bean id="address" class="cn ...
随机推荐
- 原生查找DOM的方法
JS获取DOM元素的方法(8种) 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTagName) 通过类名 ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- 字符串KMP || POJ 2185 Milking Grid
求一个最小矩阵,经过复制能够覆盖原矩阵(覆盖,不是填充,复制之后可以有多的) *解法:横着竖着kmp,求最大公倍数的做法是不对的,见http://blog.sina.com.cn/s/blog_69c ...
- HDU6199 gems gems gems (DP)
题意:有n颗石子 两个人轮流拿 如果上一个人拿了x颗 这个人就可以拿x或x+1颗 问先手能获得与后手的价值差最大是多少 题解:看起来是博弈 其实是DP dp[i][j][0/1]表示当前该0/1拿 拿 ...
- Vue之过滤器的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 微信小程序:errcode=40029和invalid code, hints: [ req_id: VyLhYa0451hb31 ]
问题: 后台用小程序返回的code请求微信服务器换取session_key和openid,返回错误状态码40029 解决问题 当前小程序绑定的appid和请求微信服务器所带的appid参数不一致导致的 ...
- /etc/rc.d启动目录详解
操作系统:CentOS6.6_32位 控制脚本目录/etc/rc.d,该目录下存在各个运行级别的脚本文件,执行ls /etc/rc.d,显示结果为:init.d rc rc0.d rc1.d ...
- Python的Turtle绘制纳兹咩的娘口三三
今天看完夏目友人帐的大电影,哭成了泪猴~ 所以我打算用Python画一只娘口三三陪伴在我身边 不过.. 画的太丑,还没上色..,你们可以完善一下~ 代码放在这里了 import turtle as t ...
- 周三面试Python开发,这几道Python面试题差点答错,Python面试题No7
第1题:阅读下面的代码,默读出A0,A1至An的最终值. A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5))) A1 = range(10) A2 = [ ...
- 阿里云服务器ecs配置之安装mysql
安装mysql数据库 1.安装工作: 下载 mysql 源安装包 [root@ming ~]# wget http://dev.mysql.com/get/ ...