简体   繁体   English

如何获取Spring框架中属性文件中定义的所有键

[英]How to get all the keys defined in property file in spring framework

I need to get all the keys in a property file and store it into a bean variable. 我需要获取属性文件中的所有键,并将其存储到bean变量中。 For example I need something like below 例如我需要下面的东西

abc=df
sad=tyh

Then I need to get {abc,sad} . 然后我需要获取{abc,sad} All this thing I need to do using spring framework .Is this can be possible (I know probability of getting this type requirement is very less but just need to know is it possible) 我需要使用spring框架做所有这些事情。这是可能的(我知道达到这种类型要求的可能性非常小,但只需要知道是否可能)

you can try try like define bean in applicationContext like 您可以尝试像在applicationContext中定义bean一样

<bean id="applicationProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
                <value>classpath*:*.properties</value>
            </list>
        </property>
    </bean>

and declare like 并声明像

@Resource(name = "applicationProperties")
    private Properties properties;

and getProperty like 和getProperty一样

String str = properties.getproperty('keyname');
Enumeration allkeys = properties.propertyNames();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM