In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to get the custom variables in the configuration file application.yml and use them". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In writing a project, we often have to write the same variable in different files countless times, so it is easy to find and very inconvenient to modify. We usually write a tool class to store our own variables for calling values, but we don't bother to write what to do. If we write it, we have to inject it, and if the injection fails, we will get it again. Trouble, is there a way to write custom variables directly in the configuration file to take values through annotations? There must be.
Text:
First, configure your own variables in application.yml, such as: person: name: 'Zhang San' age: 'age 25' home: 'own a house' car: 'car' two, use the value of the annotation and use import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController @ RestController@RequestMapping ("/ test") public class TestController {@ Value ("${person.name}") private String name; @ Value ("${person.age}") private String age; @ Value ("${person.home}") private String home; @ Value ("${person.car}") private String car; @ RequestMapping ("/ getsome") public String getSome () {System.out.println (name+age+home+car); return name+age+home+car Third, visit the browser to view the results.
You can see that it can be used directly by annotations, which is very convenient.
This is the end of "how to get and use custom variables in the configuration file application.yml by Springboot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.