如何获得前缀为"abc"的所有属性.来自PropertyPlaceholderConfigurer

Fre*_*ind 8 java spring properties

在spring上下文文件中,我org.springframework.beans.factory.config.PropertyPlaceholderConfigurer用来加载几个配置文件:

<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>
Run Code Online (Sandbox Code Playgroud)

a.properties,b.properties,c.propertes可能具有的前缀一些休眠的配置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
Run Code Online (Sandbox Code Playgroud)

现在我要定义一个hibernate会话工厂:

<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>
Run Code Online (Sandbox Code Playgroud)

您可以看到我必须一次又一次地在bean声明中编写属性:

<prop key="hibernate.xxx">${abc.hibernate.xxx}</prop>
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉spring获取所有具有前缀的属性abc.

所以我可以写:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <something prefix="abc" /> <!-- TODO -->
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

是否有可能或者还有其他简单的解决方案吗?

小智 4

您可以使用类似以下类的内容来扩展 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);
            }
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

然后你可以定义sessionFactory如下:

<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>
Run Code Online (Sandbox Code Playgroud)

我没有看到任何其他过滤属性的可能性。