首页 > 其他 > 详细

@RefreshScope 结合nacos实现配置热更新实验

时间:2021-05-08 16:33:44      阅读:357      评论:0      收藏:0      [点我收藏+]

实体类


@Component
public class PatientInstances {
    @Bean(value = "patientWithRefreshScope")
    @ConfigurationProperties(prefix = "patientWithRefreshScope")
    @RefreshScope
    private PatientDTO patientWithRefreshScope(){
        PatientDTO patientDTO = new PatientDTO();
        return patientDTO;
    }

    @Bean(value = "patient")
    @ConfigurationProperties(prefix = "patient")
    private PatientDTO patient(){
        PatientDTO patientDTO = new PatientDTO();
        return patientDTO;
    }
}

带注解 @RefreshScope


@RestController
@RequestMapping("/scope")
@Component
@RefreshScope
public class testScopeController {

    @Autowired
    @Qualifier("patientWithRefreshScope")
    private PatientDTO patientWithRefreshScope;
    @Autowired
    @Qualifier("patient")
    private PatientDTO patient;

    @Value("${scopeValue}")
    private String scopeValue;




    @GetMapping("/test")
    public result test(){
        return result.builder().value(scopeValue).patientName(patient.getPatientName()).refreshScopeName(patientWithRefreshScope.getPatientName()).build();
    }


}
@Data
@Builder
class result{
    private String value;
    private String refreshScopeName;
    private String patientName;
}

不带注解

@RestController
@RequestMapping("/noscope")
@Component
public class testController {
    @Value("${value}")
    private String value;

    @GetMapping("/test")
    public String test(){
        return value;
    }


}

试验结果:实体类是否注解都能热更新
@Value注解 必须在类上加上注解才能热更新

@RefreshScope 结合nacos实现配置热更新实验

原文:https://www.cnblogs.com/crazy-donkey/p/14744058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!