View RSS Feed

Spring Framework

Spring Inner Beans

Rate this Entry
by , 11-16-2011 at 04:15 PM (1103 Views)
Rather than configure beans separately and allow for Springís DI container to manage the injection of these beans into other beans, another means of wiring bean references is to embed a <bean> element directly in the <property> element. For example, the carDao property of the carService bean can be wired as follows:

Java Code:
<bean id="carService" class="com.acme.springexample.service.CarServiceImpl">
<property name="carDao"> 
<bean class="com.acme.springexample.dao.CarDaoImpl"/> 
</property>
</bean>
This simplifies issues of resolving references for dependency injection but the drawback of wiring a bean references in this manner is that you canít reuse the instance of CarDaoImpl anywhere else. This instance is created specifically for use by the carService bean. The other issue depends on whether there is a deep dependency between the two beans. If you donít want the actual bean instance to be accessible without a wrapper bean, then there is no problem with using inner beans. If this is not the case, using inner-bean definitions impacts the readability of the XML.

Submit "Spring Inner Beans" to Facebook Submit "Spring Inner Beans" to Digg Submit "Spring Inner Beans" to del.icio.us Submit "Spring Inner Beans" to StumbleUpon Submit "Spring Inner Beans" to Google

Updated 11-30-2011 at 11:51 AM by Spring Framework

Categories
Dependency Injection , Bean

Comments