在运行时根据输入从属性文件中获取值 - java Spring [英] Get the values from the properties file at runtime based on the input - java Spring

查看:45
本文介绍了在运行时根据输入从属性文件中获取值 - java Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的 colour.rperties 文件

I have my colour.roperties file as

rose = red
lily = white
jasmine = pink

我需要获取颜色的值

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

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

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?

推荐答案

使用@Value 无法完成您所描述的操作,但您可以这样做,这几乎是同一件事:

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

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

这篇关于在运行时根据输入从属性文件中获取值 - java Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆