How to set the parent attribute of one spring bean to a property of another bean?

Go To StackoverFlow.com

0

Is it possible to use the property of one Spring bean to set the parent attribute of another bean?

As background info, I'm trying to change a project to use a container-provided data source without making huge changes to the Spring config.

Simple class with a property I want to use

package sample;

import javax.sql.DataSource;

public class SpringPreloads {

    public static DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    //This is being set before the Spring application context is created
    public void setDataSource(DataSource dataSource) {
        SpringPreloads.dataSource = dataSource;
    }

}

Relevant bits of spring beans configuration

<!-- new -->
<bean id="springPreloads" class="sample.SpringPreloads" />

<!-- How do I set the parent attribute to a property of the above bean? -->
<bean id="abstractDataSource" class="oracle.jdbc.pool.OracleDataSource" 
abstract="true" destroy-method="close" parent="#{springPreloads.dataSource}">
    <property name="connectionCachingEnabled" value="true"/>
    <property name="connectionCacheProperties">
        <props>
            <prop key="MinLimit">${ds.maxpoolsize}</prop>
            <prop key="MaxLimit">${ds.minpoolsize}</prop>
            <prop key="InactivityTimeout">5</prop>
            <prop key="ConnectionWaitTimeout">3</prop>
        </props>
    </property>
</bean>

Exception when tested

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '#{springPreloads.dataSource}' is defined

or if I remove the Spring EL from the above I get this:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springPreloads.dataSource' is defined
2012-04-03 21:28
by user506069
I'm not sure what you're trying to do here. The parent is the template configuration, which is different class to the abstractDataSource bean. There is a nice "trick" to inject attributes from one bean to another, but that doesn't seem to be what you're after - Adam 2012-04-03 21:40


1

I think this is what you're after. The springPreloads bean is used as a "factory", but only to get hold of its dataSource attribute, which is then plugged with various properties...

I'm guessing springPreloads.dataSource is an instance of oracle.jdbc.pool.OracleDataSource?

<bean id="springPreloads" class="sample.SpringPreloads" />

<bean id="abstractDataSource" factory-bean="springPreloads" factory-method="getDataSource">
    <property name="connectionCachingEnabled" value="true" />
    <property name="connectionCacheProperties">
        <props>
            <prop key="MinLimit">${ds.maxpoolsize}</prop>
            <prop key="MaxLimit">${ds.minpoolsize}</prop>
            <prop key="InactivityTimeout">5</prop>
            <prop key="ConnectionWaitTimeout">3</prop>
        </props>
    </property>
</bean>
2012-04-03 21:49
by Adam
Ads