Closed
Description
Stepan Koltsov opened SPR-8432 and commented
Regression in 3.1 M2. When subclass overrides getter with narrower return type, Spring fails to set property.
public class ReadWritePropertyErrorMain {
public static void main(String[] args) {
final String contextXml =
"<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-3.0.xsd" +
" '" +
">" +
" <bean id='t' class='" + MyTemplate5.class.getName() + "'/>" +
" <bean class='" + MyDaoSupport5.class.getName() + "'>" +
" <property name='myTemplate4' ref='t'/>" +
" </bean>" +
"</beans>";
AbstractXmlApplicationContext applicationContext = new AbstractXmlApplicationContext() {
@Override
protected Resource[] getConfigResources() {
return new Resource[] { new ByteArrayResource(contextXml.getBytes()) };
}
};
applicationContext.refresh();
System.out.println("$");
}
}
public class MyDaoSupport4 {
private MyTemplate4 myTemplate4;
public void setMyTemplate4(MyTemplate4 jdbcTemplate4) {
this.myTemplate4 = jdbcTemplate4;
}
public MyTemplate4 getMyTemplate4() {
return myTemplate4;
}
}
public class MyDaoSupport5 extends MyDaoSupport4 {
@Override
public MyTemplate5 getMyTemplate4() { // <-- narrow subtype is here
return (MyTemplate5) super.getMyTemplate4();
}
}
public class MyTemplate4 { }
public class MyTemplate5 extends MyTemplate4 { }
Error:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ru.yandex.commune.junk.stepancheg.spring.readWrite.MyDaoSupport5#0' defined in resource loaded from byte array: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Read and write parameter types are not the same
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at ru.yandex.commune.junk.stepancheg.spring.readWrite.ReadWriteProperty.main(ReadWriteProperty.java:36)
Caused by: java.lang.IllegalStateException: Read and write parameter types are not the same
at org.springframework.core.convert.Property.resolveMethodParameter(Property.java:137)
at org.springframework.core.convert.Property.<init>(Property.java:56)
at org.springframework.beans.BeanWrapperImpl.property(BeanWrapperImpl.java:1174)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:497)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1371)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1330)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 9 more
Affects: 3.1 M2
Attachments:
- mylyn-context.zip (8.61 kB)
Issue Links:
- Overhaul non-void JavaBean write method support [SPR-10029] #14663 Overhaul non-void JavaBean write method support