如何仅在成功配置绑定后才监听RefreshScopeRefreshedEvent

时间:2018-08-29 02:26:31

标签: spring spring-boot

@Component
@ConfigurationProperties("person")
@RefreshScope
@Validated
public class PersonConfiguration {

    @NotBlank
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@RestController
class MessageRestController {

    @EventListener(RefreshScopeRefreshedEvent.class)
    void onRefresh(RefreshScopeRefreshedEvent event) {
        System.out.println("entering refresh");
        System.out.println("exiting refresh");
    }
}

person:
    name: aaaa

每当名称更改时,都会在MessageRestController中调用onRefresh方法。当名称更改为空字符串时,有一种方法可以限制onRefresh方法的调用,因为它不是PersonConfiguration类中的有效字符串。

0 个答案:

没有答案