美文网首页SpringBoot云课堂
Spring的依赖注入及三种配置方式(依赖注入的两种形式)

Spring的依赖注入及三种配置方式(依赖注入的两种形式)

作者: Tommmmm | 来源:发表于2018-01-30 15:59 被阅读9次

依赖注入的两种形式

1.1构造方法注入

LowerAction.class

public class LowerAction implements Action {
    private String prefix;
    private String message;

    public  LowerAction(){}

    public  LowerAction(String prefix, String message){
        this.prefix = prefix;
        this.message = message;
    }
    public String getPrefix() {
        return prefix;
    }
    public String getMessage() {
        return message;
    }
    public void setPrefix(String string) {
        prefix = string;
    }
    public void setMessage(String string) {
        message = string;
    }
    public void execute(String str) {
        System.out.println((getPrefix() + ", " + getMessage() + ", " + str).toLowerCase());
    }
}

ApplicationContext.xml中的TheAction2的Bean

    <bean id="TheAction2" class="com.example.service.LowerAction">
        <constructor-arg index="0">
            <value>Hi</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value>Good Afternoon</value>
        </constructor-arg>
    </bean>

测试函数

    @Test
    public void test5() {
        String XML = "file:src/applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(XML);

        LowerAction lowerAction=(LowerAction) ctx.getBean("TheAction2");
        System.out.println(lowerAction.getPrefix());
        System.out.println(lowerAction.getMessage());

    }

测试结果


测试结果

实验中想到的问题:构造注入只能通过index索引匹配吗?
还有类型匹配

<bean id="TheAction4" class="com.example.service.LowerAction">
        <constructor-arg type="java.lang.String">
            <value>Hi</value>
        </constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>Wushuohan</value>
        </constructor-arg>
    </bean>

以及参数名传值

<bean id="TheAction6" class="com.example.service.LowerAction">
        <constructor-arg name="prefix" value="Hi">
        </constructor-arg>
        <constructor-arg type="message" value="Wushuohan">
        </constructor-arg>
    </bean>

测试结果如下

测试结果

Setter()方法注入

ApplicationContext.xml中的TheAction1的Bean

<bean id="TheAction1" class="com.example.service.LowerAction">
        <property name="prefix">
            <value>Hi</value>
        </property>
        <property name="message">
            <value>Good Morning</value>
        </property>
    </bean>

测试函数

    @Test
    public void test4() {
        String XML = "file:src/applicationContext.xml";
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(XML);
        LowerAction lowerAction=(LowerAction) ctx.getBean("TheAction1");
        System.out.println(lowerAction.getPrefix());
        System.out.println(lowerAction.getMessage());
    }

测试结果如下


测试结果

实验中想到的问题:Setter()注入能不能没有构造函数?
注释掉LowerAction的构造函数

public class LowerAction implements Action {
    private String prefix;
    private String message;

    public void execute(String str) {
        System.out.println((getPrefix() + ", " + getMessage() + ", " + str).toLowerCase());
    }
}

运行后报错

报错
报错原因:TheAction2没有构造函数
TheAction2没有了构造函数
将这个Bean暂时删除后,运行成功:
运行成功
以上的测试说明了Setter()注入可以没有构造函数,但是构造注入必须有构造函数。
本章总结

对于依赖关系无需变化的注入,尽量采用构造注入。而其他的依赖关系的注入,则考虑采用设值注入。

相关文章

  • 依赖注入的方式

    依赖注入: 依赖于某些方式给Bean的资源进行注入 Spring 支持三种依赖注入的方式 属性注入 构造器注入 工...

  • spring入门--bean加载

    [*] spring加载bean的两种方式 [*] spring依赖注入方式 [*] bean配置 源码地址 sp...

  • spring

    18.7.22属性注入和构造器注入区别Spring也同时支持两种依赖注入方式:设值注入和构造注入。 这两种依赖注入...

  • JAVA 核心笔记 || [xxx] Spring 之 依赖注入

    Spring 依赖注入 DL Spring 两种注入方式 Setter 方法注入 构造器注入 使用App.java...

  • Spring三种bean注入方式

    Spring中依赖注入有三种注入方式:一、构造器注入;二、设值注入(setter方式注入);三、Feild方式注入...

  • 依赖注入

    依赖注入 Spring支持两种依赖注入方式,分别是属性注入和构造函数注入.除此之外,Spring 还支持工厂方法注...

  • Spring的依赖注入及三种配置方式(依赖注入的两种形式)

    依赖注入的两种形式 1.1构造方法注入 LowerAction.class ApplicationContext....

  • spring面试问题总结(1-20)

    一. 介绍一下Spring IOC (控制反转,依赖注入)? Spring支持三种依赖注入方式,分别是属性(Set...

  • 用小说的形式讲解Spring

    用小说的形式讲解Spring(1) —— 为什么需要依赖注入用小说的形式讲解Spring(2) —— 注入方式哪家...

  • 06--Spring注入集合属性

    Spring的依赖注入方式大体上可以分为三种: 构造函数注入 Setter方法注入 方法注入 (lookup-me...

网友评论

    本文标题:Spring的依赖注入及三种配置方式(依赖注入的两种形式)

    本文链接:https://www.haomeiwen.com/subject/mwsczxtx.html