Spring Bean Configuration

spring bean configuration and dependencies in details

In this tutorial, we will learn some important concepts of spring bean configuration and dependencies. Using Constructor arguments and property set, we can inject dependency to spring beans.

But there are some other approaches we can use in our projects.

The p-namespace

The p-namespace is basically a shortcut to set the beans property values. Instead of using traditional <property/> element, p-namespace enables you to use the bean element’s attribute to set the values.

Consider the below example :
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean name="serviceBean1" class="com.package.ServiceBean">
 <property name="serviceName" value="getLocation"/>
 </bean>

 <bean name="serviceBean2" class="com.package.ServiceBean"
 p:serviceName="getLocation"/>
</beans>

Java Class
public class ServiceBean {

 private String serviceName;
 private UserDao userDao;
}

As you can see in the above example, p-namespace serviceName called in the bean definition as p:serviceName, which tells spring framework to include a property declaration. You can also call reference type p-namesapce attribute as below :
<bean name ="serviceBean" class="com.package.ServiceBean"
 p:userDao-ref="userDao" />

<bean id="userDao" class="com.package.UserDaoImpl" />

where -ref tells that this is not a simple value rather it is a reference to another bean.

The c-namespace

Like p-namesapce, c-namespace is also a shortcut to set constructor argument as part of bean definition. Using c-namespace, you can directly configure constuctor arguments rather than <constructor-arg /> element.

As below :
<bean id="servicebean" class="com.package.ServiceBean"
 c:serviceName="getLocation"
 c:userDao-ref="userDao" />

where c:serviceName, c:userDao is the first and second argument to the ServiceBean constructor. And -ref tells that this is not a simple argument rather it is reference to other bean.

Inner Beans

Inner bean is same as annomous class in Java with no name. In spring bean definition, we can create inner bean by using <bean/> element inside <constructor-arg/> or <property/> elements.

Example :
<bean id="serviceBean" class="com.package.ServiceBean">
 <property name="userDao">
 <bean class="com.package.UserDaoImpl">
 <property name="userId" value="22" />
 <property name="userName" value="ashish" />
 </bean>
 </property>
</bean>
An inner bean does not require bean id or name, if specified container simply ignores it. The container also don’t use scope for inner beans. Inner beans typically share their containing bean’s scope.

Spring Collections

Spring allows <list/>, <set/>, <map/> and <props/> as collection elements, you can set the properties and arguments of Java Collection types such as List,Map,Set and Properties.

For Example
<bean id="springCollectionExample" class="com.pacakge.SpringCollectionExample">

 <property name="propC">
 <props>
 <prop key="key1">value1</prop>
 <prop key="key2">value2</prop>
 </porps>
 </property>

 <property name="listC">
 <list>
 <value>list value1</value>
 <ref bean="refBean" />
 </list>
 </property>

 <property name="mapC">
 <map>
 <entry key="key1" value="value1" />
 <entry key="refKey" value-ref="valueRef" />
 </map>
 </property>
 
 <property name="setC">
 <set>
 <value>set value</value>
 <ref bean="refBean" />
 </set>
 </property>
</bean>

Bean Inheritance and Collection Merging

Spring container also support bean inheritance and merging of collection from parent bean to child bean.

for Example
<beans>
 <bean id="parentBean" abstract="true" class="..">
 <property name="phoneDiary">
 <props>
 <prop key="ashish">9898989898</prop>
 <props>
 </property>
 </bean>

 <bean id="childBean" parent="parentBean" class="..">
 <property name="phoneDiary">
 <props merge="true">
 <prop key="ankit">9877878876</prop>
 </props>
 </property>
 </bean>
</beans>

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *