java 如何在没有环境的情况下读取 application.properties 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44499306/
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-11-03 08:12:46  来源:igfitidea点击:

How to read application.properties file without Environment

javaspringspring-boot

提问by Braj

Please can you help me to read the properties from application.properties file in Spring Boot, without autowiring the Environmentand without using the Environment?

请您帮我从 Spring Boot 中的 application.properties 文件中读取属性,而无需自动装配Environment和使用Environment?

No need to use ${propname}either. I can create properties object but have to pass my properties file path. I want to get my prop file from another location.

也没必要用${propname}。我可以创建属性对象,但必须传递我的属性文件路径。我想从另一个位置获取我的道具文件。

回答by OrangeDog

This is a core Java feature. You don't have to use any Spring or Spring Boot features if you don't want to.

这是一个核心 Java 特性。如果您不想,您不必使用任何 Spring 或 Spring Boot 功能。

Properties properties = new Properties();
try (InputStream is = getClass().getResourceAsStream("application.properties")) {
  properties.load(is);
}

JavaDoc: http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html

JavaDoc:http: //docs.oracle.com/javase/8/docs/api/java/util/Properties.html

回答by Vladislav Kysliy

OrangeDog solution didn't work for me. It generated NullPointerException.

OrangeDog 解决方案对我不起作用。它生成了 NullPointerException。

I've found another solution:

我找到了另一个解决方案:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try (InputStream resourceStream = loader.getResourceAsStream("application.properties")) {
    properties.load(resourceStream);
} catch (IOException e) {
    e.printStackTrace();
}

回答by Abbin Varghese

If everything else is properly set, you can the annotation @Value. Springboot will take care of loading the value from property file.

如果其他一切都正确设置,您可以注释@Value。Springboot 将负责从属性文件加载值。

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.beans.factory.annotation.Value;

@Configuration
@PropertySource("classpath:/other.properties")
public class ClassName {
  @Value("${key.name}")
  private String name;
}

回答by Smaniotto

To read application.properties just add this annotation to your class:

要阅读 application.properties 只需将此注释添加到您的类中:

@ConfigurationProperties
public class Foo { 
}

If you want to change the default file

如果要更改默认文件

@PropertySource("your properties path here")
public class Foo { 
}

回答by Bohdan Levchenko

Try to use plain old Properties.

尝试使用普通的 old Properties

final Properties properties = new Properties();
properties.load(new FileInputStream("/path/config.properties"));
System.out.println(properties.getProperty("server.port"));

In case you need to use that external properties file in your configuration it can be accomplished with @PropertySource("/path/config.properties")

如果您需要在配置中使用该外部属性文件,则可以通过 @PropertySource("/path/config.properties")

回答by Venkatesh Rajmendram

Adding to Vladislav Kysliy's elegant solution, below code can be directly plugged as REST API Call to get all the key/value of application.properties file in Spring Boot without knowing any key. Additionally, If you know the Key you can always use @Value annotation to find the value.

添加到 Vladislav Kysliy 的优雅解决方案中,下面的代码可以直接插入为 REST API 调用,以在不知道任何键的情况下获取 Spring Boot 中 application.properties 文件的所有键/值。此外,如果您知道 Key,您始终可以使用 @Value 注释来查找该值。

@GetMapping
@RequestMapping("/env")
public java.util.Set<Map.Entry<Object,Object>> getAppPropFileContent(){
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    java.util.Properties properties = new java.util.Properties();
    try(InputStream resourceStream = loader.getResourceAsStream("application.properties")){
        properties.load(resourceStream);
    }catch(IOException e){
        e.printStackTrace();
    }
    return properties.entrySet();
}