Spring中使用构造函数实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-by-Constructor.html:
此模式与byType类似,但它适用于构造函数参数。Spring容器查看在XML配置文件中将autowire属性设置为构造方法的bean。然后,它尝试把它的构造函数的参数与配置文件中beans名称中的一个进行匹配和连线。如果找到匹配,它将注入这些bean。否则,bean将不会被连线。
例如,如果bean定义通过配置文件中的构造函数设置为autowire ,并且具有使用SpellChecker类型的参数之一的构造函数,则Spring会查找名为SpellChecker的bean定义,并使用它来设置构造函数的参数。当然,还可以使用<constructor-arg>标签连接剩余的参数。
例子:
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>testautoconstructor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testautoconstructor</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.testautoconstructor;
public class SpellChecker {
public SpellChecker(){
System.out.println("SpellChecker无参数构造函数初始化");
}
public void checkSpelling(){
System.out.println("SpellChecker检查方法");
}
}
TextEditor.java:
package com.jsoft.testspring.testautoconstructor;
public class TextEditor {
private SpellChecker spellChecker;
private String name;
public TextEditor(SpellChecker spellChecker, String name) {
System.out.println("SpellChecker通过构造函数初始化");
this.spellChecker = spellChecker;
this.name = name;
}
public void spellCheck() {
this.spellChecker.checkSpelling();
}
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.testautoconstructor.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testautoconstructor.TextEditor" autowire="constructor">
<constructor-arg value="Hello World!"></constructor-arg>
</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.testautoconstructor.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testautoconstructor.TextEditor">
<constructor-arg ref="spellChecker"></constructor-arg>
<constructor-arg value="Hello World!"></constructor-arg>
</bean> </beans>
App.java:
package com.jsoft.testspring.testautoconstructor; 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();
textEditor.getName();
}
}
运行结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test11/testautoconstructor
Spring中使用构造函数实现Beans自动装配的更多相关文章
- Spring中使用byName实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.html: 此 ...
- Spring中使用byType实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html: 此 ...
- 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框架学习(四)自动装配
set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...
- Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器
1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set. 体验spring: 1.创建java项目. 2.引入spring ...
- spring实战四之Bean的自动装配(注解方式)
使用注解装配: 从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细 ...
- spring实战二之Bean的自动装配(非注解方式)
Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...
随机推荐
- Android(java)学习笔记176: 远程服务的应用场景(移动支付案例)
一. 移动支付: 用户需要在移动终端提交账号.密码以及金额等数据 到 远端服务器.然后远端服务器匹配这些信息,进行逻辑判断,进而完成交易,返回交易成功或失败的信息给移动终端.用户提交账号. ...
- libs/tools.js stringToDate dateToString 日期字符串转换函数
libs/tools.js stringToDate dateToString 日期字符串转换函数 import { stringToDate } from '@/libs/tools.js' e ...
- drawer 抽屉 弹框 在 modal的后面的解决方案
drawer 抽屉 弹框 在 modal的后面的解决方案 方案1 在框内 弹出 <Drawer title="拍照" :transfer="false" ...
- Vector(同步)和ArrayList(异步)异同
//1 同步 异步 //未响应 = 假死 占用内存过多 内存无法进行处理 //请求的方式 :同步 异步 //网页的展现过程中(同步):1. css文件的下载 2.js文件的下载 3.下载你当前的htm ...
- Markdown的安装和语法
步骤: 1.打开webstorm,File-->Setting-->输入plugin-->Install JetBrains plugin-->输入markdown--> ...
- 将自己的数据制作成voc格式
VOCdevkit2007文件下只保存VOC2007,VOC2007下只保存Annotations ImageSets JPEGImages. JPEGImages存放所有的图片数据(即训练测试验证的 ...
- Mybatis 头信息
在使用IDEA开发中,如果不使用Mybatis-Generator时,那么就需要手写Mapper文件,而在创建xml文件时无法直接创建带有mybatis头信息的mapper文件,这里来记录一下Myba ...
- mysql两种常用备份工具
一.mysqldump备份: 在开启GTID模式时,在master上执行的备份: --set-gtid-purged=OFF 在开启GTID模式时,要在slave上执行,想重新搭建一套slave环境. ...
- JavaScript基础对象---Number
一.创建Number实例对象 /** * new Number(value); * value 被创建对象的数字值 * * Number 对象主要用于: 如果参数无法被转换为数字,则返回 NaN. 在 ...
- Chrome浏览器 v68.0.3440.106 正式版怎么样?
谷歌浏览器Google Chrome稳定版迎来v68正式版第三个维护版本发布,详细版本号为v68.0.3440.106,上一个正式版v68.0.3440.84发布于8月1日,时隔8天Google又发布 ...