Spring IoC container manages beans. Container create or configure these beans through configuration metadata provided by any of 3 ways(XML, Annotation, Java based). These beans are represented as BeanDefinition Object inside container. This BeanDefinition object contains several metadata information on how bean should be created.
When ever container get a request to create an Object(Named Bean), container will look for the Configuration Metadata encapsulated by BeanDefinition Object(For Named Bean) and create an actual Object.
Important Properties of BeanDefinition Object
1- Scope
2- Constructor Argument
3- Autowiring Mode
4- Lazy initilization mode
5- Initialization Method
6- Class (Required Except in one condition)
7- Name
Registering Beans created outside Container:
In order to register beans created outside container by user, we can use BeanFactory(DefaultListableBeanFactory)(Accessed by getBeanFactory() method). Accessed beanfactory can be used to register the existing bean via registerSingleton() & registerBeanDefinition() methods.
Bean Naming:
Every bean in a single container can have one or more then one(treated as Alias) identifier(referenced via id / Name) but these identifier needs to be unique. Id / Name are not required attributes so user is not forced to provide Id / Name for a given bean. In case user does not provide any(Id / Name) of them container will generate unique name for the same.
Aliases can be create by providing different option in name attributes these options should be separated by comma.
Assign Name to Bean via XML configuration metadata:
In XML based metadata configuration id/name attribute is used to provide the identifier. Id attribute is used to identified only one identifier. But Name can be used to provide several aliase to the same bean.(Comma seperated names)
Assign Name to Bean via Annotation configuration metadata:
Assign Name to Bean via Java code configuration metadata:
Bean Scopes :
Once Bean is created, scope of the created bean needs to be identified. Spring supports following scopes out of the box.
1- Singleton : Scope of the created bean is limited to single object instance per Spring IoC container.
2- Prototype : Scope of the created bean is not limited to any single object instance but it is available to any number of object instance.
3- Request : Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back
of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
4- Session : Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
5- Global Session : Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the
context of a web-aware Spring ApplicationContext.
6- Thread Scoped :