bean element



<bean> element is used in the XML file.
It is XML analogue to Java Configuration's @Bean annotation.
Example
<bean class="com.data.User"/>
  • In the class attribute, provide the fully qualified class name.
  • Here it will create a bean of User class.
  • As here Id not specified for the bean the generated Id for the bean will be com.data.User#0
  • If we create another bean element with the same class name then it will have id com.data.User#1
Bean with Id:
<bean id="user1" class="com.data.User"/>
  • When we provide a <bean> element then spring creates the respective bean for us by using its default constructor.
  • Bean creating with XML configuration is passive than java based config but this way of creating bean is very less powerful than java config way.
  • In XML based configuration, you need to verify the class name if you change or refractor the class name.
Also you can provide the property based dependencies
<bean id="user" class="com.data.User">
<property name="address" ref="addr" />
</bean>
  • Here it will inject the "addr" reference using the property setter method