Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse Spring XML fragments

Tags:

java

spring

xml

I am working on Spring based application. The XML is simple but contains several almost identical fragments. For example I have 5 different DAO objects, 2 queues etc. Configuration of each DAO looks like:

<bean id="deviceDaoTarget" class="com.mycompany.dao.hibernate.DeviceDAOHibernateImpl"
    autowire="byName" />

<bean id="deviceDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
        <value>com.mycompany.dao.DeviceDAO</value>
    </property>
    <property name="interceptorNames">
        <list>
            <value>hibernateInterceptor</value>
            <value>deviceDaoTarget</value>
        </list>
    </property>
</bean>

I'd be happy to use some kind of import with parameters. For example I'd like to create parametrized configuration of DAO like this:

<bean id="${dao.target}" class="${dao.class}"
    autowire="byName" />

<bean id="deviceDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
        <value>${dao.interface}</value>
    </property>
    <property name="interceptorNames">
        <list>
            <value>hibernateInterceptor</value>
            <value>${dao.target}</value>
        </list>
    </property>
</bean>

and then call it several times with different parameters, e.g.:

<import resource="spring-dao.xml">
    <param name="dao.interface">com.mycompany.dao.hibernate.DeviceDAO</param>
    <param name="dao.class">com.mycompany.dao.hibernate.DeviceDAOHibernateImpl</param>
    <param name="dao.target">deviceDaoTarget</param>
</import>

Is something like this possible?

like image 348
AlexR Avatar asked Nov 25 '10 17:11

AlexR


1 Answers

You can define a <bean id="parentBean" abstract="true" ...>...</bean> with the common configuration and then have <bean id="childBean" parent="parentBean" ...>...</bean> with just specific configuration for that bean.

like image 138
gpeche Avatar answered Oct 08 '22 09:10

gpeche