从spring-boot环境获取属性的地图

时间:2016-07-06 14:25:17

标签: java spring spring-boot

我遇到了这样的问题:我的应用程序可以与其他应用程序一起使用。它们中的每一个都有一些独特的属性 - templateProject-id。所以, PdfReader reader = new PdfReader(new FileInputStream(new File("C:\\Temp\\577979.pdf"))); PdfStamper stamper = new PdfStamper(reader, (new FileOutputStream(new File("C:\\Temp\\577979-out.pdf")))); AcroFields form = stamper.getAcroFields(); // Let's add value to an existing field form.setField("idCustomer", "My customer!!!"); // Let's add a new field TextField idDocTrackTypeField = new TextField(stamper.getWriter(), new Rectangle(150, 740, 180, 790), "idDocTrackType"); PdfFormField field1 = idDocTrackTypeField.getTextField(); // First attempt to set the value (before adding it) field1.setValueAsString("idDocTrackTypeValue1"); // Let's add it stamper.addAnnotation(field1, 1); // Second attempt to set the value (after adding it) field1.setValueAsString("idDocTrackTypeValue2"); // Third attempt to set the value form.setField("idDocTrackType", "idDocTrackTypeValue3") 看起来像

application.properties

我不想在我的服务类中存储Environment对象。只有mine.application.basic-project-id.application_1=10200 mine.application.basic-project-id.application_2=10202 mine.application.basic-project-id.application_3=10001 对(application_name,project_id) 因此,从该示例中应包含对Map<String, Long>

现在我存储("application_1", 10200L), ("application_2",10202L), ("application_3",10001")并且使用应用程序名称每次都构建属性名称并检索值。

Environment

1 个答案:

答案 0 :(得分:2)

这应该有效

@ConfigurationProperties("mine.application")
public class ApplicationProperties {

   private Map<String,Long> basicProjectId = new HashMap<>();

   public Map<String,Long> getBasicProjectId() { 
     return basicProjectId;
   }
}


@SpringBootApplication
@EnableConfigurationProperties(ApplicationProperties.class)
public class YourApp { .... }

然后你需要那些东西,只需注入ApplicationProperties。如果您启用Spring Boot configuration meta-data annotation processor to your build,您还可以获得该密钥的内容帮助(以及您在该课程中添加的任何其他密钥)。