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

依赖注入的两种形式

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()注入可以没有构造函数,但是构造注入必须有构造函数。


本章总结

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan阅读 4,285评论 2 7
  • 文章作者:Tyan博客:noahsnail.com 3.4 依赖 标准企业应用不会由一个对象(或Spring用语中...
    SnailTyan阅读 1,229评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,273评论 19 139
  • 依赖注入简介 依赖注入是一个很常用的词。Java新手常会写出如下的代码,直接在写一个类的时候让它自己初始化自己。但...
    乐百川阅读 2,595评论 3 10
  • 01 咪蒙,大概三四线城市的女生们,很多人都有听说过咪蒙这个名字,其实咪蒙是我的女神,我所敬佩咪蒙写的不是她的毒鸡...
    王册3阅读 1,384评论 27 21