首页 技术 正文
技术 2022年11月15日
0 收藏 473 点赞 4,256 浏览 3403 个字

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

此模式通过属性类型来指定自动装配。Spring容器查看XML配置文件中将autowire属性设置为byTypebean。然后,如果它的类型与配置文件中的一个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>

运行结果:

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894