Spring Container是如何创建的? [英] How is Spring Container created?

查看:157
本文介绍了Spring Container是如何创建的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring Core认证,对此问题有以下疑问:

I am studying for the Spring Core certification and I have following doubt about this question:

容器"是什么意思,以及如何创建容器?

我知道Spring容器是Spring框架的核心.容器将创建对象,将它们连接在一起,进行配置,并管理从创建到销毁的整个生命周期. Spring容器使用依赖项注入(DI)来管理组成应用程序的组件.这些对象称为Spring Bean,我们将在下一章中对其进行讨论.

I know that the Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete lifecycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application. These objects are called Spring Beans which we will discuss in next chapter.

我知道这里有2个容器:

And I know that there exist 2 containers:

  • Spring BeanFactory容器:这是最简单的容器,提供对DI的基本支持,并由org.springframework.beans.factory.BeanFactory接口定义.出于对向后兼容与Spring集成的大量第三方框架的目的,Spring中仍然存在BeanFactory及其相关接口,例如BeanFactoryAware,InitializingBean,DisposableBean.

  • Spring BeanFactory Container: This is the simplest container providing basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.

Spring ApplicationContext容器:此容器添加了更多企业特定的功能,例如从属性文件解析文本消息的能力以及将应用程序事件发布到感兴趣的事件侦听器的能力.该容器由org.springframework.context.ApplicationContext接口定义.

Spring ApplicationContext Container: This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.

好吧...这对我来说很清楚,但是关于如何创建容器的正确答案是什么?

Ok...this is pretty clear for me but what is the correct answer about How to create a container?

我认为它是Spring在读取配置类或XML配置文件时自动创建的.

I think that it is automatically created by the Spring when it reads the configuration class or the XML configuration file.

还是不?我想念什么?

推荐答案

简而言之,容器"是一个Spring实例,负责管理bean的生命周期.

In short, "The Container" is a Spring instance in charge of managing the lifecycle of your beans.

要创建一个,基本上,您应该做类似

To create one, basically, you should do something like

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");

请记住用定义自己的Spring bean的文件替换/application-context.xml.

Remember replacing /application-context.xml by the file where you define your own Spring beans.

看看 http://www.springbyexample.org/examples/intro-to-ioc-creating-a-spring-application.html

您也可以用配置类代替xml.在这种情况下,您应该具有以下内容:

You could also substitute the xml by a configuration class. On that case you should have something like this:

@Configuration
public class Myconfig{

   @Bean 
   public MyBean myBean(){
      return new MyBean();
   }
}

为此,请查看 http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm

这篇关于Spring Container是如何创建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆