普通类型和字符串注入
基本类型和字符串,通过value元素直接指定值。上案例,对照类和配置。
package com.crab.spring.ioc.demo03;
/**
* @author zfd
* @version v1.0
* @date 2022/1/13 15:07
*/
public class SimpleBean {
private String name;
private int age;
private float height;
// 省略 Getter Setter toString()
}
<bean class="com.crab.spring.ioc.demo03.SimpleBean" id="simpleBean">
<property name="name" value="xxx"/>
<property name="age" value="12"/>
<property name="height" value="123.45f"/>
</bean>
引用类型的注入
引用类型类型通过ref标签设置。
public class MyBean {
private RefBean refBean;
public void setRefBean(RefBean refBean) {
this.refBean = refBean;
}
}
public class RefBean {
}
<!-- 引用类型-->
<bean class="com.crab.spring.ioc.demo03.RefBean" id="refBean"/>
<bean class="com.crab.spring.ioc.demo03.MyBean" id="myBean">