user3485807
user3485807

Reputation: 61

spring replace bean name in config file with value from property file

I have a bean config file as follows

<bean id="myFactory" class="com.public.Factory">
    <property name="dataSourceAdaptor" ref="${value.from.property file}Adaptor" />
</bean>

How do i achieve this.

I added the following to top of the config file

<util:properties id="myProperties" location="classpath:app.properties"/>

and then tried to refer to the value using ${} but i get an error stating ${value.from.property file}Adaptor is not a valid bean

I cannot put the whole name (xyzAdaptor) in the property file as the value in the property file is an institution and there are multiple adaptors for each institution.

for example xzyDisplayAdaptor, xyzProductAdaptor, xyzDatasourceAdaptor

The xyz client can change to say abc client and i want to be able to change the value in property file to abc and all the abc related beans will be injected.

Upvotes: 1

Views: 1261

Answers (2)

Prasad
Prasad

Reputation: 3795

Try this with Spel:

    <util:properties id="myProperties" location="classpath:app.properties"/>

    <bean id="myFactory" class="com.public.Factory">
        <property name="dataSourceAdaptor" ref="#{'${value.from.property file}'+'Adaptor'}" />
    </bean>

Upvotes: 1

Priyesh
Priyesh

Reputation: 2071

The util:properties tag is used to create an instance of java.util.Properties. I think what you need is a PropertyPlaceholderConfigurer. e.g.,

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="singleton">
           <property name="searchSystemEnvironment" value="true" />
           <property name="ignoreResourceNotFound" value="true" />
           <property name="locations">
                <list>
                    <value>classpath:app.properties</value>
                </list>
            </property>
</bean>

Upvotes: 1

Related Questions