以下内容引用自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. umask命令

    umask——显示.设置文件的缺省权限 the user file-creation mask 命令所在路径:Shell内置命令 示例1:显示缺省权限 # umask -S 参数S的作用是以rwx形式 ...

  2. 伪类的格式重点:父标签层级 & 当前标签类型

    伪类的格式重点:父标签层级 & 当前标签类型 通过例子说明: css1: span:nth-of-type(2){color: red;} css2: span :nth-of-type(2) ...

  3. PHP20 PHP面向对象辅助

    学习要点 常用函数 命名空间 类的自动加载 常用函数 class_exists 作用:检查类是否已定义 语法格式: bool class_exists ( string $class_name [, ...

  4. CentOS7.6 修改密码

    一.重启系统,在开机过程中,按下键盘上的e,进入编辑模式   三.将光标一直移动到 LANG=en_US.UTF-8 后面,空格,再追加init=/bin/sh.这里特别注意,需要写在UTF-8后,保 ...

  5. Maven实战读书笔记(二):Maven坐标与仓库

    2.1 Maven坐标 Maven坐标是Maven用来表示一个组件依赖的标示. Maven通过下面几个元素定义坐标:groupId.artifactId.version.packaging.class ...

  6. Shell读取一个表达式并计算其结果

    #!/bin/bash # 读取一个算数表达式并计算出结果 # 如果输入 # 5+50*3/20 + (19*2)/7 # 则结果为 # 17.929 read x printf "%.3f ...

  7. luogu 2709小b的询问--莫队

    https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...

  8. JAVA:ssm框架搭建

    文章来源:http://www.cnblogs.com/hello-tl/p/8328071.html 环境简介 : jdk1.7.0_25/jdk1.8.0_31  tomcat-7.0.81  m ...

  9. 「问题思考」python的递归中return返回none

    代码: #求最大公约数 def gcd(x,y): if x < y: swap = x x = y y = swap if x%y == 0: return y else: gcd(y,x%y ...

  10. 算法导论 第六章 堆排序(python)

    6.1堆 卫星数据:一个带排序的的数通常是有一个称为记录的数据集组成的,每一个记录有一个关键字key,记录的其他数据称为卫星数据. 原地排序:在排序输入数组时,只有常数个元素被存放到数组以外的空间中去 ...