Spring通过全类名(反射)配置Bean以及三种依赖注入

本文介绍了Spring如何通过全类名利用反射配置Bean,并详细讲解了属性注入和构造器注入两种依赖注入方式。属性注入依赖Bean的set方法,而构造器注入则依据构造函数进行。此外,文章还探讨了两种注入方式的不同应用场景和注意事项。
摘要由CSDN通过智能技术生成

IOC控制反转和DI依赖注入:
http://blog.xiaohansong.com/2015/10/21/IoC-and-DI/

配置Bean其实就是使用DI依赖注入,将Bean交给IOC容器管理,实现IOC控制反转,达到解耦的过程。

Spring通过全类名(反射)配置Bean特点:

1. 基于xml文件的方式,
2. 通过全类名反射,
3. 依靠IOC容器,
4. 依赖注入的方式:属性注入,构造器注入

常见三种依赖注入:(下文先只介绍前两种)

1. 属性注入
2. 构造器注入
3. 工厂方法注入

Spring通过全类名(反射)配置Bean:

Bean类:

package hello;

public class Hello {
   

    String name;
    int numberInt;
    double numberDouble;

    public void setName(String name) {
        this.name = name;
    }

    public Hello() {
    }
    public Hello(String name, double numberDouble) {
        this.name = name;
        this.numberDouble = numberDouble;
    }
    public Hello(String name, int numberInt) {
        this.name = name;
        this.numberInt = numberInt;
    }
    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                ", numberInt=" + numberInt +
                ", numberDouble=" + numberDouble +
                '}';
    }
}

配置Bean的xml文件:

<bean id</
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值