@EnableApolloConfig注解在启动类
@Bean注入bean
@SpringBootApplication
@Configuration
@EnableApolloConfig
class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}
@ApolloConfig将 Apollo Config 对象注入
@ApolloConfigChangeListener将被注解的方法,向指定的 Apollo Config 发起配置变更监听
对应的处理器--ApolloAnnotationProcessor
@Slf4j
public class TestJavaConfigBean {
@ApolloConfig("application")
private Config config;
//config change listener for namespace application
@ApolloConfigChangeListener("application")
private void anotherOnChange(ConfigChangeEvent changeEvent) {
ConfigChange change = changeEvent.getChange("test");
System.out.println(String.format("Found change - key: %s, oldValue: %s,"
+ " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
}
@ApolloJsonValue将一个 Item 配置项,解析成对应类型的对象
com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor
,实现 BeanFactoryAware 接口,继承 ApolloProcessor 抽象类,处理 @ApolloConfig
和 @ApolloConfigChangeListener
注解处理器的初始化。
@Override
protected void processField(Object bean, String beanName, Field field) {
ApolloConfig annotation = AnnotationUtils.getAnnotation(field, ApolloConfig.class);
if (annotation == null) {
return;
}
Preconditions.checkArgument(Config.class.isAssignableFrom(field.getType()), "Invalid type: %s for field: %s, should be Config", field.getType(), field);
// 创建 Config 对象
String namespace = annotation.value();
Config config = ConfigService.getConfig(namespace);
// 设置 Config 对象,到对应的 Field
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, bean, config);
}
@ApolloConfig
注解,创建( 获得 )对应的 Config 对象,设置到注解的 Field 中。@Override
protected void processMethod(final Object bean, String beanName, final Method method) {
ApolloConfigChangeListener annotation = AnnotationUtils.findAnnotation(method, ApolloConfigChangeListener.class);
if (annotation == null) {
return;
}
Class>[] parameterTypes = method.getParameterTypes();
Preconditions.checkArgument(parameterTypes.length == 1, "Invalid number of parameters: %s for method: %s, should be 1", parameterTypes.length, method);
Preconditions.checkArgument(ConfigChangeEvent.class.isAssignableFrom(parameterTypes[0]), "Invalid parameter type: %s for method: %s, should be ConfigChangeEvent", parameterTypes[0], method);
// 创建 ConfigChangeListener 监听器。该监听器会调用被注解的方法。
ReflectionUtils.makeAccessible(method);
ConfigChangeListener configChangeListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
ReflectionUtils.invokeMethod(method, bean, changeEvent);
}
};
// 向指定 Namespace 的 Config 对象们,注册该监听器
String[] namespaces = annotation.value();
for (String namespace : namespaces) {
Config config = ConfigService.getConfig(namespace);
config.addChangeListener(configChangeListener);
}
}
@ApolloConfigChangeListener
注解,创建回调注解方法的 ConfigChangeListener 对象,并向指定 Namespace 们的 Config 对象们,注册该监听器。