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

转载于:https://www.cnblogs.com/EasonJim/p/6892274.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值