Spring Boot - 从属性文件中注入地图

时间:2017-03-29 15:35:34

标签: java spring spring-boot configuration

属性文件如下所示:

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

仍然无法运作

4 个答案:

答案 0 :(得分:3)

如果您可以为属性文件提供更具体的示例,将会很有帮助。你应该在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

了解详情

答案 1 :(得分:1)

NullPointerException可能来自空ApplicationProperties

所有自定义属性都应注释@ConfigurationProperties(prefix="custom")。 之后,在您的主类(使用main方法的类)上,您必须添加@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

答案 2 :(得分:0)

将your.properties文件放在src/main/resources下,并将其作为

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

或将其作为PropertyPlaceholderConfigurer yourApplicationContext.xml中的@Value("app.url1") String path_to_binary1; @Value("app.url2") String path_to_binary2; // ... System.out.println(path_to_binary1+path_to_binary2); 。{/ p>

然后使用属性值,如

    function miseEnForme(){
  var classeur = SpreadsheetApp.getActive();
  var feuilleAdm = classeur.getSheetByName("SuiviAdministratif");

  var lastRow;
  for (var row = 3; row < 1000; row++) {
    var cell=feuilleAdm.getRange(row,3);
    if (cell.isBlank()) {
      lastRow =row;
      break;
    }
  }

  for (var i =3; i<lastRow;i++){
   if (feuilleAdm.getRange("B"+i).getDisplayValue()=="RS" || feuilleAdm.getRange("B"+i).getDisplayValue()=="RP" || feuilleAdm.getRange("B"+i).getDisplayValue()=="RD")
     feuilleAdm.getRange("SuiviAdministratif!A"+i+":AB"+i).setBackground("red");

   if (feuilleAdm.getRange("B"+i).getDisplayValue()=="C")
     feuilleAdm.getRange("SuiviAdministratif!A"+i+":AB"+i).setBackground("#00ff00");

   if (feuilleAdm.getRange("B"+i).getDisplayValue()=="O")
     feuilleAdm.getRange("SuiviAdministratif!A"+i+":AB"+i).setBackground("white");
  }
}

答案 3 :(得分:0)

从属性文件中获取地图需要两件事。首先,您需要一个具有配置和目标字段的类,以保存属性文件中的数据。

@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