如何从属性占位符配置器获取前缀为"abc."的所有属性



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

a.propertiesb.propertiesc.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

现在我想定义一个休眠会话工厂:

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

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

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

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

所以我可以写:

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

是否有可能或有任何其他简单的解决方案?

您可以使用类似于以下类的内容来扩展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);
            }
        }
    }    
}

然后,您可以按如下方式定义会话工厂:

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

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

正如肖恩所说,PropertyPlaceHolderConfigurer不会公开他的属性,但你可以使用反射来过滤它们。

public static Properties filterProperties(String prefix, PropertyPlaceholderConfigurer ppc) {
        Properties props = new Properties();
        Method method = ReflectionUtils.findMethod(PropertiesLoaderSupport.class, "mergeProperties");
        ReflectionUtils.makeAccessible(method);
        Properties all = (Properties) ReflectionUtils.invokeMethod(method, ppc);
        for (String key : all.stringPropertyNames()) {
            if (key.startsWith(prefix))
                props.setProperty(key, all.getProperty(key));
        }
        return props;
    }

并注射喷洒

<bean id="ppc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
...
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties"  value="#{T(SomeClass).filterProperties('abc.', @ppc)}" />
</bean>

PropertyPlaceHolderConfigurer 是一种特殊的构造,仅用于创建 bean。它不会以可以访问的方式公开基础属性。

一种可能的解决方法是 子类 PropertyPlaceHolderConfigurer ,正如我在之前的回答中概述的那样。

另一种方法是有一个自定义的PropertiesFactoryBean,像这样:

public class PrefixedPropertyFactoryBean extends AbstractFactoryBean<Properties> {
    private List<Resource> locations;
    public void setLocations(List<Resource> locations) { this.locations = locations; }
    private String prefix;
    public void setPrefix(String prefix) { this.prefix = prefix; }
    @Override public Class<?> getObjectType() { return Properties.class; }
    @Override
    protected Properties createInstance() throws Exception {
        Properties properties = new Properties();
        for (Resource resource : locations) {
           properties.load( resource.getInputStream());
        }
        final Iterator<Object> keyIterator = properties.keySet().iterator();
        while(keyIterator.hasNext()) {
            if(!keyIterator.next().toString().startsWith(prefix))
            keyIterator.remove();
        }
        return properties;
    }
}

您可以在 XML 中使用它,如下所示:

<bean id="hibernateProps" class="some.path.PrefixedPropertyFactoryBean">
    <property name="locations">
        <list>
            <value>a.properties</value>
            <value>b.properties</value>
            <value>c.properties</value>
        </list>
    </property>
    <property name="prefix" value="abc.hibernate" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties" ref="hibernateProps" />
</bean>

这当然是多余的,因为您可能仍然需要连接PropertyPlaceHolderConfigurer来设置其他 bean。

相关内容

最新更新