[英]How to assign a class property passing value to class constructor using Dependency injection in spring?
I am kind of super new to spring.我对春天有点陌生。 I need to assign a value to my class property using the constructor.我需要使用构造函数为我的类属性分配一个值。 I am using the class constructor method in spring dependency injection to do that.我在 spring 依赖注入中使用类构造函数方法来做到这一点。 How to configure bean XML to do that.如何配置 bean XML 来做到这一点。
MyBean1.class: MyBean1.类:
public class MyBean1{
private MyBean2 bean2;
public MyBean1(MyBean2 bean2){
this.bean2=bean2;
}
}
XML: XML:
<!-- Definition for bean1 -->
<bean id = "bean1" class = "com.MyBean1">
<constructor-arg ref = "bean2"/>
</bean>
<!-- Definition for bean2 -->
<bean id = "bean2" class = "com.MyBean2"></bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.