Java 在Spring3.0中,如何将属性从一个bean注入到另一个bean中?
在Spring3.0.2中,我试图将Bean A的属性注入到另一个Bean B中,但是SpringEL不起作用 Bean A是在Java中手动创建的。Bean B是通过XML创建的 在这种情况下,豆A是马铃薯,豆B是婴儿(都在springinit包装中) 豆A(土豆): 豆B(婴儿): 在我的主类中,我创建了一个土豆,并在尝试制作婴儿时在XML中使用它Java 在Spring3.0中,如何将属性从一个bean注入到另一个bean中?,java,spring,Java,Spring,在Spring3.0.2中,我试图将Bean A的属性注入到另一个Bean B中,但是SpringEL不起作用 Bean A是在Java中手动创建的。Bean B是通过XML创建的 在这种情况下,豆A是马铃薯,豆B是婴儿(都在springinit包装中) 豆A(土豆): 豆B(婴儿): 在我的主类中,我创建了一个土豆,并在尝试制作婴儿时在XML中使用它 package springinit; import static org.springframework.beans.factory.sup
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:
我希望孩子的名字是“Spudz”,这是myPotato的财产。为什么spring不将这个值注入婴儿
谢谢你的阅读。我希望它足够清晰。在获取bean之前,可能需要调用
ctx.refresh()
发件人:
典型用法是通过BeanDefinitionRegistry接口注册各种bean定义,然后调用AbstractApplicationContext.refresh()以使用应用程序上下文语义初始化这些bean(处理ApplicationContextAware、自动检测BeanFactory后处理器等)
成功了!谢谢你的帮助!实际上,我希望根据命令行参数为JMX服务器注入端口号,因此在加载JMX文件之前,我将调用ctx.refresh()。
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 + '}';
}
}
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)
);
}
}
<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>
Baby: Baby{name=#{myPotato.potatoType}, potatoThing=Potato{potatoType=spudzz}}
Potato: Potato{potatoType=spudzz}