简体   繁体   English

使用Spring PropertySource无法通过绝对路径找到属性文件

[英]Cannot find a properties file by absolute path using Spring PropertySource

My Spring application throws FileNotFoundException when I try to load external properties file using @PropertySource annotation. 当我尝试使用@PropertySource注释加载外部属性文件时,我的Spring应用程序将引发FileNotFoundException

Configuration class: 配置类:

@Configuration
@PropertySource(value = "file:/home/myuser/dev/app_name.properties")
public class ConfigurationClass {

    @Autowired
    Environment env;

    public Connection getConnection() {
        ...
        String properties = env.getProperty("cbs.url");
        ...
    }
}

Try to give like this 尝试这样给

@PropertySource("file:/home/myuser/dev/app_name.properties")

And make sure that file available in that location . 并确保该文件在该位置可用
(or) (要么)
if you don't want file not found Exception when bootup your application then use below one @PropertySource("file:/home/myuser/dev/app_name.properties",ignoreResourceNotFound=true) 如果您不想在启动应用程序时找不到文件异常 ,则使用以下一个@PropertySource("file:/home/myuser/dev/app_name.properties",ignoreResourceNotFound=true)

you can override PropertySourcesPlaceholderConfigurer bean and provide file path there. 您可以覆盖PropertySourcesPlaceholderConfigurer bean并在那里提供文件路径。

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setLocations(new Resource[] {
        new FileSystemResource("/some/path/file.properties"),
        new FileSystemResource("/some/path/other-file.properties")
    });
    return configurer;
  }

It can be more flexible by passing the initial path as system param or env variable. 通过将初始路径作为系统参数或env变量传递,可以更加灵活。

new FileSystemResource(System.getenv("CONFIG_PATH") + "/some/path/file.properties")

After overriding bean, your application configuration could be simple like this.(no need to specify property source) 覆盖bean之后,您的应用程序配置可以像这样简单。(无需指定属性源)

@Configuration
public class ConfigurationClass {
   ...

   //configurations can be accessed like this

   @Value("${config.variable.name}")
   private String configValue;

}

I hope this helps. 我希望这有帮助。

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

相关问题 Spring 3 @PropertySource在Wildfly 8模块中找不到属性文件 - Spring 3 @PropertySource cannot find properties file in Wildfly 8 module 在Spring上下文中查找* .properties文件的绝对路径 - Find absolute path of *.properties file in Spring context 使用@PropertySource的Spring属性配置 - Spring properties configuration using @PropertySource 如何在Spring中使用@PropertySource加载的属性文件中配置“动态键” - how to configure 'dynamic key' in properties file loaded using @PropertySource in Spring 使用@PropertySource和外部JSON文件配置Spring属性 - Spring properties configuration using @PropertySource with external JSON file 无法使用@PropertySource将.properties文件注入Spring MVC 4.3 - Not able to inject .properties file into Spring MVC 4.3 using @PropertySource 使用相应的专用“属性”文件(使用@PropertySource)设置时会覆盖单个弹簧“beans”属性值 - Individual spring "beans" properties value overridden while setting with corresponding dedicated "properties" file (using @PropertySource) Spring PropertySource找不到的属性 - Properties not found with Spring PropertySource Spring boot fat jar 无法从 application.properties 中定义的绝对路径读取文件 - Spring boot fat jar cannot read file from absolute path defined in application.properties Spring 4 @PropertySource相对路径 - spring 4 @PropertySource relative path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM