在运行时根据输入从属性文件中获取值 - java Spring

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21609271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:38:57  来源:igfitidea点击:

Get the values from the properties file at runtime based on the input - java Spring

javaspringproperties-file

提问by pinky

I have my colour.roperties file as

我有我的 colour.rperties 文件

rose = red
lily = white
jasmine = pink

I need to get the value for colour as

我需要获得颜色的价值

String flower = runTimeFlower;
@Value("${flower}) String colour;

where flower value we will get at runtime. How can I do this in java Spring. I need to get a single value (from among 50 values defined in the properties file )at runtime based on the user input. If i cannot use @Value , Could you tell me other ways to handle this please?

我们将在运行时获得的花值。我怎样才能在 java Spring 中做到这一点。我需要在运行时根据用户输入获取一个值(从属性文件中定义的 50 个值中)。如果我不能使用 @Value ,你能告诉我其他处理方法吗?

回答by SergeyB

There is no way to do what you are describing using @Value, but you can do this, which is the same thing pretty much:

没有办法使用@Value 来做你所描述的事情,但你可以这样做,这几乎是一样的:

package com.acme.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class Example {
    private @Autowired Environment environment;

    public String getFlowerColor(String runTimeFlower) {
        return environment.resolvePlaceholders("${" + runTimeFlower + "}");
    }
}

回答by Emerson Farrugia

The PropertySources which Spring reads from won't know the value of the flowervariable, so @Valuewon't work.

PropertySourceSpring 读取的s 不知道flower变量的值,所以@Value不会工作。

Inject a Propertiesobject or a Map. Then just look up the colour using the property name or key, respectively, e.g.

注入一个Properties对象或一个Map. 然后分别使用属性名称或键查找颜色,例如

<util:properties id="appProperties" location="classpath:app.properties" />

...

@Autowired 
@Qualifier("appProperties")
private Properties appProperties;

...

appProperties.getProperty(flower);

回答by Boyan

What @ike_love says it correct, but why don't you just load the properties in memory on app start and then you can resolve your flower taking value from a map? In my mind you don't need to delegate every simple thing like this to Spring. Anyway I don't know your Spring config, but in order Spring to be able to load the properties you need to define a PropertyPlaceholderConfigurer to tell where are the property files:

@ike_love 说的是对的,但为什么不在应用程序启动时加载内存中的属性,然后您可以解决从地图中获取值的花?在我看来,您不需要将像这样的每件简单事情都委托给 Spring。无论如何,我不知道您的 Spring 配置,但是为了 Spring 能够加载属性,您需要定义一个 PropertyPlaceholderConfigurer 来告诉属性文件在哪里:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:app.properties</value>
            </list>
        </property>
</bean>