@@ -73,15 +73,19 @@ public int getOrder() {
7373 private Map <String , TargetRefreshable > targetListenerMap = new ConcurrentHashMap <>();
7474 private Map <String , AtomicReference <String >> groupKeyCache = new ConcurrentHashMap <>();
7575
76- private String getGroupKeyContent (String dataId , String group ) throws Exception {
76+ private String getGroupKeyContent (String dataId , String group , boolean refreshed ) throws Exception {
7777 if (groupKeyCache .containsKey (GroupKey .getKey (dataId , group ))) {
7878 return groupKeyCache .get (GroupKey .getKey (dataId , group )).get ();
7979 }
8080 synchronized (this ) {
8181 if (!groupKeyCache .containsKey (GroupKey .getKey (dataId , group ))) {
8282 String content = getNacosConfigManager ().getConfigService ().getConfig (dataId , group , 5000 );
8383 groupKeyCache .put (GroupKey .getKey (dataId , group ), new AtomicReference <>(content ));
84-
84+ if (!refreshed ) {
85+ log .info ("[Nacos Config] refreshed is set to false, not listening config for annotation: dataId={}, group={}" , dataId ,
86+ group );
87+ return content ;
88+ }
8589 log .info ("[Nacos Config] Listening config for annotation: dataId={}, group={}" , dataId ,
8690 group );
8791 getNacosConfigManager ().getConfigService ().addListener (dataId , group , new AbstractListener () {
@@ -115,7 +119,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
115119 Class clazz = bean .getClass ();
116120 NacosConfig annotationBean = AnnotationUtils .findAnnotation (clazz , NacosConfig .class );
117121 if (annotationBean != null ) {
118- handleBeanNacosConfigAnnotation (annotationBean .dataId (), annotationBean .group (), annotationBean .key (), beanName , bean , annotationBean .defaultValue ());
122+ handleBeanNacosConfigAnnotation (annotationBean .dataId (), annotationBean .group (), annotationBean .key (), annotationBean . refreshed (), beanName , bean , annotationBean .defaultValue ());
119123 return bean ;
120124 }
121125
@@ -147,10 +151,10 @@ private void handleFiledAnnotation(Object bean, String beanName, Field field) {
147151 }
148152 }
149153
150- private void handleBeanNacosConfigAnnotation (String dataId , String group , String key , String beanName , Object bean ,
154+ private void handleBeanNacosConfigAnnotation (String dataId , String group , String key , boolean refreshed , String beanName , Object bean ,
151155 String defaultValue ) {
152156 try {
153- String config = getDestContent (getGroupKeyContent (dataId , group ), key );
157+ String config = getDestContent (getGroupKeyContent (dataId , group , refreshed ), key );
154158 if (!org .springframework .util .StringUtils .hasText (config )) {
155159 config = defaultValue ;
156160 }
@@ -163,6 +167,10 @@ private void handleBeanNacosConfigAnnotation(String dataId, String group, String
163167 }
164168
165169 String refreshTargetKey = beanName + "#instance#" ;
170+ if (!refreshed ) {
171+ log .info ("[Nacos Config] refresh is set to false,do not register listener for {} to bean {} " , refreshTargetKey , bean );
172+ return ;
173+ }
166174 TargetRefreshable currentTarget = targetListenerMap .get (refreshTargetKey );
167175 if (currentTarget != null ) {
168176 log .info ("[Nacos Config] reset {} listener from {} to {} " , refreshTargetKey ,
@@ -270,7 +278,7 @@ public String toString() {
270278 return String .format ("sca nacos config listener on bean method %s" , bean + "#" + methodSignature (method ));
271279 }
272280 };
273- nacosPropertiesKeyListener .setLastContent (getGroupKeyContent (dataId , group ));
281+ nacosPropertiesKeyListener .setLastContent (getGroupKeyContent (dataId , group , true ));
274282 getNacosConfigManager ().getConfigService ().addListener (dataId , group ,
275283 nacosPropertiesKeyListener );
276284 targetListenerMap .put (refreshTargetKey , nacosPropertiesKeyListener );
@@ -309,7 +317,7 @@ private void handleMethodNacosConfigListener(NacosConfigListener annotation, Str
309317 "@NacosConfigListener must be over a method with a single parameter" );
310318 }
311319
312- String configInfo = getGroupKeyContent (dataId , group );
320+ String configInfo = getGroupKeyContent (dataId , group , true );
313321 String refreshTargetKey = beanName + "#method#" + methodSignature (method );
314322 TargetRefreshable currentTarget = targetListenerMap .get (refreshTargetKey );
315323 if (currentTarget != null ) {
@@ -433,23 +441,23 @@ private void handleFiledNacosConfigAnnotation(NacosConfig annotation, String bea
433441 String key = annotation .key ();
434442 try {
435443 ReflectionUtils .makeAccessible (field );
436- handleFiledNacosConfigAnnotationInner (dataId , group , key , beanName , bean , field , annotation .defaultValue ());
444+ handleFiledNacosConfigAnnotationInner (dataId , group , key , annotation . refreshed (), beanName , bean , field , annotation .defaultValue ());
437445 }
438446 catch (Exception e ) {
439447 throw new RuntimeException (e );
440448 }
441449 }
442450
443- private void handleFiledNacosConfigAnnotationInner (String dataId , String group , String key , String beanName , Object bean ,
451+ private void handleFiledNacosConfigAnnotationInner (String dataId , String group , String key , boolean refreshed , String beanName , Object bean ,
444452 Field field , String defaultValue ) {
445453 try {
446- String config = getDestContent (getGroupKeyContent (dataId , group ), key );
454+ String config = getDestContent (getGroupKeyContent (dataId , group , refreshed ), key );
447455 if (!org .springframework .util .StringUtils .hasText (config )) {
448456 config = defaultValue ;
449457 }
450458
451459 //primitive type
452- if (handPrimitiveFiled (field , dataId , group , config , key , defaultValue , beanName , bean )) {
460+ if (handPrimitiveFiled (field , dataId , group , config , key , defaultValue , refreshed , beanName , bean )) {
453461 return ;
454462 }
455463
@@ -461,6 +469,11 @@ private void handleFiledNacosConfigAnnotationInner(String dataId, String group,
461469 }
462470
463471 String refreshTargetKey = beanName + "#filed#" + field .getName ();
472+
473+ if (!refreshed ) {
474+ log .info ("[Nacos Config] refresh is set to false,do not register listener for {} to bean {} " , refreshTargetKey , bean );
475+ return ;
476+ }
464477 TargetRefreshable currentTarget = targetListenerMap .get (refreshTargetKey );
465478 if (currentTarget != null ) {
466479 log .info ("[Nacos Config] reset {} listener from {} to {} " , refreshTargetKey ,
@@ -530,7 +543,7 @@ public String toString() {
530543 }
531544 }
532545
533- private boolean handPrimitiveFiled (Field field , String dataId , String group , String config , String key , String defaultValue , String beanName , Object bean ) throws Exception {
546+ private boolean handPrimitiveFiled (Field field , String dataId , String group , String config , String key , String defaultValue , boolean refreshed , String beanName , Object bean ) throws Exception {
534547 if (field .getType ().isPrimitive ()) {
535548
536549 if (org .springframework .util .StringUtils .hasText (config )) {
@@ -543,6 +556,11 @@ private boolean handPrimitiveFiled(Field field, String dataId, String group, Str
543556 }
544557
545558 String refreshTargetKey = beanName + "#filed#" + field .getName ();
559+ if (!refreshed ) {
560+ log .info ("[Nacos Config] refresh is set to false,do not register listener for {} to bean {} " , refreshTargetKey , bean );
561+ return true ;
562+ }
563+
546564 TargetRefreshable currentTarget = targetListenerMap .get (refreshTargetKey );
547565 if (currentTarget != null ) {
548566 log .info ("[Nacos Config] reset {} listener from {} to {} " , refreshTargetKey ,
@@ -729,7 +747,7 @@ private void handleMethodAnnotation(final Object bean, String beanName, final Me
729747 String group = (String ) stringObjectMap .get ("group" );
730748 String key = (String ) stringObjectMap .get ("key" );
731749 String defaultValue = (String ) stringObjectMap .get ("defaultValue" );
732- handleBeanNacosConfigAnnotation (dataId , group , key , beanName , bean , defaultValue );
750+ handleBeanNacosConfigAnnotation (dataId , group , key , true , beanName , bean , defaultValue );
733751 }
734752 }
735753
0 commit comments