外部属性文件在Spring中

huangapple go评论66阅读模式
英文:

External properties file in spring

问题

我有一个使用Maven构建的Java命令行程序,涉及Spring框架但不是Spring Boot。在使用Maven的maven-assembly-plugin插件构建包含所有依赖的JAR文件时,会包含application.properties文件。我想知道如何读取外部的application.properties文件而不是JAR包内部的那个。

我目前是这样读取属性文件的:

@PropertySource(value = { "classpath:application.properties" })

如果我打印类路径(classpath),类路径只包括JAR包路径,而不包括当前目录。

英文:

I have a java,spring and not spring boot command line program with maven , which when i build a jar with all dependencies using maven-assembly-plugin , it includes application.properties, what i need to know is how to read the external application.properties and not the jar one.

I am reading the property file as:

@PropertySource(value = { "classpath:application.properties" })

If I print the classpath, the classpath does only include the jar and not the current directory.

答案1

得分: 0

这里是你可以做的:

@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource(value = "file:/etc/config/my-custom-config.properties", ignoreResourceNotFound = true)
})

这个 ignoreResourceNotFound 自从 Spring 4.3 版本就可用,而且很容易理解。

你也可以选择 "编程式" 方法。在纯 Spring 中(不是你在问题中提到的 Spring Boot):

@Configuration
public class CommonConfig {
...
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
            PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
            ppc.setLocations(new FileSystemResource("/etc/config/my-custom-config.properties"),
                    new ClassPathResource("config/application.properties"));
            return ppc;
        }
...
}

FileSystemResource 用于访问外部文件系统中的资源。

ClassPathResource 用于访问类路径中的资源。

英文:

Here is what you can do:

@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource(value = "file:/etc/config/my-custom-config.properties", ignoreResourceNotFound = true)
})

This ignoreResourceNotFound is available since spring 4.3 and is pretty self-explanatory.

You can also opt for "programmatic" approach. In pure spring (not spring boot as you've mentioned in the question):

@Configuration
public class CommonConfig {
...
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
            PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
            ppc.setLocations(new FileSystemResource("/etc/config/my-custom-config.properties"),
                    new ClassPathResource("config/application.properties"),
            return ppc;
        }
...
}

FileSystemResource is for accessing resources available externally in the file system

ClassPathResource is for accessing resources in the classpath

答案2

得分: 0

我们使用动态生成的配置文件,其中注入了特定环境的机密信息。使一切保持一致的组合如下:

代码:

myFunction(@Value("${custom.configuration.name}") final String customConfigurationName) {
    InputStream inputStream = new ClassPathResource(customconfigFileName).getInputStream();
    // ...
}

模板 - 添加一个硬性要求的路径,以确认文件在磁盘上并已加载:

- name: SPRING_CONFIG_LOCATION
  value: file:./config/schema-${ENV}.yml
  ...
- name: custom.configuration.name
  value: file:config/schema-${ENV}.yml
英文:

We use dynamically generated configuration files that are injected with environment specific secrets. The combination to get everything aligned is as follows:

Code:

    myFunction(@Value("${custom.configuration.name}") final String customConfigurationName) {
        InputStream inputStream=new ClassPathResource(customconfigFileName).getInputStream()){}
        ...
    }

Template - Add a hard required path as this confirms the file is on disk and loaded:

- name: SPRING_CONFIG_LOCATION
  value: file:./config/schema-${ENV}.yml
  ...
- name: custom.configuration.name
  value: file:config/schema-${ENV}.yml

huangapple
  • 本文由 发表于 2020年8月16日 15:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63434472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定