简体   繁体   English

如何将一个 spring bean 的父属性设置为另一个 bean 的属性?

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

Is it possible to use the property of one Spring bean to set the parent attribute of another bean?是否可以使用一个 Spring bean 的属性来设置另一个 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.作为背景信息,我正在尝试更改项目以使用容器提供的数据源,而无需对 Spring 配置进行大量更改。

Simple class with a property I want to use带有我想使用的属性的简单 class

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 spring beans 配置的相关位

<!-- 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:或者如果我从上面删除 Spring EL 我得到这个:

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... springPreloads bean 被用作“工厂”,但只是为了获取它的 dataSource 属性,然后插入各种属性......

I'm guessing springPreloads.dataSource is an instance of oracle.jdbc.pool.OracleDataSource?我猜 springPreloads.dataSource 是 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM