Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Dec 29, 2006 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 29, 2006

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

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Thanks for pointing this out! The root of the problem here is the automatic generation of unique bean names, even for (singleton) inner beans: FieldRetrievingFactoryBean then receives the generated name with a "#1" suffix as its bean name, which it doesn't expect and hence can't handle...

This also affects PropertyPathFactoryBean. I've fixed it in both through always extracting the original bean name there.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 2.0.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants