在启动类上添加annotation @RefreshScope,并将@Value注释用于带有 application.properties 中属性的字段。例如:
启动类:
@SpringBootApplication
@RefreshScope
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
自动注入属性:
@Service
public class MyService {
@Value("${myproperty}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
此外,需要通过在 bootstrap.properties 或 application.properties 文件中添加以下配置来禁用缓存:
spring.resources.cache.period=-1