You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
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());
}
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.
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
The text was updated successfully, but these errors were encountered: