当前位置:网站首页>Section 6: basic configuration I of spingboot
Section 6: basic configuration I of spingboot
2022-06-23 02:44:00 【Introductory notes】
SpringBoot The default configuration file is application.properties perhaps application.yml, This file will be automatically loaded when the application starts , No need to introduce it manually .
Custom properties
stay
application.propertiesDefine attribute values in
# Custom properties rumenz.name=rumenz rumenz.url=https://rumenz.com
adopt
@ValueAnnotation to get the value
@RestController
@RequestMapping("/rumenz")
public class RumenzController {
@Value("${rumenz.name}")
private String rumenzName;
@Value("${rumenz.url}")
private String rumenzUrl;
@RequestMapping("/index")
public String index(){
return rumenzName+":::"+rumenzUrl;
}
}visit
http://127.0.0.1:8080/rumenz/indexroute , Returns therumenz:::https://rumenz.com.
Configure the binding
There are many attributes that need to be injected when there is time , Binding one by one is tedious , The official suggestion is that Bean How to load .
application.properties The configuration file defines attribute values
com.rumenz.id=1 com.rumenz.name=hello
RumenzConfig The configuration file
@Component
@ConfigurationProperties(prefix = "com.rumenz")
public class RumenzConfig {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}test
Browser input
http://127.0.0.1:8080/rumenz/index1, return1:::hello
@RestController
@RequestMapping("/rumenz")
public class RumenzController {
@Autowired
RumenzConfig rumenzConfig;
@RequestMapping("/index1")
public String index1(){
return rumenzConfig.getId()+":::"+rumenzConfig.getName();
}
}Parameter reference
stay application.properties The parameters in can be directly referenced to use
com.rumenz.id=1
com.rumenz.name=hello
com.rumenz.des=${com.rumenz.name}${com.rumenz.id}newly build
RumenzControllertest
@RestController
@RequestMapping("/rumenz")
public class RumenzController {
@Value("${com.rumenz.des}")
private String rumenzDes;
@RequestMapping("/index2")
public String index2(){
// Profile parameter reference
//com.rumenz.des=${com.rumenz.name}${com.rumenz.id}
return rumenzDes;
}
}Browser access
http://127.0.0.1:8080/rumenz/index2return hello1
Custom profile
General configuration items are
application.propertiesin , Many times, we want to store some configuration information separately . For example, we created arumenz.propertiesCustom profile for .
com.rumenz.tag= Entry station com.rumenz.txt= This is a tutorial website
The custom configuration file system will not load automatically , Need to use
@PropertySourceload . You need to load multiple arrays that can be set .test
@RestController
@RequestMapping("/rumenz")
@PropertySource(value="classpath:rumenz.properties",encoding = "utf-8")
public class Rumenz1Controller {
@Value("${com.rumenz.tag}")
private String rumenzTag;
@Value("${com.rumenz.txt}")
private String rumenzTxt;
@RequestMapping("/index3")
public String index3(){
// This is the property configured in the custom configuration
return rumenzTag+rumenzTxt;
}
}Browser access
http://127.0.0.1:8080/rumenz/index3, returnThis is a tutorial site
Load multiple custom profiles
@PropertySource(value = {
"classpath:rumenz.properties",
"classpath:rumenz2.properties",
}, encoding = "utf-8")The source code address of this summary :
- GitHub:https://github.com/mifunc/springboot/tree/main/lession6
- Gitee:https://gitee.com/rumenz/springboot/tree/master/lession6
- https://rumenz.com/rumenbiji/springboot-basic-config.html
Introduce
- My blog https://rumenz.com/
- My toolbox https://tooltt.com/
- WeChat official account :【 Entry station 】
边栏推荐
- 6. template for integer and real number dichotomy
- Windows system poisoning, SQL Server database file recovery rescue and OA program file recovery
- 2021-11-11
- Optimization method of live weak network
- No error is reported when using the Gorm framework to create a table, but the data cannot be inserted successfully
- Special exercise split line-----------------------------
- Goframe framework (RK boot): fast implementation of server-side JWT verification
- Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
- method
- PHP Base64 image processing Encyclopedia
猜你喜欢

Evolution history of mobile communication

Performance testing -- Interpretation and practice of 16 enterprise level project framework

Lying in the trough, write it yourself if you can't grab it. Use code to realize a Bing Dwen Dwen. It's so beautiful

My good brother gave me a difficult problem: retry mechanism

5g core network and core network evolution

Stop automatically after MySQL starts (unable to start)

You must know the type and method of urllib

8. greed

How to design API return codes (error codes)?

Custom shapes for ugui skill learning
随机推荐
This monitoring tool is enough for the operation and maintenance of small and medium-sized enterprises - wgcloud
How to design API return codes (error codes)?
5g spectrum
How to use fortress on mobile devices
How to prohibit copying and copying files to the local server remote desktop
Canvas draw the clock
Use of apicloud AVM framework list component list view and flex layout tutorial
Detailed explanation of online reputation management
Windows system poisoning, SQL Server database file recovery rescue and OA program file recovery
Performance test -- 14 detailed explanation of performance test report and precautions
Third order magic cube formula
6. template for integer and real number dichotomy
Docker builds redis3 master-slave cluster and expands the capacity
Detailed explanation of various networking modes of video monitoring platform
"Return index" of live broadcast E-commerce
Web components series (I) - Overview
8. greed
5. concept of ruler method
Xgboost Guide
About the use of mock framework