Bean life cycle in spring

Advertisement

This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring. Run the given bean example to retrieves the values of the bean using java file.

Bean life cycle in spring

This example gives you an idea on how to Initialize
bean in the program and also explains the lifecycle of bean in spring. Run the
given bean example to retrieves the values of the bean using java file. Here in the file given
below i.e. (context.xml) we have declare the bean definition.

Here is the file named Main.java through which we are retrieving
the properties of the bean which we have defined in the above file i.e. context.xml

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-Here
we are creating an instance of the XmlBeanFactory which is used to read bean
definition from an XML document

new ClassPathResource("context.xml"):-Creates a new ClassPathResource for ClassLoader
.Here the context.xml is the file which is to be loaded.

class BeanSupport implements InitializingBean :-Here
the InitializingBean interface is implemented by bean class. The use of
this interface here is to do some post processing actions when all the properties have been set
by the Bean Factory..