如何在Spring 3.0中将属性从一个bean注入另一个bean?

时间:2010-11-23 15:38:43

标签: java spring

在Spring 3.0.2中,我试图将Bean A的属性注入另一个Bean B,但是Spring EL无效。

Bean A是用Java手动创建的。 Bean B是通过XML创建的。

在这种情况下 Bean A是Potato Bean B是Baby (两者都在springinit包中)。

Bean A(马铃薯):

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(宝贝):

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 + '}';
    }
}

在我的Main类中,我创建了一个Potato并在尝试制作Baby时在XML中使用它

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)
        );
    }
}

这是我的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>

当我运行main时,我得到了这个输出:

Baby: Baby{name=#{myPotato.potatoType}, potatoThing=Potato{potatoType=spudzz}}

Potato: Potato{potatoType=spudzz}

我希望宝宝的名字是“spudzz”,这是myPotato的财产。为什么不春天将这个值注入婴儿?

感谢您的阅读。我希望它足够清楚。

1 个答案:

答案 0 :(得分:5)

也许你需要在获得bean之前调用ctx.refresh()

来自javadoc

  

典型用法是通过BeanDefinitionRegistry接口注册各种bean定义,然后调用AbstractApplicationContext.refresh()以使用应用程序上下文语义初始化这些bean(处理ApplicationContextAware,自动检测BeanFactoryPostProcessors等)。