Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all properties with prefix 'abc.' from PropertyPlaceholderConfigurer

In spring context file, I use org.springframework.beans.factory.config.PropertyPlaceholderConfigurer to load several configuration files:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>a.properties</value>
            <value>b.properties</value>
            <value>c.properties</value>
        </list>
    </property>
</bean>

The a.properties, b.properties, c.propertes may have some hibernate configurations which have prefix of abc.:

abc.hibernate.show_sql=true
abc.hibernate.default_schema=myschema
abc.hibernate.xxx=xxx
abc.hibernate.xxx=xxx
abc.hibernate.xxx=xxx
abc.hibernate.xxx=xxx
abc.hibernate.xxx=xxx
abc.hibernate.xxx=xxx

Now I want to define a hibernate session factory:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <util:properties>
            <prop key="hibernate.show_sql">${abc.hibernate.show_sql}</prop>
            <prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>
            <prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>
            <prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>
            <prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>
            <prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>
        </util:properties>
    </property>
</bean>

You can see I have to write the property in bean declaration again and again:

<prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>

Is there any way to just tell spring to get all properties which have prefix abc.?

So I can write:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <something prefix="abc" /> <!-- TODO -->
    </property>
</bean>

Is it possible or is there any other simple solutions for it?

like image 871
Freewind Avatar asked Dec 27 '13 07:12

Freewind


1 Answers

You can use something like the following class to extend java.util.Properties:

import java.util.Enumeration;
import java.util.Properties;

public class PrefixedProperties extends Properties {
    public PrefixedProperties(Properties props, String prefix){
        if(props == null){
            return;
        }

        Enumeration<String> en = (Enumeration<String>) props.propertyNames();
        while(en.hasMoreElements()){
            String propName = en.nextElement();
            String propValue = props.getProperty(propName);

            if(propName.startsWith(prefix)){
                String key = propName.substring(prefix.length());
                setProperty(key, propValue);
            }
        }
    }    
}

Then you can define sessionFactory as following:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <bean id="sessionFactoryProperties" class="PrefixedProperties">
            <constructor-arg ref="props"/> <!-- reference to all properties -->
            <constructor-arg value="abc.hibernate."/> <!-- prefix -->
        </bean>
    </property>
</bean>

I don't see any other possibility to filter properties.

like image 137
Vlad Avatar answered Oct 07 '22 00:10

Vlad