Skip to content

FieldRetrievingFactoryBean throws java.lang.NoSuchFieldException [SPR-2992] #7678

Closed
@spring-projects-issues

Description

@spring-projects-issues

Steve Gioberti opened SPR-2992 and commented

When using the FieldRetrievingFactoryBean in an XML-based configuration in the name-based "convenience" format to retrieve a static constant thus:

<bean name="foo.bar.MyConstants.A" class="org.springframework.beans.factory.config.Fi eldRetrievingFactoryBean"/>

A second access of the same constant using a second inclusion of '<bean name="foo.bar.MyConstants.A" ... />' results in a org.springframework.beans.factory.BeanCreationException. The eventual cause is "Caused by: java.lang.NoSuchFieldException: A#1".

I have tested this with Spring 2.0.1 and 2.0, which both give the above BeanCreationException error. Back-testing this with Spring 2.0-m1 and 1.2.3 give no BeanCreationException exception.

If I make the FieldRetrievingFactoryBean a "prototype" (i.e. singleton="false"), then this removes the exception.

Is this a fault introduced between 2.0-m1 and 2.0, or was this use-case never formally supported? Any help greatfully received.

Sample files


These are the files I have used to re-create the problem:

MBean.java


package foo.bar;

public class MyBean {

private int propA;

private int propB;

public int getPropA() {
return propA;
}

public void setPropA(int propA) {
this.propA = propA;
}

public int getPropB() {
return propB;
}

public void setPropB(int propB) {
this.propB = propB;
}

}

MyConstants.java


package foo.bar;

public interface MyConstants {
final static int A = 0;
final static int B = 1;
}

Junit: TestFieldRetrievingBeanFactoryBean


package foo.bar;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
import junit.framework.TestCase;

public class TestFieldRetrievingBeanFactoryBean extends TestCase {

public void testIt() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
MyBean b = (MyBean)context.getBean("myBean");
System.out.println(b.getPropA());
}

}

spring-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="myBean" class="foo.bar.MyBean">
<property name="propA">
<bean name="foo.bar.MyConstants.A" class="org.springframework.beans.factory.config.Fi eldRetrievingFactoryBean"/>
</property>
<!-- Second access of foo.bar.MyConstants.A causes BeanCreationException execption -->
<property name="propB">
<bean name="foo.bar.MyConstants.A" class="org.springframework.beans.factory.config.Fi eldRetrievingFactoryBean"/>
</property>
</bean>
</beans>


Affects: 2.0 final, 2.0.1

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