✨ 批量配置刷新读取
This commit is contained in:
parent
4c25f24561
commit
cdc03e35e2
|
@ -514,3 +514,46 @@ public class ProductServiceApplication {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 6、批量配置刷新读取
|
||||||
|
|
||||||
|
> [!TIP]
|
||||||
|
>
|
||||||
|
> 配置批量绑定无需 @RefreshScope 实现动态刷新。
|
||||||
|
>
|
||||||
|
> 中划线写法会自动映射为小驼峰。
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Configuration
|
||||||
|
// 配置批量绑定无需 @RefreshScope 实现动态刷新
|
||||||
|
@ConfigurationProperties(prefix = "order")
|
||||||
|
public class OrderProperties {
|
||||||
|
|
||||||
|
private String timeout;
|
||||||
|
|
||||||
|
// 中划线写法会自动映射为小驼峰
|
||||||
|
private String autoConfirm;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
测试读取内容
|
||||||
|
|
||||||
|
```java
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/order")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class OrderController {
|
||||||
|
|
||||||
|
private final OrderService orderService;
|
||||||
|
private final OrderProperties orderProperties;
|
||||||
|
|
||||||
|
@Operation(summary = "读取配置")
|
||||||
|
@GetMapping("config")
|
||||||
|
public String config() {
|
||||||
|
String timeout = orderProperties.getTimeout();
|
||||||
|
String autoConfirm = orderProperties.getAutoConfirm();
|
||||||
|
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package cn.bunny.service.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
// 配置批量绑定无需 @RefreshScope 实现动态刷新
|
||||||
|
@ConfigurationProperties(prefix = "order")
|
||||||
|
@Data
|
||||||
|
public class OrderProperties {
|
||||||
|
|
||||||
|
private String timeout;
|
||||||
|
|
||||||
|
// 中划线写法会自动映射为小驼峰
|
||||||
|
private String autoConfirm;
|
||||||
|
|
||||||
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
package cn.bunny.service.controller;
|
package cn.bunny.service.controller;
|
||||||
|
|
||||||
import cn.bunny.model.order.bean.Order;
|
import cn.bunny.model.order.bean.Order;
|
||||||
|
import cn.bunny.service.config.OrderProperties;
|
||||||
import cn.bunny.service.service.OrderService;
|
import cn.bunny.service.service.OrderService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
@ -16,11 +16,13 @@ public class OrderController {
|
||||||
|
|
||||||
private final OrderService orderService;
|
private final OrderService orderService;
|
||||||
|
|
||||||
@Value("${order.timeout}")
|
// @Value("${order.timeout}")
|
||||||
private String timeout;
|
// private String timeout;
|
||||||
|
//
|
||||||
|
// @Value("${order.auto-confirm}")
|
||||||
|
// private String autoConfirm;
|
||||||
|
|
||||||
@Value("${order.auto-confirm}")
|
private final OrderProperties orderProperties;
|
||||||
private String autoConfirm;
|
|
||||||
|
|
||||||
@Operation(summary = "创建订单")
|
@Operation(summary = "创建订单")
|
||||||
@GetMapping("create")
|
@GetMapping("create")
|
||||||
|
@ -31,6 +33,8 @@ public class OrderController {
|
||||||
@Operation(summary = "读取配置")
|
@Operation(summary = "读取配置")
|
||||||
@GetMapping("config")
|
@GetMapping("config")
|
||||||
public String config() {
|
public String config() {
|
||||||
|
String timeout = orderProperties.getTimeout();
|
||||||
|
String autoConfirm = orderProperties.getAutoConfirm();
|
||||||
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue