A Bean definition contains the following piece of information called configuration metadata, which helps the container know the following things.
• The way a bean should be created.
• Life cycle details of a bean.
• Associated Bean dependencies.
The above metadata for the bean configuration is provided as a set of properties or attributes in an XML file (configuration file) which together prepare a bean definition. The following are the set of properties.
Properties | Usage |
---|---|
class | In a bean definition, it is a mandatory attribute. It is used to specify the bean class which can be used by the container to create the bean. |
name | In a bean definition, this attribute is used to specify the bean identifier uniquely. In XML based configuration metadata for a bean, we use the id and/or name attributes in order to specify the bean identifier(s). |
scope | This attribute is used to specify the scope of the objects which are created from a particular bean definition. |
constructor-arg | In a bean definition, this attribute is used to inject the dependencies. |
properties | In a bean definition, this attribute is used to inject the dependencies. |
autowiring mode | In a bean definition, this attribute is used to inject the dependencies. |
lazy-initialization mode | In a bean definition, a lazy-initialized bean informs the IoC container to create a bean instance only when it is first requested, instead of startup. |
initialization method | In a bean definition, a callback to be called after all required properties on the bean have been set up by the container. |
destruction method | In a bean definition, a callback to be used when the container that contains the bean is destroyed. |
In the following example, we are going to look into an XML based configuration file which has different bean definitions. The definitions include lazy initialization (lazy-init), initialization method (init-method), and destruction method (destroy-method) as shown below. This configuration metadata file can be loaded either through BeanFactory or ApplicationContext
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation = "http://www.springframework.org/schema/beans <!-- A simple bean definition --> < bean id = "..." class = "..." > <!— Here will be collaborators and configuration for this bean --> </ bean > <!-- A bean definition which has lazy init set on --> < bean id = "..." class = "..." lazy-init = "true" > <!-- Here will be collaborators and configuration for this bean --> </ bean > <!-- A bean definition which has initialization method --> < bean id = "..." class = "..." init-method = "..." > <!-- Here will be collaborators and configuration for this bean --> </ bean > <!-- A bean definition which has destruction method --> < bean id = "..." class = "..." destroy-method = "..." > <!-- collaborators and configuration for this bean go here --> </ bean > <!-- more bean definitions can be written below --> </ beans > |