2626import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
2727import org .springframework .beans .factory .config .PlaceholderConfigurerSupport ;
2828import org .springframework .context .EnvironmentAware ;
29+ import org .springframework .core .convert .ConversionService ;
30+ import org .springframework .core .convert .support .DefaultConversionService ;
2931import org .springframework .core .env .ConfigurableEnvironment ;
3032import org .springframework .core .env .ConfigurablePropertyResolver ;
3133import org .springframework .core .env .Environment ;
@@ -240,16 +242,37 @@ public boolean containsProperty(String name) {
240242 }
241243
242244 @ 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 ) {
244247 for (PropertySource <?> propertySource : super .source .getPropertySources ()) {
245248 Object candidate = propertySource .getProperty (name );
246249 if (candidate != null ) {
247- return candidate ;
250+ return convertToString ( candidate ) ;
248251 }
249252 }
250253 return null ;
251254 }
252255
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+
253276 @ Override
254277 public String toString () {
255278 return "ConfigurableEnvironmentPropertySource {propertySources=" + super .source .getPropertySources () + "}" ;
@@ -275,7 +298,8 @@ public boolean containsProperty(String name) {
275298 }
276299
277300 @ 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 ) {
279303 return super .source .getProperty (name );
280304 }
281305
0 commit comments