In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
本篇内容主要讲解"springboot怎么获取application.yml里值",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot怎么获取application.yml里值"吧!
在项目中,很多时候需要用到一些配置信息,这些信息在测试环境和生产环境下可能会有不同的配置,后面根据实际业务情况有可能还需要再做修改。我们不能将这些配置在代码中写死,最好是写到配置文件中,比如可以把这些信息写到 application.yml 文件中。
那么,怎么在代码里获取或者使用这个地址呢?有2个方法。
方法一:
我们可以通过@Value 注解的 ${key} 即可获取配置文件(application.yml)中和 key 对应的 value 值,这个方法适用于微服务比较少的情形
方法二:
在实际项目中,遇到业务繁琐,逻辑复杂的情况,需要考虑封装一个或多个配置类。例如,假如在当前服务中,某个业务需要同时调用微服务1、微服务2和微服务3。
如果这样一个个去使用 @Value 注解引入相应的微服务地址的话,太过于繁琐。
也许实际业务中,远远不止这三个微服务,甚至十几个都有可能。对于这种情况,我们可以先定义一个 MicroServiceUrl 类来专门保存微服务的 URL
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "url")public class MicroServiceUrl {private String orderUrl;private String userUrl;private String shoppingUrl;public String getOrderUrl() {return orderUrl;}public void setOrderUrl(String orderUrl) {this.orderUrl = orderUrl;}public String getUserUrl() {return userUrl;}public void setUserUrl(String userUrl) {this.userUrl = userUrl;}public String getShoppingUrl() {return shoppingUrl;}public void setShoppingUrl(String shoppingUrl) {this.shoppingUrl = shoppingUrl;}}
添加依赖:
org.springframework.bootspring-boot-configuration-processortrue
目前配置写好了,此时,不需要在代码中一个个引入这些微服务的 URL,直接通过 @Resource 注解将刚刚写好的配置类注入进来即可使用了,以下是测试Controller:
import com.example.test1.config.MicroServiceUrl;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;/*** 获取配置文件(application.yml)中和 key 对应的 value 值* 2种方法*/@RestController@RequestMapping("/test")public class ConfigController {private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);@Value("${url.orderUrl}")private String orderUrl;@Resourceprivate MicroServiceUrl microServiceUrl;@RequestMapping("/config")public String testConfig() {LOGGER.info("获取的地址为:{}", orderUrl);LOGGER.info("微服务1地址为:{}", microServiceUrl.getOrderUrl());LOGGER.info("微服务2地址为:{}", microServiceUrl.getUserUrl());LOGGER.info("微服务3地址为:{}", microServiceUrl.getShoppingUrl());return "success";}}到此,相信大家对"springboot怎么获取application.yml里值"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.