介绍
这篇文章主要介绍“春引导怎么获取配置文件的属性”,在日常操作中,相信很多人在春天引导怎么获取配置文件的属性问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答“春引导怎么获取配置文件的属性”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
服务器: ,,端口:8888 tomcat:才能 ,,,uri编码:utf - 8 #,配置微服务的地址 url: #,才能订单微服务的地址 orderUrl才能:http://localhost: 8002 #微才能服务地址2 taskUrl才能:http://localhost: 8003 #微才能服务地址3 customerUrl才能:http://localhost: 8004 , 那才能么我们如何获取呢? 第一种方式:直接使用@ value(“${名称}“)注解就可以将配置文件中的属性值注入进的来。 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; ,/* * ,*描述:微服务地址调用 ,* @author 管理员 ,* @create 2018 - 10 - 18岁,十六11 ,*/@RestController @RequestMapping (“/url") public class  ConfigController { , ,,,private static final Logger Logger =, LoggerFactory.getLogger (ConfigController.class); , ,,,//在属性上使用,@Value 注解即可获取到配置文件中的配置信息 ,,,@ value (“$ {url.orderUrl}“) ,,,private String orderUrl; , ,,,@RequestMapping (“/orderUrl") ,,,public String testConfig (), { ,,,,,,,LOGGER.info(“=====获取的订单服务地址为:{},,,orderUrl); ,,,,,,,return orderUrl; ,,,} } 第二种方式:多个配置信息的情形,列入我们有多个微服务地址,这样的话我们就还可以简单一些。 1,引入依赖 & lt; dependency> ,,,& lt; groupId> org.springframework.boot ,,,& lt; artifactId> spring-boot-configuration-processor ,,,& lt; optional> true & lt;/dependency> 2,定义一个保存服务url的类: import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; ,/* * ,*描述:微服务地址 ,* @author 管理员 ,* @create 2018 - 10 - 18日16:28 ,*/@ component @ConfigurationProperties (=prefix “url") public class  ServiceUrl { ,,,private String orderUrl; ,,,private String taskUrl; ,,,private String customerUrl; ,,,public String getOrderUrl (), { ,,,,,,,return orderUrl; ,,,} ,,,public void setOrderUrl (String orderUrl), { ,,,,,,,this.orderUrl =, orderUrl; ,,,} , ,,,public String getTaskUrl (), { ,,,,,,,return taskUrl; ,,,} , ,,,public void setTaskUrl (String taskUrl), { ,,,,,,,this.taskUrl =, taskUrl; ,,,} , ,,,public String getCustomerUrl (), { ,,,,,,,return customerUrl; ,,,} ,,,使用,@ConfigurationProperties 注解并使用,prefix 指定一个前缀,那么该类中的属性名就是配置中去掉前缀后的名字,一一对应即可。即:前缀名,+,属性名就是配置文件中定义的,关键。同时,该类上面需要加上,@Component 注解,把该类作为组件放到,Spring 容器中,让,Spring 去管理,我们使用的时候直接注入即可。 然后我们直接使用@注入就可以使用了 import com.ruifeng.demo.common.ServiceUrl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; , import javax.annotation.Resource; ,/* * ,*描述:微服务地址调用 ,* @author 管理员 ,* @create 2018 - 10 - 18岁,十六11 ,*/@RestController @RequestMapping (“/url") public class  ConfigController { , ,,,private static final Logger Logger =, LoggerFactory.getLogger (ConfigController.class); , , ,,@ ,,,private ServiceUrl microServiceUrl; , ,,,@RequestMapping (“/config") ,,,public String testConfigs (), { ,,,,,,,LOGGER.info(“=====获取的订单服务地址为:{},,,microServiceUrl.getOrderUrl ()); null null null null null null弹簧引导怎么获取配置文件的属性