1. Introduction
Beans in application are same as bricks in house. A bean is an object of your application. An application is made of several beans. A bean is an object which is instantiated, assembled (refers to injection of dependencies) and managed by Spring IoC container.
IoC container instantiates, assembles and manages the beans using the configuration metadata provided to the container. Two common ways to provide configuration metadata to the container is using the XML bean definitions and annotations.
Beans are represented as BeanDefinition
objects inside the container. BeanDefinition
is an interface in org.springframework.beans.factory.config
package.
A bean contains very important information such as:
- A package-qualified class name. Typically the class name is of the actual implementation class of the bean.
- Bean’s dependencies, i.e. references to other beans that are required by the defined bean to do its work. These bean references are also called collaborators.
- Bean configuration elements which define the behavior of the bean in the container, for example scope, lifecycle callbacks and so forth.
- Other configuration elements for the newly created object, for example if the bean manages a connection pool it contains the number of connections.
To summarize, a bean definition contains properties:
- class
- name
- constructor arguments
- initialization method
- scope
- autowiring mode
- properties
- lazy initialization method
- destruction method
2. Note
- Existing objects created outside container can also be registered by first getting BeanFactory through the
getBeanFactory()
method.getBeanFactory()
method returnsDefaultListableBeanFactor
implementation which hasregisterSingleton(..)
andregisterBeanDefinition(..)
methods. - Registration of new beans at runtime with concurrent access to factory is not supported.
3. Overriding Beans
Bean overriding happens when a bean is registered using an identifier that is already allocated. Bean overriding makes it difficult to read configuration and is intended to be deprecated in the future.
Bean overriding can be disabled by setting the allowBeanDefinitionOverriding
flag to false
on the ApplicationContext
before it is refreshed. If you try to override the bean then exception is thrown with this setting.
When using Java Configuration, an @Bean
method will silently override a scanned bean class with the same component name, provided the return type of the @Bean
method matches the bean class. This means the container will prioritize the @Bean
factory method over any pre-declared constructor in the bean class.
4. Naming of Beans and alias
- Every bean within the container must have a unique identifier. If a bean has more than one identifier then the extra ones are called as aliases.
- In XML-based configuration metadata,
id
andname
attributes are used to specify the bean identifiers. - It is not mandatory to provide
id
andname
attribute for a bean. If we do not provideid
orname
explicitly, the container generates a unique name for the bean. - We can provide aliases for the bean by specifying values in
name
attribute, separated by white space, comma (,) or semicolon(;).
<bean id="customer" name="firstCustomerAlias,secondCustomerAlias"/>
- Bean names are alphanumeric and can contain special characters. For example, studentService, testBean.
- The convention for bean names is to start with lowercase letter, then afterwards camel case is followed. For example, customerService.
- When we do not provide identifiers, Spring generates identifiers using the class name. The rule for generating the identifier is same as mentioned before, start with lowercase and afterwards camel case is followed. If the first two letters of the class name are capital case, the original case is preserved.
- Bean alias can also be provided outside the bean definition using
alias
.
<alias name="studentName" alias="studentFirstName"/>
5. Conclusion
Beans in Spring are objects of the application managed by the container. IoC container uses metadata to create beans. Beans are very important to understand as we start learning Spring. This was a short article but an important one. The id and name attributes which we discussed is very important to remember.
We hope this article was informative for you. Happy Learning!