spring注解值发生改变动态刷新

1.在项目中添加依赖

org.springframework.cloud
spring-cloud-starter-config
1.3.1.RELEASE


org.springframework.boot
spring-boot-starter-actuator
1.5.4.RELEASE

2.在需要刷新的类中添加注解
@RefreshScope
3.调用http://ip:port/refresh重新加载(post提交)
这个时候可能出现无权限的问题,这是因为refresh是spring内部的私密内容,不对外开放,需要在spring全局配置中添加配置management.security.enabled=false
4.修改你在配置文件中的变量值,调用步骤3中的方法,这个时候注解值已发生改变

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接入 Apollo 可以使用 Apollo 提供的 Spring Boot Starter,具体步骤如下: 1. 引入依赖 在 `pom.xml` 文件中引入如下依赖: ```xml <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>${apollo.version}</version> </dependency> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-spring-boot-starter</artifactId> <version>${apollo.version}</version> </dependency> ``` 其中 `${apollo.version}` 为 Apollo 的版本号。 2. 配置文件 在 `application.properties` 或 `application.yml` 文件中配置 Apollo 的相关信息,例如: ```yml spring: application: name: your-application-name apollo: meta: http://localhost:8080 bootstrap: enabled: true refresh: enabled: true ``` 其中 `your-application-name` 为应用名称,`http://localhost:8080` 为 Apollo 的地址。 3. 使用注解 在需要动态刷新的 Bean 上添加 `@ApolloConfig` 和 `@ApolloConfigChangeListener` 注解,例如: ```java @Component public class DemoBean { @ApolloConfig private Config config; @Value("${your.key}") private String yourKey; @ApolloConfigChangeListener private void onChange(ConfigChangeEvent event) { if (event.isChanged("your.key")) { yourKey = config.getProperty("your.key", ""); } } // getter and setter } ``` 其中 `your.key` 为需要动态刷新的属性名称,`onChange` 方法会在属性发生变化时被调用。 通过以上步骤,就可以实现在 Spring 中使用 Apollo 并实现动态刷新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值