@RefreshScope 結合nacos實現配置熱更新實驗


實體類


@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注解 必須在類上加上注解才能熱更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM