In Spring 3.0.2, I'm trying to insert a Bean A property into another Bean B, but EL Spring is not working.
Bean A is created manually in Java. Bean B is created through XML.
In this case, Bean A is potato and Bean B is Baby (both in springinit package).
Bean A (potato):
public class Potato { String potatoType; public String getPotatoType() { return potatoType; } public void setPotatoType(String potatoType) { this.potatoType = potatoType; } @Override public String toString() { return "Potato{" + "potatoType=" + potatoType + '}'; } }
Bean B (Baby):
public class Baby { private String name; private Potato potatoThing; public String getName() { return name; } public void setName(String name) { this.name = name; } public Potato getPotatoThing() { return potatoThing; } public void setPotatoThing(Potato p) { this.potatoThing = p; } @Override public String toString() { return "Baby{" + "name=" + name + ", potatoThing=" + potatoThing + '}'; } }
In my main class, I create potatoes and use it in XML when trying to create Baby
package springinit; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.ClassPathResource; public class Main { public static void main(String[] args) { GenericApplicationContext ctx= new GenericApplicationContext();
Here is my spring_init.xml:
<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="myBaby" class="springinit.Baby" depends-on="myPotato"> <property name="name" value="#{myPotato.potatoType}" /> <property name="potatoThing"> <ref bean="myPotato" /> </property> </bean> </beans>
When I run main, I get this output:
Baby: Baby{name=
I want the baby name to be "spudzz", which is the property of myPotato. Why doesn't Spring introduce this value to a child?
Thank you for reading. Hope this was clear enough.
source share