How to understand Bean in Spring?

I'm new to Spring and I still can't figure out what a Bean is. From its definition, it appears that the object is defined by some pre-configured configuration files or an annotation to the class. After starting Spring, a Bean is created. But can Spring use DI to create some instances which attributes are not predefined? (For example, a user is submitting json from a site to Spring. And this json contains some data that is used for a new instance. Does Spring use this json to instantiate using DI?)

+3


source to share


2 answers


A bean is just an object created by your spring app. As you know, any spring application has multiple interacting objects working together to trigger the desired programmed behavior.

A Bean is basically a managed object, that is, at runtime, the IOC container creates a Bean based on its definition provided by the coder or as configured in the apllicationContext.xml file in the beans tag and injects this for other classes as needed.

Any spring application is basically a conglomeration of various objects interacting with each other, these objects or beans cooperate to create an application.

A Bean lifecycle is managed by a spring IOC container.



The JSON consumed by the spring app is served by the HttpMessageConverter. When a new request is received, the spring framework will use the content-type header to determine the media type of the request. It will then try to find an appropriate converter available in the application's classpath to transform the request body.

Thus, it is clear that the body object of the incoming request is not managed by the spring IOC container and therefore is not a Bean.

But these deserialized instances are used as data transfer objects in spring app for different tiers like service, DAO, controller.

+2


source


Spring beans are the objects that make up your application and are managed by the Spring framework. Comparing them to the concepts of JavaBeans and POJOs provides an explanatory context , and the Spring reference documentation contains extensive documentation on Spring beans, including this summary:

A bean definition is essentially a recipe for creating one or more objects. The container considers the recipe for the named bean when and uses the configuration metadata encapsulated by that bean definition to create (or acquire) the actual object.

Also included in the reference documentation are descriptions of different ways to instantiate beans using xml or annotation based , as well as Java Config (which also uses annotations). This is managed by Spring's BeanFactory interface (API here ; source here ).



The @Bean annotation is used to indicate that the method instantiates, configures and initializes a new object to be managed by SpringIoC. For those familiar with Spring's XML, the @Bean annotation configuration plays the same role as the element. You can use @Bean annotated methods with any Spring @ Component , however, they are most commonly used with @Configuration beans.

In Dependency Injection (DI), you are referring to a design pattern based on the Inversion of Control Principle, which is an important part of the Spring Framework, especially for instance bean. DI allows values โ€‹โ€‹to be passed to an object from outside. The Spring documentation describes both a constructor and a setter-based DI approach provided by the Spring IoC container for creating beans.

0


source







All Articles