spring-framework: Bean injection fails due to `nullSafeConciseToString()` invoking `isEmpty()` on a `Map`/`Collection` proxy

I have two beans like this:

<alias name="defaultProblematicBean" alias="problematicBean" />
<bean id="defaultProblematicBean"
    class="com.application.ProblematicClass">
    <property name="strategies" ref="problematicStrategiesList" />
</bean>

<alias name="defaultProblematicStrategiesList" alias="problematicStrategiesList" />
<util:list id="defaultProblematicStrategiesList">
    <bean id="bean1" parent="defaultProblematicBean"
        class="com.application.ProblematicClass$ProblematicInnerClass1" />
    <bean id="bean2" parent="defaultProblematicBean"
        class="com.application.ProblematicClass$ProblematicInnerClass2" />
</util:list>

Please note: ProblematicInnerClass1 and ProblematicInnerClass2 are static inner classes inside of ProblematicClass that extend from ProblematicClass.

When I try to start the context, spring throws the following summarized error: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘jdk.proxy4.$Proxy363 implementing java.util.List’ to required type ‘java.util.List’ for property ‘strategies’; nested exception is java.lang.IllegalStateException: Singleton instance not initialized yet

Note: This works fine with 5.3.27 but fails with 5.3.29, we noticed that some changes were made to ObjectUtils.nullSafeConciseToString, which is part of the stack trace.

This is the stacktrace that contains the reference to ObjectUtils.nullSafeConciseToString

Caused by: java.lang.IllegalStateException: Singleton instance not initialized yet
at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.config.AbstractFactoryBean.getSingletonInstance(AbstractFactoryBean.java:188) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.config.AbstractFactoryBean.access$200(AbstractFactoryBean.java:63) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.config.AbstractFactoryBean$EarlySingletonInvocationHandler.invoke(AbstractFactoryBean.java:275) ~[spring-beans-5.3.29.jar:5.3.29]
at jdk.proxy4.$Proxy363.isEmpty(Unknown Source) ~[?:?]
at org.springframework.util.ObjectUtils.nullSafeConciseToString(ObjectUtils.java:981) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.core.convert.ConversionFailedException.<init>(ConversionFailedException.java:52) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:609) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:219) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1756) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1712) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:374) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:127) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:428) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:173) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.3.29.jar:5.3.29]
... 30 more

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I can see a potential regression here indeed for lazy beans of type Collection, caused by #30810. One way or the other, we’ll have to fix this for 6.0.12 and 5.3.30.

I can see a potential regression here indeed for lazy beans of type Collection, caused by #30810. One way or the other, we’ll have to fix this for 6.0.12 and 5.3.30.

I agree.

I’ve put some thought into it, and here are a few brainstorming ideas.

  1. Revert the changes made in #30810 so that we never invoke isEmpty() on a Collection or Map, but keep the changes for array support.
  2. Only invoke isEmpty() if the collection/map is not a proxy, and fall back to a class name based string representation otherwise.
  3. Invoke isEmpty() (or practically the entire nullSafeConciseToString() method if we want to be safer) in a try-catch block, and fall back to a class name based string representation if an exception is thrown.
  4. If we want a targeted fix for this particular use case, we could add some logic to AbstractFactoryBean.EarlySingletonInvocationHandler so that isEmpty() always returns true if the AbstractFactoryBean is !initialized (if the isEmpty() Method is from Map or Collection).

Though, the latter might only be applicable for SetFactoryBean, ListFactoryBean, and MapFactoryBean instead of for AbstractFactoryBean in general.

Additional ideas are welcome.


As a side note, it might be a good idea to implement isEmpty() in the proxy for SetFactoryBean, ListFactoryBean, and MapFactoryBean in any case.

I came across the same issue on my Spring based project

I think the stacktrace of .27 vs .29 is a good indicator of the root cause as you can see below (for the same project, just different spring core version).

On core 5.3.27, the exception is logged only in DEBUG mode, as it is handled by Spring :

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [jdk.proxy5.$Proxy341] to type [@org.springframework.beans.factory.annotation.Required java.util.List<fr.royality.UserLoader>] for value [jdk.proxy5.$Proxy341@16be9b34]; 

nested exception is java.lang.IllegalStateException: Singleton instance not initialized yet

at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.3.27.jar:5.3.27]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192) ~[spring-core-5.3.27.jar:5.3.27]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129) ~[spring-beans-5.3.27.jar:5.3.27]

On 5.3.29, the exception is fatal and breaks the context initialization:

java.lang.IllegalStateException: Singleton instance not initialized yet

at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.config.AbstractFactoryBean.getSingletonInstance(AbstractFactoryBean.java:188) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.config.AbstractFactoryBean.access$200(AbstractFactoryBean.java:63) ~[spring-beans-5.3.29.jar:5.3.29]
at org.springframework.beans.factory.config.AbstractFactoryBean$EarlySingletonInvocationHandler.invoke(AbstractFactoryBean.java:275) ~[spring-beans-5.3.29.jar:5.3.29]

at jdk.proxy4.$Proxy341.isEmpty(Unknown Source) ~[?:?]
at org.springframework.util.ObjectUtils.nullSafeConciseToString(ObjectUtils.java:981) ~[spring-core-5.3.29.jar:5.3.29]

at org.springframework.core.convert.ConversionFailedException.<init>(ConversionFailedException.java:52) ~[spring-core-5.3.29.jar:5.3.29]

at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192) ~[spring-core-5.3.29.jar:5.3.29]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129) ~[spring-beans-5.3.29.jar:5.3.29]

Looks like the error on 5.3.29 breaks the context initialization because when instantiating a new ConversionFailedException, there is the use of ObjectUtils.nullSafeConciseToString which calls the proxy’s isEmpty and this tries to invoke the bean creation again- which fails. On 5.3.27, the ObjectUtils.nullSafeConciseToString did only return the class name as the evan representation without invoking any proxy method.

It looks like this change is related to the commit : https://github.com/spring-projects/spring-framework/commit/a3907a64e528cc055807d02aa4e4e6132aa07a1e

@rafikhamid is right: As Spring is a) a dependency of SAP Commerce as platform, so it’s their issue to deal with and b) it’s not exactly trivial to override dependencies, they will have to fix is quickly it for the bulk of all SAP Commerce users.

Thanks @sbrannen, I’ll give the SAP guys a hint, so they can test this and pull the new Spring jars into their release. Hopefully you’ll don’t have to deal with any more of us SAP CC devs here 😉 Thanks for the support!

@sbrannen Thanks for the info, but @lennartjuette meant that we will wait until SAP Commerce releases a patch using Spring Framework 5.3.30 or higher. because SAP Commerce does not provide a “standard” way to choose dependencies versions.

until a new spring release has been released

FYI: Spring Framework 6.0.12 and 5.3.30 were both released today.

Not knowing where to look i searched and stumbled upon a 5.3.30-SNAPSHOT build from 08.09.2023: https://repo.spring.io/artifactory/snapshot/org/springframework/spring/5.3.30-SNAPSHOT/spring-5.3.30-20230908.144355-28-dist.zip

Using these jars worked for me on my local setup. I’d say the issue was fixed. Assuming that I picked the correct SNAPSHOT build, that is.

We’ll give it a shot!

Thanks, @luisericlf.

I also just noticed that this was the first issue you have created on GitHub – “(their first ever)” as GitHub phrases it.

So, congratulations on that! 👍

Very much appreciated @sbrannen!

We’ll give it a shot!

This has been addressed in 6.0.x in ea4105165119937d1da59704be053f4023d56a9c and backported to 5.3.x, for inclusion in 6.0.12 and 5.3.30.

Please try out the upcoming snapshots and let us know if you run into any further issues.

Thanks!