java - 如何从 PropertyPlaceholderConfigurer 获取前缀为 'abc.' 的所有属性

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

a.propertiesb.propertiesc.propertes 可能有一些前缀为 abc 的 hibernate 配置。 :

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

现在我想定义一个 hibernate session 工厂:

<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);
            }
        }
    }    
}

然后你可以定义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>

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

关于java - 如何从 PropertyPlaceholderConfigurer 获取前缀为 'abc.' 的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20796882/

相关文章:

java - 通过字段 'employeeDao' 表示不满足的依赖关系;

java - 如何动态注入(inject)spring bean(原型(prototype)范围)

mysql - docker 中的 Spring 应用程序如何访问主机或云 mysql 中的 mysql?

java - 在 Spring Controller 中声明互参数

c# - 为什么要使用成员变量?

c++ - 通过匿名类型成员结构的 C++ 属性的一般后果是什么

javascript - toString() 和 toLocaleString() 是 Javascript 中的属性和/或方法吗?

java - 我正在使用 Authorize.net 支付网关并通过直接邮寄方法进行交易,是否可以仅验证卡信息

java - 使用 displaytag JSP 选项卡库对 HTML 表格进行排序

java - 将 Jackson 与泛型类型一起使用