YAML - Spring Boot 下更好的配置选择? Myth丶恋晨 2022-05-09 01:52 253阅读 0赞 > YAML an alternative to Properties file … with Spring Boot ![在这里插入图片描述][70] 对于许多Java工程师来说,一直习惯于使用properties文件或者xml文件来定义应用程序配置。但SpringBoot允许我们仅通过单个yml文件就可以完成所有与应用相关的配置。**补充说明一下:比较不同格式的配置文件的优劣,都会有一定的主观性**。但对于SpringBoot配置来说,依据[官方文档][Link 1]的说法,使用YAML似乎只有一个缺点:使用通过注解PropertySource加载YAML文件。 > YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file. ### 什么是[YAML][]文件 ### > It is a human friendly data serialization standard for all programming languages. YAML是一种适用于所有编程语言的人性化数据序列化标准。YAML是JSON的子集,因而非常适用于定义树型配置参数。同时,YAML易于阅读,更利于工程师进行阅读和编辑。 ### YAML的设计目标 ### * 易于阅读 * 可移植性高,适用于各种编程语言 * 符合[native data structures][] * 包含一致性模型 * 支持one-pass处理 * 更好的语义表述性,易于扩展 * 易于实现,易于使用 ### 使用YAML注意事项 ### 你无法去校验YAML文件本身的完整性,相反,XML文件则总是要求格式完整。 YAML配置示例 environment: profiles: dev name: Developer App url: http://dev.abc.com profiles: qa name: QA App url: http://qa.abc.com ### 常见JAVA YAML解析器 ### * SnakeYAML * JYaml * YamlBeans * JvYaml [Spring Boot][]使用[SnakeYAML][]库加载解析YAML配置文件,**YamlPropertiesFactoryBean**将YAML文件解析为Properties文件,**YamlMapFactoryBean**将YAML文件解析为一个Map对象。 ### **YamlPropertiesFactoryBean**加载YAML文件方式 ### * 使用Java配置类 @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("appConfig.yml"); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); return propertySourcesPlaceholderConfigurer; } * 使用XML组件配置 <context:annotation-config/> <bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean"> <property name="resources" value="classpath:appConfig.yml"/> </bean> <context:property-placeholder properties-ref="yamlProperties"/> #### 参考文档 #### * [https://stackoverflow.com/questions/47462950/application-yml-vs-application-properties-for-spring-boot][https_stackoverflow.com_questions_47462950_application-yml-vs-application-properties-for-spring-boot] * [https://meetsnehal.wordpress.com/2015/09/12/yaml-an-alternative-to-properties-file-with-spring-boot/][https_meetsnehal.wordpress.com_2015_09_12_yaml-an-alternative-to-properties-file-with-spring-boot] [70]: /images/20220509/473ed522a0904bba99a7a54ca8751908.png [Link 1]: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings [YAML]: http://yaml.org/ [native data structures]: https://wordpress.com/post/84008038/58/redir.aspx?C=iIVoi758YUam3BoU91hAwS5KRK38t9JIG_sSS9Z9so3j2B5_qM_kbAAt1AwGIWBDvAXGWaEX0IQ.&URL=http%3a%2f%2fwww.yaml.org%2fspec%2f1.2%2fspec.html%23native+data+structure%2f%2f [Spring Boot]: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter/pom.xml [SnakeYAML]: https://bitbucket.org/asomov/snakeyaml [https_stackoverflow.com_questions_47462950_application-yml-vs-application-properties-for-spring-boot]: https://stackoverflow.com/questions/47462950/application-yml-vs-application-properties-for-spring-boot [https_meetsnehal.wordpress.com_2015_09_12_yaml-an-alternative-to-properties-file-with-spring-boot]: https://meetsnehal.wordpress.com/2015/09/12/yaml-an-alternative-to-properties-file-with-spring-boot/
还没有评论,来说两句吧...