NacosValue 定义在 nacos-api 工程中:com.alibaba.nacos.api.config.annotation.NacosValue
注解解析在 nacos-spring-project 工程中:
com.alibaba.nacos.spring.util.NacosBeanUtils#registerNacosValueAnnotationBeanPostProcessor
com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor
实现 BeanPostProcessor 接口,可以在创建 bean 的过程中插入自定义逻辑,NacosValueAnnotationBeanPostProcessor 扫描属性和方法上的 NacosValue 注解。
执行顺序:bean 的构造方法,postProcessBeforeInitialization,init,postProcessAfterInitialization
1@Override 2public Object postProcessBeforeInitialization(Object bean, final String beanName) 3 throws BeansException { 4 5 doWithFields(bean, beanName); 6 7 doWithMethods(bean, beanName); 8 9 return super.postProcessBeforeInitialization(bean, beanName); 10}
nacos 客户端定时从 nacos server 拉取更新,如果有更新,则通过事件把新值注入到属性中。
com.alibaba.nacos.client.config.impl.ClientWorker.LongPullingRunnable
com.alibaba.nacos.client.config.impl.CacheData#safeNotifyListener
检测到变化

1// 配置文件发生变化,把这种变化转化为 spring 事件 2private void safeNotifyListener(final String dataId, final String group, final String content, 3 final String md5, final ManagerListenerWrap listenerWrap) { 4 final Listener listener = listenerWrap.listener; 5 6 // 把逻辑封装成 job,可异步可同步 7 Runnable job = new Runnable() { 8 public void run() { 9 ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); 10 ClassLoader appClassLoader = listener.getClass().getClassLoader(); 11 try { 12 if (listener instanceof AbstractSharedListener) { 13 AbstractSharedListener adapter = (AbstractSharedListener) listener; 14 adapter.fillContext(dataId, group); 15 LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5); 16 } 17 // 执行回调之前先将线程classloader设置为具体webapp的classloader,以免回调方法中调用spi接口是出现异常或错用(多应用部署才会有该问题)。 18 Thread.currentThread().setContextClassLoader(appClassLoader); 19 20 ConfigResponse cr = new ConfigResponse(); 21 cr.setDataId(dataId); 22 cr.setGroup(group); 23 cr.setContent(content); 24 configFilterChainManager.doFilter(null, cr); 25 String contentTmp = cr.getContent(); 26 // 真正发布事件的地方 27 listener.receiveConfigInfo(contentTmp); 28 listenerWrap.lastCallMd5 = md5; 29 LOGGER.info("[{}] [notify-ok] dataId={}, group={}, md5={}, listener={} ", name, dataId, group, md5, 30 listener); 31 } catch (NacosException de) { 32 LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} errCode={} errMsg={}", name, 33 dataId, group, md5, listener, de.getErrCode(), de.getErrMsg()); 34 } catch (Throwable t) { 35 LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} tx={}", name, dataId, group, 36 md5, listener, t.getCause()); 37 } finally { 38 Thread.currentThread().setContextClassLoader(myClassLoader); 39 } 40 } 41 }; 42 43 final long startNotify = System.currentTimeMillis(); 44 try { 45 if (null != listener.getExecutor()) { 46 listener.getExecutor().execute(job); 47 } else { 48 job.run(); 49 } 50 } catch (Throwable t) { 51 LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} throwable={}", name, dataId, group, 52 md5, listener, t.getCause()); 53 } 54 final long finishNotify = System.currentTimeMillis(); 55 LOGGER.info("[{}] [notify-listener] time cost={}ms in ClientWorker, dataId={}, group={}, md5={}, listener={} ", 56 name, (finishNotify - startNotify), dataId, group, md5, listener); 57}
发布事件
com.alibaba.nacos.spring.context.event.config.DelegatingEventPublishingListener#publishEvent
DelegatingEventPublishingListener 类在 nacos-spring-context 工程中
1private void publishEvent(String content) { 2 NacosConfigReceivedEvent event = new NacosConfigReceivedEvent(configService, dataId, groupId, content); 3 applicationEventPublisher.publishEvent(event); 4}
消费事件
com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor#onApplicationEvent
NacosValueAnnotationBeanPostProcessor 类在 nacos-spring-context 工程中
NacosValueAnnotationBeanPostProcessor 实现了 ApplicationListener<NacosConfigReceivedEvent> 接口,NacosConfigReceivedEvent 继承自 ApplicationEvent。
1public void onApplicationEvent(NacosConfigReceivedEvent event) { 2 String content = event.getContent(); 3 if (content != null) { 4 Properties configProperties = toProperties(content); 5 6 for (Object key : configProperties.keySet()) { 7 String propertyKey = (String)key; 8 9 List<NacosValueTarget> beanPropertyList = placeholderNacosValueTargetMap.get(propertyKey); 10 if (beanPropertyList == null) { 11 continue; 12 } 13 14 String propertyValue = configProperties.getProperty(propertyKey); 15 for (NacosValueTarget nacosValueTarget : beanPropertyList) { 16 if (nacosValueTarget.method == null) { 17 setField(nacosValueTarget, propertyValue); 18 } else { 19 setMethod(nacosValueTarget, propertyValue); 20 } 21 } 22 } 23 } 24}