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