In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Springboot adopts the idea of building production-ready Spring applications. The convention that Spring Boot takes precedence over configuration is designed to get you up and running as soon as possible. In general, we don't need to do much configuration to make spring boot work properly. To understand the springcloud architecture, you can add: 3536247259. In some special cases, we need to modify some configurations, or we need to have our own configuration properties.
I. Custom attributes
When we create a springboot project, an application.properties is created for us under the src/main/java/resources directory by default. Personal habit, I will change application.properties to application.yml file, both file formats are supported.
Customize a set of properties in application.yml:
My: name: forezp age: 12
If you need to read the value of the configuration file, just add @ Value ("${property name}"):
@ RestControllerpublic class MiyaController {@ Value ("${my.name}") private String name; @ Value ("${my.age}") private int age; @ RequestMapping (value = "/ miya") public String miya () {return name+ ":" + age;}}
Start the project, visit: localhost:8080/miya, the browser displays:
Forezp:12
Assign the attributes of the configuration file to the entity class
When we have a lot of configuration properties, we create a javabean with these properties as fields and assign property values to them, such as:
My: name: forezp age: 12 number: ${random.int} uuid: ${random.uuid} max: ${random.int (10)} value: ${random.value} greeting: hi,i'm ${my.name}
${random} is used in the configuration file, which can be used to generate different types of random values.
How to assign these attributes to a javabean, first create a javabean:
@ ConfigurationProperties (prefix = "my") @ Componentpublic class ConfigBean {private String name; private int age; private int number; private String uuid; private int max; private String value; private String greeting; omitted getter setter....
You need to add an annotation @ ConfigurationProperties and add its prrfix. In addition, @ Component can be added or not. In addition, spring-boot-configuration-processor dependence can be added or not, the specific reasons are unknown.
Org.springframework.boot spring-boot-configuration-processor true
In addition, you need to add EnableConfigurationProperties comments to the application class or application class.
@ RestController@EnableConfigurationProperties ({ConfigBean.class}) public class LucyController {@ Autowired ConfigBean configBean; @ RequestMapping (value = "/ lucy") public String miya () {return configBean.getGreeting () + ">" + configBean.getName () + ">" + configBean.getUuid () + ">" + configBean.getMax ();}
Start the project, visit localhost:8080/lucy, and we will find that the configuration file information has been read.
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.