【发布时间】:2020-05-15 15:32:42
【问题描述】:
我在 pom 中添加了以下依赖项,以便可以使用 Spring cloud
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
将 spring-cloud.version 添加为 Finchley.RELEASE
这是我打算刷新的bean:
@Component
@ConfigurationProperties(ignoreUnknownFields = true,prefix="global")
@PropertySource(value = "${spring.config.location}")
@Getter
@Setter
@Validated
@RefreshScope
public class GeneralProperties {
private String mode;
}
在我的控制器中访问 bean:
@RestController
@RequestMapping
@RefreshScope
public class AppController {
private static final AnalyticsLogger LOGGER = AnalyticsLoggerFactory
.getLogger(AppController.class);
@Autowired
SimulatorRequestProcessor simulatorRequestProcessor;
@Autowired
GeneralProperties generalProperties;
@ResponseBody
@PostMapping(value = "/api/mock-getdata", produces = "application/json")
public String fetchResponseBasedOnMode(@RequestHeader HttpHeaders headers,
@RequestBody String request) {
String response = null;
LOGGER.info("color: "+color);
switch (generalProperties.getMode()) {
//code
在应用程序配置中设置的属性:
spring.config.location=file:///var/lib/data/demo/config/generalConfig.properties
management.endpoints.web.exposure.include=refresh
无法刷新模式的值。有人可以指出此代码中的错误。我的属性文件位于 git 中未提交的文件夹中,因此我没有使用 spring-cloud-config。
【问题讨论】:
-
Finchley 不再受支持,你能用 Hoxton.SR1 再试一次吗?
-
配置属性 bean 也不需要 PropertySource 和 RefreshScope。控制器上也不需要刷新范围
-
我正在使用 Spring-boot 2.0.1 RELEASE,所以兼容的 Spring cloud 版本是 Finchley
-
从那以后的 11 个版本中可能已经修复了一些问题。
-
Boot 2.0.x 也不再支持
标签: spring spring-cloud spring-cloud-config