构造函数注入
一、创建实体类
public class UserBean {
private String username;
private Integer age;
private Date birthday;
public UserBean(String username, Integer age, Date birthday) {
this.username = username;
this.age = age;
this.birthday = birthday;
}
}
二、创建 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="user" class="chu.yi.bo.UserBean">
<constructor-arg name="username" value="xiaoming"/>
<constructor-arg name="age" value="10"/>
<constructor-arg name="birthday" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
三、constructor-arg 标签的属性
属性 | 方法 |
---|---|
index | 指定参数在构造函数参数列表的索引位置 |
type | 指定参数在构造函数中的数据类型 |
name | 指定参数在构造函数中的名称 |
value | 赋的值是基本数据类型和 String 类型 |
ref | 赋的值是其他 bean 类型,必须得是在配置文件中配置过的 bean |
Set 方法注入
一、创建实体类
public class UserBean {
private String username;
private Integer age;
private Date birthday;
public void setUsername(String username) {
this.username = username;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
二、创建 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="user" class="chu.yi.bo.UserBean">
<property name="username" value="xiaoming"/>
<property name="age" value="10"/>
<property name="birthday" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
三、property 标签属性
属性 | 说明 |
---|---|
name | set 方法后面的部分 |
ref | 属性赋值是其他 bean |
value | 属性赋值是基本数据类型和 string 类型的 |
P 命名空间注入
一、创建实体类
public class UserBean {
private String username;
private Integer age;
private Date birthday;
public void setUsername(String username) {
this.username = username;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
二、创建 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">
<bean id="user" class="chu.yi.bo.UserBean"
p:username="xiaoming" p:age="18" p:birthday-ref="date"/>
<bean id="date" class="java.util.Date"/>
</beans>
注入集合属性
一、创建实体类
public class MyArray {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
}
二、创建 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="myArray" class="chu.yi.bo.MyArray">
<!-- 给数组注入数据 -->
<property name="myStrs">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<!-- 注入 list 集合数据 -->
<property name="myList">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<!-- 注入 set 集合数据 -->
<property name="mySet">
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
<!-- 注入 Map 数据 -->
<property name="myMap">
<props>
<prop key="testA">aaa</prop>
<prop key="testB">bbb</prop>
</props>
</property>
<!-- 注入 properties 数据 -->
<property name="myProps">
<map>
<entry key="testA" value="aaa"></entry>
<entry key="testB">
<value>bbb</value>
</entry>
</map>
</property>
</bean>
</beans>