2727import com .ctrip .framework .apollo .spring .util .SpringInjector ;
2828import com .ctrip .framework .apollo .util .ConfigUtil ;
2929import com .google .common .base .Preconditions ;
30+ import com .google .common .collect .Sets ;
3031import com .google .gson .Gson ;
3132import java .lang .reflect .Field ;
3233import java .lang .reflect .Method ;
3334import java .lang .reflect .Type ;
3435import java .util .Set ;
35-
36- import com .google .common .collect .Sets ;
36+ import javax .annotation .Nullable ;
3737import org .slf4j .Logger ;
3838import org .slf4j .LoggerFactory ;
3939import org .springframework .beans .BeansException ;
@@ -120,12 +120,7 @@ private void processApolloConfigChangeListener(final Object bean, final Method m
120120 String [] namespaces = annotation .value ();
121121 String [] annotatedInterestedKeys = annotation .interestedKeys ();
122122 String [] annotatedInterestedKeyPrefixes = annotation .interestedKeyPrefixes ();
123- ConfigChangeListener configChangeListener = new ConfigChangeListener () {
124- @ Override
125- public void onChange (ConfigChangeEvent changeEvent ) {
126- ReflectionUtils .invokeMethod (method , bean , changeEvent );
127- }
128- };
123+ ConfigChangeListener configChangeListener = changeEvent -> ReflectionUtils .invokeMethod (method , bean , changeEvent );
129124
130125 Set <String > interestedKeys =
131126 annotatedInterestedKeys .length > 0 ? Sets .newHashSet (annotatedInterestedKeys ) : null ;
@@ -145,18 +140,15 @@ public void onChange(ConfigChangeEvent changeEvent) {
145140 }
146141 }
147142
148-
149143 private void processApolloJsonValue (Object bean , String beanName , Field field ) {
150144 ApolloJsonValue apolloJsonValue = AnnotationUtils .getAnnotation (field , ApolloJsonValue .class );
151145 if (apolloJsonValue == null ) {
152146 return ;
153147 }
154- String placeholder = apolloJsonValue .value ();
155- Object propertyValue = placeholderHelper
156- .resolvePropertyValue (this .configurableBeanFactory , beanName , placeholder );
157148
158- // propertyValue will never be null, as @ApolloJsonValue will not allow that
159- if (!(propertyValue instanceof String )) {
149+ String placeholder = apolloJsonValue .value ();
150+ Object propertyValue = this .resolvePropertyValue (beanName , placeholder );
151+ if (propertyValue == null ) {
160152 return ;
161153 }
162154
@@ -181,19 +173,16 @@ private void processApolloJsonValue(Object bean, String beanName, Method method)
181173 if (apolloJsonValue == null ) {
182174 return ;
183175 }
184- String placeHolder = apolloJsonValue .value ();
185-
186- Object propertyValue = placeholderHelper
187- .resolvePropertyValue (this .configurableBeanFactory , beanName , placeHolder );
188176
189- // propertyValue will never be null, as @ApolloJsonValue will not allow that
190- if (!(propertyValue instanceof String )) {
177+ String placeHolder = apolloJsonValue .value ();
178+ Object propertyValue = this .resolvePropertyValue (beanName , placeHolder );
179+ if (propertyValue == null ) {
191180 return ;
192181 }
193182
194183 Type [] types = method .getGenericParameterTypes ();
195184 Preconditions .checkArgument (types .length == 1 ,
196- "Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters" ,
185+ "Ignore @ApolloJsonValue setter {}.{}, expecting 1 parameter, actual {} parameters" ,
197186 bean .getClass ().getName (), method .getName (), method .getParameterTypes ().length );
198187
199188 boolean accessible = method .isAccessible ();
@@ -204,14 +193,26 @@ private void processApolloJsonValue(Object bean, String beanName, Method method)
204193 if (configUtil .isAutoUpdateInjectedSpringPropertiesEnabled ()) {
205194 Set <String > keys = placeholderHelper .extractPlaceholderKeys (placeHolder );
206195 for (String key : keys ) {
207- SpringValue springValue = new SpringValue (key , apolloJsonValue .value (), bean , beanName ,
208- method , true );
196+ SpringValue springValue = new SpringValue (key , placeHolder , bean , beanName , method , true );
209197 springValueRegistry .register (this .configurableBeanFactory , key , springValue );
210198 logger .debug ("Monitoring {}" , springValue );
211199 }
212200 }
213201 }
214202
203+ @ Nullable
204+ private Object resolvePropertyValue (String beanName , String placeHolder ) {
205+ Object propertyValue = placeholderHelper
206+ .resolvePropertyValue (this .configurableBeanFactory , beanName , placeHolder );
207+
208+ // propertyValue will never be null, as @ApolloJsonValue will not allow that
209+ if (!(propertyValue instanceof String )) {
210+ return null ;
211+ }
212+
213+ return propertyValue ;
214+ }
215+
215216 private Object parseJsonValue (String json , Type targetType ) {
216217 try {
217218 return GSON .fromJson (json , targetType );
0 commit comments