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
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>