In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to get the properties of the configuration file by spring boot". In the daily operation, I believe that many people have doubts about how to obtain the properties of the configuration file by spring boot. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to get the properties of the configuration file by spring boot". Next, please follow the editor to study!
Server: port: 8888 tomcat: uri-encoding: UTF-8# configuration microservice address url: # order microservice address orderUrl: http://localhost:8002 # microservice address 2 taskUrl: http://localhost:8003 # microservice address 3 customerUrl: http://localhost:8004 so how do we get it? The first way: you can inject property values in the configuration file directly using the @ Value ("${name}") annotation. 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; / * description: microservice address call * @ author Administrator * @ create 2018-10-18 16:11 * / @ RestController@RequestMapping ("/ url") public class ConfigController {private static final Logger LOGGER = LoggerFactory.getLogger (ConfigController.class) / / use @ Value annotation on the attribute to get the configuration information in the configuration file @ Value ("${url.orderUrl}") private String orderUrl; @ RequestMapping ("/ orderUrl") public String testConfig () {LOGGER.info ("= obtained order service address is: {}", orderUrl); return orderUrl The second way: in the case of multiple configuration information, include that we have multiple micro-service addresses, so that we can make it easier. 1 introduce a class that depends on org.springframework.boot spring-boot-configuration-processor true2 to define a service url: import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component; / * description: microservice address * @ author Administrator * @ 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 } use @ ConfigurationProperties annotation and use prefix to specify a prefix, then the attribute name in this class is the name with the prefix removed from the configuration, and it can be matched one by one. That is, the prefix name + attribute name is the key defined in the configuration file. At the same time, the class needs to be annotated with @ Component. Put the class as a component in the Spring container and let Spring manage it. When we use it, we can directly inject it. Then we can use 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 directly with @ Resource injection / * description: microservice address call * @ author Administrator * @ create 2018-10-18 16:11 * / @ RestController@RequestMapping ("/ url") public class ConfigController {private static final Logger LOGGER = LoggerFactory.getLogger (ConfigController.class); @ Resource private ServiceUrl microServiceUrl; @ RequestMapping ("/ config") public String testConfigs () {LOGGER.info ("= obtained order service address is: {}", microServiceUrl.getOrderUrl ()) LOGGER.info ("= obtained task service address is: {}", microServiceUrl.getTaskUrl ()); LOGGER.info ("= obtained customer service address is: {}", microServiceUrl.getCustomerUrl ()); return "success";}} at this point, the study on "how spring boot gets the properties of the configuration file" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 301
*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.