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)
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.
isEmpty()on aCollectionorMap, but keep the changes for array support.isEmpty()if the collection/map is not a proxy, and fall back to a class name based string representation otherwise.isEmpty()(or practically the entirenullSafeConciseToString()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.AbstractFactoryBean.EarlySingletonInvocationHandlerso thatisEmpty()always returnstrueif theAbstractFactoryBeanis!initialized(if theisEmpty()Methodis fromMaporCollection).Though, the latter might only be applicable for
SetFactoryBean,ListFactoryBean, andMapFactoryBeaninstead of forAbstractFactoryBeanin general.Additional ideas are welcome.
As a side note, it might be a good idea to implement
isEmpty()in the proxy forSetFactoryBean,ListFactoryBean, andMapFactoryBeanin 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 :
On 5.3.29, the exception is fatal and breaks the context initialization:
Looks like the error on 5.3.29 breaks the context initialization because when instantiating a new
ConversionFailedException, there is the use ofObjectUtils.nullSafeConciseToStringwhich calls the proxy’sisEmptyand this tries to invoke the bean creation again- which fails. On 5.3.27, theObjectUtils.nullSafeConciseToStringdid 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.
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.
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.xin ea4105165119937d1da59704be053f4023d56a9c and backported to5.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!