SpringBoot中@Value注解读取配置⽂件的使⽤SpringBoot中@Value注解读取配置⽂件的使⽤
总结⼀下SpringBoot项⽬中⼀般⽤到的@Value注解(来⾃由org.springframework.beans.factory.annotation.Value)读取配置⽂件的使⽤
前置介绍
⽬录结构读取配置文件失败
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
配置⽂件
server.port=8080
test.url=editor.csdn
读取⼀般变量 ValueController
package;随风奔跑
import Value; import GetMapping; import RestController;
@RestController
public class ValueController {
@Value("${test.url}")
private String url;
@GetMapping("test")
public void test(){
System.out.println("url = "+ url);
}
}
访问localhost:8080/test,成功读取到参数
读取静态变量 ValueController
package;
import Value; import Component;
import GetMapping; import RestController;
@RestController
@Component
public class ValueController {科目二考试规则
二手车平台private static String url;
@Value("${test.url}")
public void setUrl(String url){
this.url = url;
}
电工证报考
@GetMapping("test")
public void test(){
System.out.println("url = "+ url);
研招网查成绩}
}
访问localhost:8080/test,成功读取到参数