package com.huhy.demo.properties; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @author : huhy on 2018/9/28. * @Project_name:springboot_self_gitlab * @LOCAL:com.huhy.demo.properties * @description: * 注意: * 1》spring-boot更新到1.5.2版本后locations属性没法使用 * @PropertySource注解只能够加载proprties文件,没法加载yaml文件 * 2》 若是有多个配置文件须要注入,能够用value[]来接收 @PropertySource(value={"classpath:yang.properties","classpath:yang2.properties"}) 3》文件读取 默认是resource下文件 @PropertySource("classpath:config/remote.properties") 配置config文件路径 4》加上encoding = "utf-8"属性防止中文乱码,也能够大写的"UTF-8" 5》ignoreResourceNotFound = true 扫描文件不存在的处理方式 默认false */ @ConfigurationProperties(prefix="huhy") @PropertySource(value = {"classpath:yang.properties","classpath:yang2.properties"},encoding = "UTF-8",ignoreResourceNotFound = true) @Data @Component public class PropertiesYang { private String name; private String age; /** * * name的值咱们设置的是"classpath:yang.properties","classpath:yang2.properties"。这个值在Springboot的环境中必须是惟一的,若是不设置, * 则值为:“class path resource ["classpath:yang.properties","classpath:yang2.properties"]“。 * 可能不少人比较纳闷,为何是“class path resource ["classpath:yang.properties","classpath:yang2.properties"]“呢? * 这个就涉及到了Spring中对资源文件的封装类Resource。上文咱们配置的value值为""classpath:yang.properties","classpath:yang2.properties"", * 所以Spring发现是classpath开头的,所以最终使用的是Resource的子类ClassPathResource。若是是file开头的,则最终使用的类是FileSystemResource。 * 了解了上文所述的Resource类以后。咱们再次明确一点,若是@PropertySource中若是没有设置name值,则name值的生成规则是:根据value值查找到最终封装的Resource子类, * 而后调用具体的Resource子类实例对象中的getDescription方法,getDescription方法的返回值为最终的name值。 * 好比ClassPathResource类中的getDescription方法实现以下: * public String getDescription() { * StringBuilder builder = new StringBuilder("class path resource ["); * String pathToUse = path; * if (this.clazz != null && !pathToUse.startsWith("/")) { * builder.append(ClassUtils.classPackageAsResourcePath(this.clazz)); * builder.append('/'); * } * if (pathToUse.startsWith("/")) { * pathToUse = pathToUse.substring(1); * } * builder.append(pathToUse); * builder.append(']'); * return builder.toString();} * */ }
因为springboot1.5.2以后中止了localtions的指定。如今加载yml文件的实现方式以下:html
YmlConfig 配置类:git
/** * @author : huhy on 2018/9/28. * @Project_name:springboot_self_gitlab * @LOCAL:com.huhy.demo.properties * @description:{todo} */ @Component public class YmlConfig { @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); //yaml.setResources(new FileSystemResource("yang.yml"));//File引入 yaml.setResources(new ClassPathResource("yang.yml"));//class引入 configurer.setProperties(yaml.getObject()); return configurer; } }
实体类:spring
/** * @author : huhy on 2018/9/28. * @Project_name:springboot_self_gitlab * @LOCAL:com.huhy.demo.properties * @description:自定义加载yml文件 * 1> ConfigurationProperties注解的locations属性在1.5.X之后没有了,不能指定locations来加载yml文件 * PropertySource注解只支持properties文件加载,详细见官方文档: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings * 2> */ @Data @Component @ConfigurationProperties(prefix = "yang") public class YmlYang{ private String name; private String age; }
配置文件“springboot
yang: name: yml age: 18
就这样就好了,你们能够试一下app