当前位置: 代码迷 >> 综合 >> Spring实现IOC容器的两种实现方式
  详细解决方案

Spring实现IOC容器的两种实现方式

热度:2   发布时间:2024-03-09 15:03:36.0

Spring实现IOC容器的两种实现方式

两种实现方式实则是两个接口,ApplicationContextBeanFactory

两者的区别:

1.BeanFactory:它是Spring内部的使用接口,不提供给开发人员使用,在加载配置文件的时候不会自动创建对象,需要获取对象的时候才去创建对象。如:

//加载配置文件。
BeanFactory bean = new ClassPathXmlApplicationContext("bean.xml");//不会立即执行下面的语句,在需要使用对象时才创建对象。
Student obj = bean.getBean("student",Student.class);

2.ApplicationContext:它是BeanFactory的子接口。提供了更多强大的功能,一般由开发人员使用,在加载配置文件时就会把对象进行创建创建。

//加载配置文件。
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");//在配置文件时,对象会一起创建。
Student student = context .getBean("student",Student.class);

 

  相关解决方案