7、spring 依赖注入(DI)

本文详细介绍了Spring框架中的依赖注入(DI)机制,包括setter方法注入、构造器注入等常见方式,并通过示例展示了如何配置和使用这些注入方式。

7、spring 依赖注入(DI)

在spring框架中,主要有以下四种依赖注入的方式

  • setter方法注入
  • 构造器注入
  • 静态方法注入
  • 实例工厂注入

    在实际的运用中主要使用前两种,所以在本文中也主要介绍前两种DI方式


示例1:setter方法依赖注入

目录结构如下:
这里写图片描述

配置文件bean.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="HelloWorldHelprt" class="com.main.autowrite.DI.HelloWorldHelper">
    <property name="helloWorldImpl" ref="helloWorldImpl" />
 </bean>
  <bean id="helloWorldImpl" class="com.main.autowrite.DI.HelloWorldImpl"/>

</beans>

HelloWorld.java

package com.main.autowrite.DI;
public interface HelloWorld {
    public void sayHello();
}

HelloWorldImpl.java

package com.main.autowrite.DI;

public class HelloWorldImpl implements HelloWorld{

    public void sayHello() {
        System.out.println("Hello world");
    }

}

HelloWorldHelper.java

package com.main.autowrite.DI;

public class HelloWorldHelper {

    private HelloWorldImpl helloWorldImpl;
    public HelloWorldHelper(){

    }
    public void setHelloWorldImpl(HelloWorldImpl helloWorldImpl){
        this.helloWorldImpl = helloWorldImpl;
    }

    public void sayHello(){
        helloWorldImpl.sayHello();
    }
}

测试方法:

    @Test
    public void test(){
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("com/main/autowrite/DI/bean.xml");

        HelloWorldHelper helper = 
                (HelloWorldHelper)context.getBean("HelloWorldHelprt");

        helper.sayHello();

    }

例子2:构造器方法依赖注入

在例子1的基础上

修改HelloWorldHelper类如下:

package com.main.autowrite.DI;

public class HelloWorldHelper {

    private HelloWorldImpl helloWorldImpl;

    public HelloWorldHelper(HelloWorldImpl helloWorldImpl){
        this.helloWorldImpl = helloWorldImpl;
    }
    public void sayHello(){
        helloWorldImpl.sayHello();
    }
}

bean.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="HelloWorldHelprt" class="com.main.autowrite.DI.HelloWorldHelper">
    <constructor-arg>
            <ref bean="helloWorldImpl" />
        </constructor-arg>
 </bean>
  <bean id="helloWorldImpl" class="com.main.autowrite.DI.HelloWorldImpl"/>

</beans>

测试方法和例子1的一致,无需更改

两个例子的运行结果均是下图的结果:
这里写图片描述

如需了解静态工厂以及实例工厂的依赖注入,请点击这里学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值