简体   繁体   English

如何理解Spring中的Bean?

[英]How to understand Bean in Spring?

I am a beginner of Spring and I still can't understand clearly what Bean is.我是Spring的初学者,我仍然无法清楚地理解Bean是什么。 From its definition, it seems a object that is determined by some pre-set configuration files or using annotation on a class.从它的定义来看,它似乎是由一些预先设置的配置文件或在类上使用注释确定的对象。 Once the spring starts up, the bean has been created.一旦 spring 启动,bean 就被创建了。 But can Spring use DI to create some instances of which attributes are not pre-determined?(Like, a user posts a json from website to Spring. And this json contains some data that are used to new a instance. Can Spring use this json to create the instance by using DI?)但是 Spring 可以使用 DI 创建一些属性不是预先确定的实例吗?(例如,用户从网站向 Spring 发布一个 json。这个 json 包含一些用于新建实例的数据。Spring 可以使用这个 json使用 DI 创建实例?)

Bean is just the object created by your spring application. Bean 只是你的 spring 应用程序创建的对象。 As you know any spring application has several interacting objects working together to give rise to the desired programmed behavior.如您所知,任何 Spring 应用程序都有多个交互对象协同工作以产生所需的编程行为。

A Bean is basically a managed object ie at run time the IOC container creates the bean object based on its definition supplied by the coder or as configured in the apllicationContext.xml file under beans tag, and injects it to other classes as required. Bean 基本上是一个托管对象,即在运行时,IOC 容器根据编码器提供的定义或 beans 标记下的 apllicationContext.xml 文件中的配置创建 bean 对象,并根据需要将其注入其他类。

Any Spring application is basically a conglomeration of various objects interacting with each other, these objects or beans collaborate to create the application.任何 Spring 应用程序基本上都是相互交互的各种对象的集合,这些对象或 bean 协作创建应用程序。

A Bean's lifecycle is managed by the Spring IOC container. Bean 的生命周期由 Spring IOC 容器管理。

The JSON consumed by the Spring Application is taken care of by the HttpMessageConverter. Spring 应用程序使用的 JSON 由 HttpMessageConverter 处理。 When recieving a new request, the Spring framework will use the content-type header to determine the media type of the request.当接收到一个新的请求时,Spring 框架将使用content-type头来确定请求的媒体类型。 It will then try to find the corresponding converter, available in the classpath of the application, to convert the Request body.然后它会尝试在应用程序的类路径中找到相应的转换器来转换请求体。

Thus its clear that the incoming request body object is not managed by the Spring IOC container and hence is not a Bean.因此很明显传入的请求正文对象不是由 Spring IOC 容器管理的,因此不是 Bean。

But these deserialized instances are used as Data Transfer Objects in a Spring application's various layers like, service, DAO, controller.但是这些反序列化的实例在 Spring 应用程序的各个层(如服务、DAO、控制器)中用作数据传输对象。

Spring beans are the objects that comprise your application and are managed by the Spring framework. Spring bean 是构成应用程序的对象,由 Spring 框架管理。 Comparing them to the concepts of JavaBeans and POJOs provides some explanatory context , and the Spring reference documentation contains extensive documentation of Spring beans, including this summary:将它们与 JavaBeans 和 POJOs 的概念进行比较提供了一些解释性上下文,Spring 参考文档包含大量 Spring bean 的文档,包括以下摘要:

A bean definition essentially is a recipe for creating one or more objects. bean 定义本质上是创建一个或多个对象的方法。 The container looks at the recipe for a named bean when asked, and uses the configuration metadata encapsulated by that bean definition to create (or acquire) an actual object.当被询问时,容器会查看命名 bean 的配方,并使用该 bean 定义封装的配置元数据来创建(或获取)实际对象。

Also included in the reference documentation are descriptions of various ways to instantiate beans via an xml-based or annotation-based configuration approach as well as the Java Config approach (which also uses annotations).参考文档中还包括对通过基于xml基于注解的配置方法以及Java 配置方法(也使用注解)来实例化 bean 的各种方法的描述。 This is managed by the Spring BeanFactory interface (API here ; source here ).这是由 Spring BeanFactory 接口管理的( 此处为API; 此处为源)。

The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container. @Bean 注解用于指示一个方法实例化、配置和初始化一个由 Spring IoC 容器管理的新对象。 For those familiar with Spring's XML configuration the @Bean annotation plays the same role as the element.对于那些熟悉 Spring 的 XML 配置的人来说,@Bean 注释扮演着与元素相同的角色。 You can use @Bean annotated methods with any Spring @Component, however, they are most often used with @Configuration beans.您可以将 @Bean 注释方法与任何 Spring @Component 一起使用,但是,它们最常与 @Configuration bean 一起使用。

You refer in the question to Dependency Injection (DI), a design pattern based on the Inversion of Control principle, which is a critical part of the Spring Framework, particularly for bean instantiation.您在问题中提到了依赖注入 (DI),这是一种基于控制反转原则的设计模式,它是 Spring 框架的关键部分,特别是对于 bean 实例化。 DI allows values to be passed into the object from outside. DI 允许将值从外部传递到对象中。 The Spring documentation describes both the constructor-based and setter-based approaches to DI provided by the Spring IoC container for instantiating objects (beans). Spring 文档描述了 Spring IoC 容器为实例化对象(bean)提供的基于构造函数和基于setter 的DI 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM