依赖注入有三种方式

进行依赖注入有三种方式:

1、构造方法依赖注入

public class StupidStudent {
    private SmartStudent smartStudent;
    
    public StupidStudent(SmartStudent smartStudent) {
        this.smartStudent = smartStudent;
    }
    
    public doHomewrok() {
        smartStudent.doHomework();
        System.out.println("学渣抄作业");
    }
}
public class StudentTest {
    public static void main(String[] args) {
        SmartStudent smartStudent = new SmartStudent();
        StupidStudent stupidStudent = new StupidStudent(smartStudent);
        stupidStudent.doHomework();
    }
}

这种方式好比学渣从一开始就赖上了一个学霸,并且和这个学霸建立了长期合作关系。

2、setter方法注入

public class StupidStudent {
    private SmartStudent smartStudent;
    
    public void setSmartStudent(SmartStudent smartStudent) {
       this.smartStudent = smartStudent;
    }
    
    public doHomewrok() {
        smartStudent.doHomework();
        System.out.println("学渣抄作业");
    }
}
public class StudentTest {
    public static void main(String[] args) {
        SmartStudent smartStudent = new SmartStudent();
        StupidStudent stupidStudent = new StupidStudent();
        stupidStudent.setSmartStudent(smartStudent);
        stupidStudent.doHomework();
    }
}

这种方式学霸和学渣只是暂时的合作关系,如果学渣赖上了另一个学霸(调用set()方法传入了另一个对象),那么学渣和学霸的合作关系就结束了。

3、接口注入

public class StupidStudent {
    public void doHomewrok(SmartStudent smartStudent) {
        smartStudent.doHomework();
        System.out.println("学渣抄作业");
    }
}
public class StudentTest {
    public static void main(String[] args) {
        SmartStudent smartStudent = new SmartStudent();
        StupidStudent stupidStudent = new StupidStudent();
        stupidStudent.doHomework(smartStudent);
    }
}

采用这种注入方式,学渣只是在做作业时,才临时抱佛脚地找一下学霸。

一:spring容器

1.在spring IOC容器读取bean配置创建bean实例之前,必须对他进行实例化,只有在容器实例化之后,才可以在IOC容器里获取实例并使用。

  1. //创建spring容器对象,applicationContext是IOC容器,实际上也是一个接口

  2. //ClassPathXmlApplicationContext:是ApplicationContext的接口实现类,是在类的路径下实现该类

  3. ApplicationContext ctx=new ClassPathXmlApplicationContext(“applicationContext.xml”);

2.spring提供了两种类型的IOC容器实现

  • BeanFactory:IOC容器的基本实现。
  • ApplicationContext:提供了更多的高级特性,是BeanFactory的子类。
  • BeanFactory是spring框架的基础设施,面向spring本身,而ApplicationContext面向使用spring框架的开发中,几乎所有的应用场合都是使用ApplicationContext,而非BeanFactory。
  • 无论使用何种方式,配置文件时是相同的。

二:ApplicationContext

1.ApplicationContext是一个接口,其有两个主要实现类

  • ClassPathXmlApplicationContext:从类的路径下加载配置文件。
  • FileSystemXmlApplicationContext:从文件系统中加载配置文件

2.configurableApplicationContext扩展于ApplicationContext,新增加两个主要方法,refresh()方法,close()方法。让ApplicationContext具有启动,刷新和关闭上下文的能力。

3.ApplicationContext在初始上下文的时候,就实例化所有单例的bean

4.WebApplication是专门用来为WEB应用而准备的,他允许从相对于WEB根目录的路径中完成初始操作。

三:依赖 注入的三种方式

1.spring支持三种依赖注入的方式

  • 属性注入(最常用的)
  • 构造器注入
  • 工厂方法注入(很少使用,不推荐大家使用,这里就不介绍了)

2.属性注入

  • 属性注入即通过set方法注入Bean的属性值或依赖的对象
  • 属性注入使用元素,使用name属性指定Bean的属性名称,value属性或者value子节点指定属性值
  • 属性注入是事件开发中最常用的注入方式

3.构造器方法注入

  • 通过构造方法注入bean的属性值或依赖的对象,他保证了Bean实例在实例化就可以使用
  • 构造器注入在元素里声明属性,中没有name属性
  • 使用构造器注入属性值可以指定参数的位置和参数的类型,来区分重载的构造器。

转载:https://zhuanlan.zhihu.com/p/90939765

本文转自 https://www.cnblogs.com/ztf20/p/13752016.html,如有侵权,请联系删除。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值