Skip to content

Property injection fails for arrays of enums if there is more than 1 value [SPR-6547] #11213

Closed
@spring-projects-issues

Description

@spring-projects-issues

Antony Lees opened SPR-6547 and commented

Given an array of enums, if the array is injected using a comma-separated list of string values, the injection will fail if there are more than one value eg

<property name="enumTypes" value="TEST1,TEST2"/>

However, it will work if there is only 1 value eg

<property name="enumTypes" value="TEST1"/>

To test this I tried a list of enums, an array of enums and an array of strings. The strings and the list work, but the enum array does not and fails with this exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enumBugTest' defined in class path resource [EnumTestApplicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'uk.org.sith.jeraz.Test[]' for property 'enumTypes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [uk.org.sith.jeraz.Test] for property 'enumTypes[0]': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:558)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:852)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:422)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at uk.org.sith.jeraz.EnumTestMain.main(EnumTestMain.java:10)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'uk.org.sith.jeraz.Test[]' for property 'enumTypes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [uk.org.sith.jeraz.Test] for property 'enumTypes[0]': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:452)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1351)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [uk.org.sith.jeraz.Test] for property 'enumTypes[0]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:259)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:121)
at org.springframework.beans.TypeConverterDelegate.convertToTypedArray(TypeConverterDelegate.java:478)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:197)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:135)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:442)
... 15 more

My test code is below:

package uk.org.sith.jeraz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EnumTestMain {

public static void main(String[] args) {

EnumTestMain main = new EnumTestMain();
new ClassPathXmlApplicationContext(main.getPaths());

}

public String[] getPaths() {
return new String[] {"EnumTestApplicationContext.xml"};
}

}

package uk.org.sith.jeraz;

import java.util.List;

public class EnumBugTest {

private Test[] enumTypes;

private String[] stringTypes;

private List<Test> enumList;

public void setEnumTypes(Test[] enumTypes) {
this.enumTypes = enumTypes;
}

public void setStringTypes(String[] stringTypes) {
this.stringTypes = stringTypes;
}

public void setEnumList(List<Test> enumList) {
this.enumList = enumList;
}

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="enumBugTest" class="uk.org.sith.jeraz.EnumBugTest">
    <property name="stringTypes" value="HELLO,STRING"/>
    <property name="enumTypes" value="TEST1,TEST2"/>
    <property name="enumList">
        <list>
            <value>TEST1</value>
            <value>TEST2</value>
        </list>
    </property>
</bean>

</beans>


Affects: 3.0 RC3

Issue Links:

Referenced from: commits 5f9b444

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions