Spring Boot - 从属性文件注入地图[重复]

     2023-02-26     65

关键词:

【中文标题】Spring Boot - 从属性文件注入地图[重复]【英文标题】:Spring Boot - inject map from properties file [duplicate] 【发布时间】: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

还是不行

【问题讨论】:

您可以在这里找到更好的答案:How to fill HashMap from java property file with Spring @Value 【参考方案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

【讨论】:

Spring Boot - 从 YAML 文件中注入地图

】SpringBoot-从YAML文件中注入地图【英文标题】:SpringBoot-InjectingamapfromaYAMLfile【发布时间】:2018-09-2508:49:36【问题描述】:我尝试了几个版本,但都没有工作。将其作为地图注入的正确方法是什么:application.ymlalias:name:titledesc:descr... 查看详情

在spring boot中从属性文件中注入值数组

】在springboot中从属性文件中注入值数组【英文标题】:Injectingarrayofvaluesfrompropertiesfileinspringboot【发布时间】:2018-10-1505:17:33【问题描述】:好的,所以我有一个config.properties..:market.curpairs[0].name=EuroDollarmarket.curpairs[0].symbol=EURUSD... 查看详情

在单元/集成测试阶段向 Spring Boot 属性文件注入/覆盖属性值

】在单元/集成测试阶段向SpringBoot属性文件注入/覆盖属性值【英文标题】:Inject/overrideapropertiesvaluetoaSpringBootpropertiesfileinunit/integrationtestsphase【发布时间】:2018-11-2609:42:28【问题描述】:使用SpringBoot和Testcontainers我需要一种方法... 查看详情

spring-boot-配置文件值注入

...属性的值,映射到这个组件中*@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;  默认在全局配置文件中获取值的*prefix="person":配置文件中哪个下面的所有属性进行一一映射**只有这个... 查看详情

如何将 application.yml 中的地图注入 Spring Boot 中的字段?

】如何将application.yml中的地图注入SpringBoot中的字段?【英文标题】:Howtoinjectamapfromapplication.ymlintoafieldinSpringBoot?【发布时间】:2020-03-0105:00:35【问题描述】:这是参考这个问题。SpringBoot-injectmapfromapplication.yml在那个问题上,他... 查看详情

spring-boot配置文件使用

配置文件格式两种application.properties和application.yml.yml配置的使用1、使用@Value("${配置文件中的属性名}")注解注入配置内容2、在配置中引用配置  ${配置文件中的属性名}3、配置注入到实体类注解@ConfigurationProperties(prefix="girl") &nb... 查看详情

如何在 Spring Boot 中将属性注入测试类?

】如何在SpringBoot中将属性注入测试类?【英文标题】:HowtogetpropertiesinjectedintotestclassinSpringBoot?【发布时间】:2020-02-0702:32:41【问题描述】:如何将application-test.properties中的属性加载到SpringBoot中的测试类中?我做错了什么但无法... 查看详情

spring-boot 属性占位符

】spring-boot属性占位符【英文标题】:spring-bootpropertyplaceholder【发布时间】:2014-07-1622:22:03【问题描述】:我无法弄清楚为什么我无法在spring-boot中将值注入到我的application.properties文件中。外部属性到logging.file变量中。我有一个... 查看详情

多部分表单数据输入 Java 模型属性未在请求类中注入元素 - Spring Boot

...多部分表单数据输入Java模型属性未在请求类中注入元素-SpringBoot【英文标题】:MultipartFormDataInputJavaModelAttributeisnotinjectingtheelement\'sinRequestClass-SpringBoot【发布时间】:2022-01-2310:27:33【问题描述】:我正在尝试上传文件以及一些表... 查看详情

如何在spring boot中将@Value属性从application.properties注入@InjectMocks?

】如何在springboot中将@Value属性从application.properties注入@InjectMocks?【英文标题】:Howtoinject@Valuepropertiesfromapplication.propertiesto@InjectMocksinspringboot?【发布时间】:2021-11-2404:13:15【问题描述】:BatchService.java@ServicepublicclassBat 查看详情

将 Gradle 属性注入 Spring Boot application.yml,在 IntelliJ IDEA 中不起作用

】将Gradle属性注入SpringBootapplication.yml,在IntelliJIDEA中不起作用【英文标题】:InjectGradlepropertiesintoSpringBootapplication.yml,notworkinginIntelliJIDEA【发布时间】:2018-10-1111:23:02【问题描述】:我已经成功地将Gradleproj.ver注入到application.yml... 查看详情

如果 Spring Boot 的属性文件中未提供占位符值,如何跳过?

】如果SpringBoot的属性文件中未提供占位符值,如何跳过?【英文标题】:howtoskipplaceholdervalueifnotsuppliedinpropertiesfileinspringboot?【发布时间】:2016-12-0916:48:01【问题描述】:我有一个使用gradle的SpringBoot应用程序。我已将gradle配置为... 查看详情

转储 Spring Boot 配置

】转储SpringBoot配置【英文标题】:DumpSpringbootConfiguration【发布时间】:2016-02-2200:41:41【问题描述】:我们的运维人员希望在应用启动时将Spring引导配置(即所有属性)转储到日志文件中。我假设这可以通过注入带有注释@Configurat... 查看详情

spring配置文件中注入复杂类型属性

Spring在整合其他框架时在配置文件中可能会涉及到复杂类型属性的注入,以下举例说明:1.数组类型2.List集合类型3.Set集合类型4.Map集合类型5.Properties属性类型直接上代码:实体类:CollectionBean.javapackagecom.imooc.ioc.demo5;importjava.util.*... 查看详情

Spring Boot 将 Java 注释上的配置属性/消息外部化

】SpringBoot将Java注释上的配置属性/消息外部化【英文标题】:Springbootexternalizeconfigproperties/messagesonJavaannotations【发布时间】:2016-12-3015:36:54【问题描述】:有没有办法在spring中从外部属性文件中读取文本而不使用@Value注释。例如... 查看详情

Spring - 将属性文件中的日期列表注入bean属性[重复]

】Spring-将属性文件中的日期列表注入bean属性[重复]【英文标题】:Spring-Injectingalistofdatesfrompropertyfiletoabeanproperty[duplicate]【发布时间】:2012-06-2904:29:51【问题描述】:可能重复:HowdoIspecifyvaluesinapropertiesfilesotheycanberetrievedusingResour... 查看详情

Spring Boot:从属性文件中读取值列表

】SpringBoot:从属性文件中读取值列表【英文标题】:SpringBoot:Readlistofvaluesfrompropertiesfile【发布时间】:2020-07-0117:10:46【问题描述】:我的属性文件包含以下值列表prop.myVariable=v1,v2,v3我尝试使用springboot读取它们,如下所示:@Value(... 查看详情

Spring-Boot 多模块项目加载属性文件

】Spring-Boot多模块项目加载属性文件【英文标题】:Spring-Bootmultimoduleprojectloadproperty-file【发布时间】:2018-03-2818:51:08【问题描述】:我在Maven中有一个Spring-Boot-Application作为多模块项目。结构如下:Parent-Project|--MainApplication|--Module... 查看详情