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 ...
随机推荐
- Python 风格规范
分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 每行不超过80 个字符 例外: 如果使用Python 2.4 或更早的版本, 导入模块的行可能多于80 个字符. Pyth ...
- Bootstrap学习笔记之Nestable可拖拽树结构
Nestable是基于Bootstrap的一个可拖拽的树结构表现插件. 下面粗略的介绍一下它的用法,只作为学习参考,如有不合适之处,请各位凑合看. 下图是我在现在系统中用到的Nestable,对系统模 ...
- elasticsearch时间格式DateFormat的含义
时间格式 枚举(或者英文)format pattern 含义 custom - 自定义属性 none - 不转化 basic_date yyyyMMdd 基本时间 basic_date_time ...
- environ - 用户环境(变量)
SYNOPSIS 总览 extern char **environ; DESCRIPTION 描述 变量 environ 指向的是一个叫 'environment'(环境)的字符串数组 (这个变量必须 ...
- autoHeight.vue 高度自适应
autoHeight.vue 高度自适应 <!-- * @description 自适应高度 * @fileName autoHeight.vue * @author 彭成刚 * @date 2 ...
- django 模板中{%for%}的使用
1.{%for athlete in list reversed%} reversed用于反向迭代 2.for 标签 支持一个可选的 empty 变量 3.forloop 模板变量 4.forloo ...
- Navicat连不上MySQL的解决办法
USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{your password}'; ...
- 初探node.js
一.定义及优势 定义:Node.js是一个基于 Chrome V8 引擎 的 JavaScript 运行时,它以事件驱动为基础实现了非阻塞模型. 优势:由于Web场景下的大多数任务(静态资源读取.数据 ...
- 【MyBatis】MyBatis Tomcat JNDI原理及源码分析
一. Tomcat JNDI JNDI(java nameing and drectory interface),是一组在Java应用中访问命名和服务的API,所谓命名服务,即将对象和名称联系起来,使 ...
- (十六)python 3 全局变量局部变量
全局变量和局部变量 全局变量与局部变量两者的本质区别就是在于作用域. 用通俗的话来理解的话,全局变量是在整个py文件中声明,全局范围内都可以访问.局部变量是在某个函数中声明的,只能在该函数中调用它,如 ...