JAVA框架spring复杂类型注入
2018-03-06 15:55
399 查看
我们在以前的文章说过简单的set属性注入,现在,我们说说复杂类型注入,例如成员变量为类,List,Map,Properties等类型的
先说类,我们有个类:
public class Car {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
}然后,我们的User成员变量car就是Car类型的,如下:public class User {
private String name;
private int age;
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}我们在配置user的时候,怎么注入car的值呢?
首先,car先注册:<bean name ="car" class="com.fei.demo1.Car">
<property name="name" value="byd"></property>
<property name="color" value="白色"></property>
</bean>然后,我们在注册user:<bean name="user" class="com.fei.demo1.User" >
<property name="name" value="tom"></property>
<property name="age" value = "18"></property>
<property name="car" ref="car"></property>
</bean>
List是个数组,可以存众多值,假如我们有个成员变量list是List类型的,那么要赋值,是这样:<property name="list">
<list>
<value>"1124"</value>
<value>112</value>
</list>
</property>先定位成员变量名,然后一组list标签,value标签赋值
Map和properties的套路跟List相似:
Map:<property name="map">
<map>
<entry key="url" value="www.baidu.com"></entry>
<entry key="user" value-ref ="car"></entry>
</map>
</property>Properties:<property name="properties">
<props >
<prop key="urls">jdbc:mysql://localhost</prop>
<prop key="用户">root</prop>
</props>
</property>
先说类,我们有个类:
public class Car {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
}然后,我们的User成员变量car就是Car类型的,如下:public class User {
private String name;
private int age;
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}我们在配置user的时候,怎么注入car的值呢?
首先,car先注册:<bean name ="car" class="com.fei.demo1.Car">
<property name="name" value="byd"></property>
<property name="color" value="白色"></property>
</bean>然后,我们在注册user:<bean name="user" class="com.fei.demo1.User" >
<property name="name" value="tom"></property>
<property name="age" value = "18"></property>
<property name="car" ref="car"></property>
</bean>
<property name="car" ref="car"></property>与普通相比,ref变了,ref关联到car,这样我们就可以获得car实例了,运行结果:User [name=tom, age=18, car=Car [name=byd, color=白色]]接下来,我们说下List
List是个数组,可以存众多值,假如我们有个成员变量list是List类型的,那么要赋值,是这样:<property name="list">
<list>
<value>"1124"</value>
<value>112</value>
</list>
</property>先定位成员变量名,然后一组list标签,value标签赋值
Map和properties的套路跟List相似:
Map:<property name="map">
<map>
<entry key="url" value="www.baidu.com"></entry>
<entry key="user" value-ref ="car"></entry>
</map>
</property>Properties:<property name="properties">
<props >
<prop key="urls">jdbc:mysql://localhost</prop>
<prop key="用户">root</prop>
</props>
</property>
相关文章推荐
- 【Spring实战】—— 7 复杂集合类型的注入
- spring复杂类型属性注入
- Spring(8)注入复杂类型属性
- SSH与SSM学习之Spring08——Spring属性注入之复杂类型注入
- Spring注入复杂类型属性
- Spring属性注入【三】 复杂类型注入
- Spring中复杂类型注入
- Spring_Spring_教程8_注入_复杂类型的注入
- Spring注入复杂类型属性
- 【Spring实战】—— 7 复杂集合类型的注入
- Spring的IOC逐层深入——依赖注入的两种实现类型(四)
- Spring注入容器类型List、Map、Properties、Set
- Spring3注入类型(Injection Type)
- Spring的DI(Ioc) - 注入集合类型
- Spring 【引用】类型【注入】
- spring注入类型要点
- Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
- Spring集合类型数据的注入
- 02_Spring3.2_属性类型设值注入
- spring--注入类型--构造方法(不常用)