Spring IoC container can manage more than one bean. Beans are created with details provided via metadata configuration to container (via Java code, Java Annotation, XML). Bean Definitions are represented as the Object of BeanDefinition that contains the several metadata information.
Properties those are part of BeanDefinitions are
- Constructor Argument
- Autowiring mode
- Initialization method
- Destruction method
Registering Beans created outside ContainerIn 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 ConventionEvery 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.
Bean InstantiationContainer combine the application class and metadata configuration details to build the beans. There are different ways a container can build the Beans.
- Constructor Instantiation
- Static Factory Method Instantiation
- Instance Factory Method Instantiation
Constructor InstantiationFor Constructor instantiation bean class(class attribute in <bean>) is required. Default constructor of class may also be required.
Example of constructor instantiation with XML based metadata.
<bean id="HibernateDao" class="com.dao.HibernateDaoImpl"> </bean>
Static Factory Method InstatiationIn this case bean will be created by calling static factory method of a Class. Spring container doesn't have direct involvement in defining the beans.
Example of Static Factory instantiation with XML based metadata.
<bean id="contactService" class="com.ContactService" factory-method="createInstance"/>Here we are defining class having static factory method(createInstance).