【问题标题】:Spring Boot - inject map from properties file [duplicate]Spring Boot - 从属性文件注入地图[重复]
【发布时间】:2017-08-23 04:57:34
【问题描述】:

属性文件如下所示:

url1=path_to_binary1
url2=path_to_binary2

根据this我尝试了以下方法:

@Component
@EnableConfigurationProperties
public class ApplicationProperties {
    private Map<String, String> pathMapper;

    //get and set
}

在另一个组件中,我自动装配了 ApplicationProperties:

@Autowired
private ApplicationProperties properties;         
      //inside some method:
      properties.getPathMapper().get(appName);

产生NullPointerException

如何纠正?

更新

根据 user7757360 的建议,我有正确的建议:

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {

和属性文件:

app.url1=path_to_binary1
app.url2=path_to_binary2

还是不行

更新 2

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {
    private Map<String, String> app;

application.properties内部:

app.url1=path_to_binary1
app.url2=path_to_binary2

还是不行

【问题讨论】:

标签: java spring spring-boot configuration


【解决方案1】:

NullPointerException 可能来自空的ApplicationProperties

所有自定义属性都应标注@ConfigurationProperties(prefix="custom")。 之后,在您的主类(具有主方法的类)上,您必须添加@EnableConfigurationProperties(CustomProperties.class)。 对于自动完成,您可以使用:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

如果您使用不带前缀的@ConfigurationProperties,则仅使用字段名称。您属性中的字段名称。在您的情况下path-mapper,接下来是具体的keyvalue。示例:

path-mapper.key=value

请记住,更改您自己的属性后,您需要重新加载应用程序。示例:

https://github.com/kchrusciel/SpringPropertiesExample

【讨论】:

  • 前缀是什么?
  • 您可以使用前缀 custom.pathMapper.value=my value,或使用 @ConfigurationProperties("") 作为 pathMapper.value=my value。这个值需要在application.properties中添加。
  • 请阅读主题更新。看起来与您的建议相同的另一个答案
  • 你使用 app.url1=path_to_binary1 但变量名是 pathMapper
  • 应该如何使用?
【解决方案2】:

如果您可以为属性文件提供更具体的示例,将会很有帮助。您应该在 url1 和 url2 中具有相同的前缀,然后您可以使用

@ConfigurationProperties(prefix="my")

my.pathMapper.url1=path_to_binary1 my.pathMapper.url2=path_to_binary2

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="my")
public class ApplicationProperties {
    private Map<String, String> pathMapper;

    //get and set for pathMapper are important
}

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading-yaml查看更多信息

【讨论】:

  • 属性文件名在哪里写?
  • 根据您的更新,您的属性文件应该是 app.app.url1=path_to_binary1 app.app.url2=path_to_binary2 我建议使用不同的前缀
  • 假设您使用默认值,spring-boot 会查找默认名称application.properties,因此无需在任何地方写入属性文件名。只需确保application.propertiessrc/main/resources 中,如@kchrusciel 提供的示例所示
【解决方案3】:

将 your.properties 文件放在 src/main/resources 下,或者将其作为一个

@Configuration
@PropertySource("classpath:your.properties")
public class SpringConfig(){}

或者在你的 Spring yourApplicationContext.xml 中将其作为 PropertyPlaceholderConfigurer

然后使用类似的属性值

@Value("app.url1")
String path_to_binary1;

@Value("app.url2")
String path_to_binary2;

// ...

System.out.println(path_to_binary1+path_to_binary2);

【讨论】:

    【解决方案4】:

    您需要从属性文件中提供两件事。首先,您需要有一个类,该类具有配置和目标字段来保存属性文件中的数据。

    @Configuration
    @PropertySource("classpath:myprops.properties")
    @ConfigurationProperties("props")
    @Component
    public class Properties{
       private Map<String,String> map = new HashMap<String,String>();
       // getter setter
    }
    

    接着定义名为 myprops.properties 的属性文件,所有属性为 props

    props.map.port = 443
    props.map.active = true
    props.map.user = aUser
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 2022-01-20
      • 2014-09-15
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 2019-06-04
      相关资源
      最近更新 更多