Spring - Benefits of Using, Container, Spring bean

POJO - plain old java object (기본 자바 파일)



Benefits of using Spring Framework
  • Works on POJOs + dependency injection 
  • Provides loose coupling between different modules
  • With the Dependency Injection, dependencies are clear in constructor or JavaBean properties
  • Can be configured by XML based schema or annotation-based style.
  • Supports declarative transaction, caching, validation and formatting.
  • Enhances modularity. Provides more readable codes.




Container- a software which provides execution environment for the Spring Beans. It's written in Java. It controls life cycle of Spring beans, dependency resolution, build infrastructure

Spring bean = any POJO Java class registered in Spring Container.

 4 Life cycle of a Spring Bean:

  1. Instantiation phase: Spring container loads Spring Bean into memory, creates the instance of the bean, populates bean using XML file.
  2. Initialization phase: Spring container executes custom initialization (init) method of any Spring bean 
  3. Ready-To-Use phase: Spring container dispense instantiated initialized Spring bean to user.
  4. Destruction phase: - 



Bean definition - 
@Configuration
Annotating a class with the @Configuration annotation indicates that the class will be used by Java Config as a source of bean definitions.
An application may make use of just one @Configuration-annotated class, or many. @Configuration can be considered the equivalent of XML's <beans/> element. Like <beans/>




Declaring Bean
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory.




Spring Bean -
When you create a bean definition what you are actually creating is a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, just like a class, you can potentially have many object instances created from a single recipe.

You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition, but also the scope of the objects created from a particular bean definition. This approach is very powerful and gives you the flexibility to choose the scope of the objects you create through configuration instead of having to 'bake in' the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes
Previous
Next Post »