26
26
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
27
27
import org .springframework .beans .factory .config .PlaceholderConfigurerSupport ;
28
28
import org .springframework .context .EnvironmentAware ;
29
+ import org .springframework .core .convert .ConversionService ;
30
+ import org .springframework .core .convert .support .DefaultConversionService ;
29
31
import org .springframework .core .env .ConfigurableEnvironment ;
30
32
import org .springframework .core .env .ConfigurablePropertyResolver ;
31
33
import org .springframework .core .env .Environment ;
@@ -240,16 +242,37 @@ public boolean containsProperty(String name) {
240
242
}
241
243
242
244
@ Override
243
- public @ Nullable Object getProperty (String name ) {
245
+ // Declare String as covariant return type, since a String is actually required.
246
+ public @ Nullable String getProperty (String name ) {
244
247
for (PropertySource <?> propertySource : super .source .getPropertySources ()) {
245
248
Object candidate = propertySource .getProperty (name );
246
249
if (candidate != null ) {
247
- return candidate ;
250
+ return convertToString ( candidate ) ;
248
251
}
249
252
}
250
253
return null ;
251
254
}
252
255
256
+ /**
257
+ * Convert the supplied value to a {@link String} using the {@link ConversionService}
258
+ * from the {@link Environment}.
259
+ * <p>This is a modified version of
260
+ * {@link org.springframework.core.env.AbstractPropertyResolver#convertValueIfNecessary(Object, Class)}.
261
+ * @param value the value to convert
262
+ * @return the converted value, or the original value if no conversion is necessary
263
+ * @since 6.2.8
264
+ */
265
+ private @ Nullable String convertToString (Object value ) {
266
+ if (value instanceof String string ) {
267
+ return string ;
268
+ }
269
+ ConversionService conversionService = super .source .getConversionService ();
270
+ if (conversionService == null ) {
271
+ conversionService = DefaultConversionService .getSharedInstance ();
272
+ }
273
+ return conversionService .convert (value , String .class );
274
+ }
275
+
253
276
@ Override
254
277
public String toString () {
255
278
return "ConfigurableEnvironmentPropertySource {propertySources=" + super .source .getPropertySources () + "}" ;
@@ -275,7 +298,8 @@ public boolean containsProperty(String name) {
275
298
}
276
299
277
300
@ Override
278
- public @ Nullable Object getProperty (String name ) {
301
+ // Declare String as covariant return type, since a String is actually required.
302
+ public @ Nullable String getProperty (String name ) {
279
303
return super .source .getProperty (name );
280
304
}
281
305
0 commit comments