How to add a property from one bean to another in Spring 3.0?

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(); // define java-based spring bean ctx.registerBeanDefinition("myPotato", genericBeanDefinition(Potato.class) .addPropertyValue("potatoType", "spudzz") .getBeanDefinition()); // read in XML-bean XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx); xmlReader.loadBeanDefinitions( new ClassPathResource("/resources/spring_init.xml")); // print out results System.out.format( "Baby: %s%n%n" + "Potato: %s%n", ctx.getBean(Baby.class), ctx.getBean(Potato.class) ); } } 

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=#{myPotato.potatoType}, potatoThing=Potato{potatoType=spudzz}} Potato: Potato{potatoType=spudzz} 

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.

+4
source share
1 answer

You may need to call ctx.refresh() before getting beans.

From javadoc :

A typical application is to register various bean definitions through the BeanDefinitionRegistry interface, and then call AbstractApplicationContext.refresh () to initialize these beans with application context semantics (processing ApplicationContextAware, automatic detection of BeanFactoryPostProcessors, etc.).

+5
source

All Articles