准备
准备如下 POJO:
// com.zze.bean.Department
package com.zze.bean;
public class Department {
public Department() {
}
public Department(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department{" +
"name='" + name + '\'' +
'}';
}
}
// com.zze.bean.User
package com.zze.bean;
public class User {
public User() {
}
public User(String name, Integer age, Department department) {
this.name = name;
this.age = age;
this.department = department;
}
private String name;
private Integer age;
private Department department;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", department=" + department +
'}';
}
}
几种注入方式
构造方法注入
<bean name="department" class="com.zze.bean.Department">
<constructor-arg name="name" value="信息部"/>
</bean>
<bean name="user" class="com.zze.bean.User">
<constructor-arg name="name" value="李四"/>
<constructor-arg name="age" value="30"/>
<constructor-arg name="department" ref="department"/>
</bean>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object user = applicationContext.getBean("user");
System.out.println(user);
/*
User{name='李四', age=30, department=Department{name='信息部'}}
*/
setter 注入
<bean name="department" class="com.zze.bean.Department">
<property name="name" value="信息部"/>
</bean>
<bean name="user" class="com.zze.bean.User">
<property name="name" value="张三"/>
<property name="age" value="12"/>
<property name="department" ref="department"/>
</bean>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object user = applicationContext.getBean("user");
System.out.println(user);
/*
User{name='张三', age=12, department=Department{name='信息部'}}
*/
p 名称空间注入
<!--
在 Spring 2.5 之后支持。
需在 beans 标签中添加属性 xmlns:p="http://www.springframework.org/schema/p" 来引入 p 名称空间
-->
<bean name="department" class="com.zze.bean.Department" p:name="信息部"/>
<bean name="user" class="com.zze.bean.User" p:name="张三" p:age="21" p:department-ref="department"/>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object user = applicationContext.getBean("user");
System.out.println(user);
/*
User{name='张三', age=21, department=Department{name='信息部'}}
*/
SpEL 注入
<!--
在 Spring 3.0 之后支持。
-->
<bean name="department" class="com.zze.bean.Department">
<property name="name" value="#{'推广部'}"/>
</bean>
<bean name="user" class="com.zze.bean.User">
<property name="name" value="#{'李四'}"/>
<!--在表达式中可以有计算操作,并且可以直接调用对象属性及方法-->
<property name="age" value="#{10+22}"/>
<property name="department" ref="department"/>
</bean>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object user = applicationContext.getBean("user");
System.out.println(user);
/*
User{name='李四', age=32, department=Department{name='推广部'}}
*/
其它
注入Array
// com.zze.bean.Customer
package com.zze.bean;
import java.util.Arrays;
public class Customer {
private String name;
private String[] hobbies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", hobbies=" + Arrays.toString(hobbies) +
'}';
}
}
<!--
List 和 Set 的注入方式与 Array 一致
-->
<bean name="customer" class="com.zze.bean.Customer">
<property name="name" value="二狗"/>
<property name="hobbies">
<list>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</list>
</property>
</bean>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object user = applicationContext.getBean("customer");
System.out.println(user);
/*
Customer{name='二狗', hobbies=[吃饭, 睡觉, 打豆豆]}
*/
注入 Map
// com.zze.bean.TestBean
package com.zze.bean;
import java.util.Map;
public class TestBean {
private Map<String, Object> map;
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
@Override
public String toString() {
return "TestBean{" +
"map=" + map +
'}';
}
}
<bean id="testBean" class="com.zze.bean.TestBean">
<property name="map">
<map>
<entry key="1" value="a"/>
<entry key="2" value="b"/>
<entry key="3" value="c"/>
</map>
</property>
</bean>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object testBean = applicationContext.getBean("testBean");
System.out.println(testBean);
/*
TestBean{map={1=a, 2=b, 3=c}}
*/
评论区