讲解SpringBoot的配置文件前我们先学习配置文件读取的相关注解一文带你全部搞定!前言本来想讲解SpringBoot的配置文件但是里面一堆注解让我很是蒙圈感觉用起来都差不多比如PropertySource、ImportResource、Value、ConfigurationProperties等加上之前掌握的Configuration和Bean。一个配置文件搞这么多注解感觉头有点大这篇文章主要是对这些注解进行扫盲希望扫盲完后我以后就不再怕它们了。基于XML方式读取配置为了方便示例讲解我们在包resources下创建一个配置文件jdbc.propertiesjdbc.shop.urljdbc:mysql://localhost:3104/xm_jointly?characterEncodingutf8 jdbc.shop.usernamelouzai jdbc.shop.password123456之前在文章中我们也有这个一个配置但是我们使用XML的方式读取然后还需要在XML中配置文件扫描路径!-- 用于加载配置文件 -- bean classorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer property namelocation valueclasspath:jdbc.properties/value /property /bean然后才能读取里面的配置!-- 定义数据源 -- bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSource property nameurl value${jdbc.shop.url}/property property nameusername value${jdbc.shop.username}/property property namepassword value${jdbc.shop.password}/property /bean那么有没有更优雅的实现方式呢可以继续往下面看。ImportResourceXML中“用于加载配置文件”这块不变直接看示例ImportResource(classpath:applicationContext.xml) public class ImportResourceTest { Value(${jdbc.shop.url}) private String url; Value(${jdbc.shop.username}) private String userName; Value(${jdbc.shop.password}) private String passWord; public static void main(String args[]) { ApplicationContext context new AnnotationConfigApplicationContext(PropertySourceTest.class); PropertySourceTest connection context.getBean(PropertySourceTest.class); System.out.println(connection.toString()); } } // 输出 // PropertySourceTest(urljdbc:mysql://localhost:3104/xm_jointly?characterEncodingutf8, userNamelouzai, passWord123456)通过ImportResource读取配置文件然后通过Value赋值给成员变量Value的用法可以继续往后面看。这种方式感觉还是不太优雅因为还是依赖XML配置有没有完全脱离XML的方式呢PropertySource就登场了。PropertySource ValuePropertySource此注解PropertySource 为Spring 中的 Environment提供方便和声明机制通常与Configuration一起搭配使用直接看示例ComponentScan({com.java.annotation.spring.bean.fileconfig}) Component PropertySource(classpath:jdbc.properties) public class EnvironmentTest { Resource Environment environment; private String url; private String userName; private String passWord; public void getData() { url environment.getProperty(jdbc.shop.url); userName environment.getProperty(jdbc.shop.username); passWord environment.getProperty(jdbc.shop.password); System.out.println(url: url); System.out.println(userName: userName); System.out.println(passWord: passWord); } public static void main(String args[]) { ApplicationContext context new AnnotationConfigApplicationContext(EnvironmentTest.class); EnvironmentTest connection context.getBean(EnvironmentTest.class); connection.getData(); } } // 输出 // url:jdbc:mysql://localhost:3104/xm_jointly?characterEncodingutf8 // userName:louzai // passWord:123456ComponentScan用于扫描EnvironmentTest的beanPropertySource用于引入外部属性配置然后配合Environment可以读取配置的内容这个示例一看就懂So Easy!Value用Environment读取配置感觉好麻烦有没有更简单的方式呢直接看示例ComponentScan({com.java.annotation.spring.bean.fileconfig}) Component PropertySource(classpath:jdbc.properties) ToString public class PropertySourceTest { Value(${jdbc.shop.url}) private String url; Value(${jdbc.shop.username}) private String userName; Value(${jdbc.shop.password}) private String passWord; public static void main(String args[]) { ApplicationContext context new AnnotationConfigApplicationContext(PropertySourceTest.class); PropertySourceTest connection context.getBean(PropertySourceTest.class); System.out.println(connection.toString()); } } // 输出: // DBConnection(urljdbc:mysql://localhost:3104/xm_jointly?characterEncodingutf8, userNamelouzai, passWord123456)直接通过Value进行标注这个也是项目中常用的姿势强烈推荐这种。Value还可以直接为字段赋值获取系统属性这里就不展开了网上资料很多。这种读取配置的方式感觉已经非常优雅了但是每个配置数据都需要通过Value赋值还是有点麻烦有没有更优雅的方式呢ConfigurationProperties登场了。ConfigurationProperties这个是用在SpringBoot中yml配置文件的读取我们先看通过Value逐个读取配置的方式。我们新建一个application.yml配置shop: url: jdbc:mysql://localhost:3104/xm_jointly?characterEncodingutf8 username: louzai password: 123456通过Value方式Service RequestMapping public class HelloWorld { Value(${shop.url}) private String url; Value(${shop.username}) private String userName; Value(${shop.password}) private String passWord; ResponseBody RequestMapping(/hello) public String test() { return url: url , userName: userName , passWord: passWord; } }这个和前面的示例很像只是因为SpringBoot会自动读取application.yml配置所以不用再指定配置文件扫描路径启动SpringBoot程序后输入“http://localhost:8080/hello”输出如下通过ConfigurationProperties方式Service RequestMapping Setter ConfigurationProperties(prefix shop) public class HelloWorld2 { private String url; private String username; private String password; ResponseBody RequestMapping(/hello2) public String test() { return url: url , username: username , password: password; } }启动服务后浏览器输入“http://localhost:8080/hello2”输出如下Configuration Bean这个常用于配置文件管理类用于替换XML中的Bean注入这个必须掌握具体可参考文章《【Spring基础系列3】Spring常用的注解》我们一般会结合上面的配置读取方式然后生成对应的配置类Configuration PropertySource(classpath:es.properties) public class EsConfig { Value(${es.hostname}) private String hostname; Value(${es.port}) private String port; Bean public RestHighLevelClient client() { return new RestHighLevelClient(RestClient.builder(new Node(new HttpHost(hostname, Integer.parseInt(port))))); } }这样我们就可以得到一个配置类EsConfig配置类中的RestHighLevelClient已经通过Bean完成Spring的注入这个也是项目中常用的实现姿势。总结昨天刚陪老婆看了修仙的动画感觉这篇文章就像修仙一样修仙等级炼气→筑基→金丹→元婴→化神→渡劫合道→地仙→天仙→金仙→太乙金仙→大罗金仙→准圣→混元大罗金仙圣人→天道→大道炼气通过XML方式读取配置这个在小米中台的代码中大部分还是这种方式读取配置。筑基通过ImportResource读取XML但是读取数据值的部分移到了对象中即将对象的成员变量用Value赋值。如果还是依赖XML读取数据比如旧的系统代码那这个还是需要使用只是获取数据配置的值可以更优雅一些。金丹干掉XML直接通过PropertySource读取数据配置文件无需XML中转但是配置文件数据用Environment获取。感觉这个已经很少使用了。元婴干掉Environment直接用Value代替这个还是结合PropertySource使用。如果想干掉XML就直接用这个代替吧这个是Spring框架使用配置文件的终极版本。化神已经干掉XML也不用Value一一替换直接通过ConfigurationProperties自动将配置的数据和类的成员变量名关联。这个只基于SpringBoot所以也是我目前遇到的SpringBoot框架使用配置文件的终极版本。渡劫结合Configuration和Bean利用配置类通过自动读取数据配置然后通过这些数据配置项生成对应的对象完全摆脱对XML配置的依赖。这个可以用于Spirng和Spring Boot也是项目中常见的配置类生成姿势。
