以下内容引用自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自动装配的更多相关文章

  1. Spring中使用byName实现Beans自动装配

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

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

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

  3. Spring中的applicationContext.xml实现自动装配

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. Spring Beans 自动装配 简介

    Beans 自动装配 Spring 容器可以在不使用<constructor-arg>和<property> 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少 ...

  5. Spring - bean的autowire属性(自动装配)

    当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...

  6. spring框架学习(四)自动装配

    set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...

  7. Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器

    1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set. 体验spring: 1.创建java项目. 2.引入spring ...

  8. spring实战四之Bean的自动装配(注解方式)

    使用注解装配: 从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细 ...

  9. spring实战二之Bean的自动装配(非注解方式)

    Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...

随机推荐

  1. 第一周作业javaee strainmap

  2. [整理] webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用

    webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用 https://www.cnblogs.com/moqiutao/p/7496718.html

  3. docker 镜像管理操作

    镜像特点 1. 分层存储的文件 2.一个软件运行环境 3.一个镜像可以创建多个容器 4.一种标准交付 5.不包含Linux内核而又精简的Linux操作系统 6.不是一个单一的文件而是由多层构成的,可以 ...

  4. faster rcnn的改进方向

    http://blog.csdn.net/linolzhang/article/details/74159463 http://blog.csdn.net/linolzhang/article/det ...

  5. C-基础:关于预编译以及宏

    这是没有引入任何头文件时,如果使用"NULL",编译器会报错:没有定义NULL.此时可用下面代码定义. #undef NULL //#undef 是在后面取消以前定义的宏定义#if ...

  6. python之道02

    猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了,然后继续让用户输入; 如果比66小,则显示猜测的结果小了,然后继续让用户输入;只有等于66,显示猜测结果正确,然 ...

  7. Git Bash Windows客户端乱码

    最近升级Git后,打开Git Bash出现了乱码,解决方法是: 注意,我升级之后,本地和字符集栏位出现了空白的情况.如果检查这里为空白,那么把本地设置为zn_CN,字符集设置为UTF-8

  8. Java中的线程安全和非线程安全以及锁的几个知识点

    1. 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不会出现数据不一致或者数据污染. 线程不安全就是不提供 ...

  9. 使用github中py12306抢票系得

    首先需要安装最新的python:安装步骤见:https://www.cnblogs.com/weven/p/7252917.html 其次下载python源码: 链接:https://pan.baid ...

  10. c#如何判断textbox中输入的数据是datatime型的

    ()你好,标准的方法是用一个验证控件:RangeValidator,把type设为DateTime,最大值设为'3000-1-1'或者别的,最小值最好设为'1900-1-1'. ()程序里面自己验证: ...