Requirement Background: Obtain the corresponding configuration information through nacOS configuration center, and then inject attribute values through bean. Some of the attribute values need to be converted according to the type in the configuration, so as to directly obtain the processed attribute values.

As shown in the figure, rules are obtained from the configuration center and grayRuleInfos is provided to other services. Rules need to be converted into the List object grayRuleInfos.

This can be done in three ways

  1. Initialization of grayRuleInfos is done (inappropriately) by overriding the set method while the bean is automatically initialized to inject rules.
  2. Listen for the configuration change event delivered through EventListener, and then modify the corresponding grayRuleInfos initialization (retrieve the value of last rules).
@EventListener
public void envChangeListener(EnvironmentChangeEvent event) {}
Copy the code
  1. GrayRuleInfos is gracefully initialized via @postconstruct.

Because of the @refreshScope addition to the bean, each configuration change destroys the original bean and reinitialates the bean (note that this is lazy loading and PostConstruct is only triggered when it is used).