月光代碼園

記錄、分享、交流

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

SpringBoot学习笔记#2 具体化配置文件

case1. 通过注解的方式读取配置 24.Externalized Configuration

application.properties文件新增配置

1
externalized.configuration=extConfigSampleVal

控制层代码(仅做示例用,不作为推荐写法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
public class GreetingController {
     
    @Value("${externalized.configuration}")
    private String extConfig;
     
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();
 
    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, extConfig));
    }
}

*通过@Value注解将配置赋值到所注解的变量

访问host/greeting

case2.JAVA BEAN的方式读取配置

java bean:

1
2
3
4
5
6
7
8
9
10
@ConfigurationProperties(prefix="my")
@Component
public class Config {
 
    private int port;
     
    private List<String> servers = new ArrayList<String>();
 
    //getter,setter
}

读取代码

1
2
3
4
5
6
7
8
@Autowired
private Config config;
 
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, config.getPort()));
}

自动注入Config实例,测试结果:

 

case3.区分环境读取不同的配置文件 24.4 Profile-specific Properties

 新建3个配置文件分别对应开发环境dev,测试环境sit,生产环境prod

三个文件各自添加自己的变量做测试用:

 

application.properties设置配置指定要读取的配置文件,相关属性spring.profiles.active

这里将以application-{profile}.properties的格式查找对应配置文件,这里将使用application-dev.properties

编辑控制层代码

1
2
3
4
5
6
7
8
@Value("${currentEnv}")
private String currentEnv;
 
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, currentEnv));
}

访问host/greeting  

 case4.占位符

1
2
app.name=MyApp
app.description=${app.name} is a Spring Boot application
1
2
3
4
5
6
7
8
@Value("${app.description}")
    private String appDesc;
 
    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, appDesc));
    }

case5.随机值

1
2
3
4
5
6
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

返回

 

  

posted on   bangdikka  阅读(154)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示