第一种:set方法注入
首先创建两个实体类 Car 和 User:
package pers.zhang.bean;
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 + "]";
}
}
package pers.zhang.bean;
public class User {
private String name;
private Integer age;
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
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;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
在applicationContext.xml中进行配置:
创建测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.User;
public class Demo {
@Test
public void fun1(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"user对象
User u = (User) ac.getBean("user");
//3 打印user对象
System.out.println(u);
}
}
运行JUnit测试输出:
User [name=tom, age=18, car=Car [name=保时捷, color=红色]]
第二种方式:构造函数注入
首先,为User类添加3个构造函数:
package pers.zhang.bean;
public class User {
private String name;
private Integer age;
private Car car;
public User(String name, Car car) {
System.out.println("User(String name, Car car)!!");
this.name = name;
this.car = car;
}
public User(Car car,String name) {
System.out.println("User(Car car,String name)!!");
this.name = name;
this.car = car;
}
public User(Integer name, Car car) {
System.out.println("User(Integer name, Car car)!!");
this.name = name+"";
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
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;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
applicationContext.xml配置:
index:用于确定参数的位置
type:用于确定参数的类型
这两个属性可以完全确定一个构造函数。
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.User;
public class Demo {
@Test
public void fun2(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"user对象
User u = (User) ac.getBean("user2");
//3 打印user对象
System.out.println(u);
}
}
运行JUnit测试输出:
User(Integer name, Car car)!!
User [name=24, age=null, car=Car [name=保时捷, color=红色]]
第三种方式:P名称空间注入(Spring2.x版本后提供的方式)
其本质还是调空参构造,使用set方法注入。
所以我们为User添加一个显式的空参构造:
package pers.zhang.bean;
public class User {
private String name;
private Integer age;
private Car car;
public User() {
super();
}
public User(String name, Car car) {
System.out.println("User(String name, Car car)!!");
this.name = name;
this.car = car;
}
public User(Car car,String name) {
System.out.println("User(Car car,String name)!!");
this.name = name;
this.car = car;
}
public User(Integer name, Car car) {
System.out.println("User(Integer name, Car car)!!");
this.name = name+"";
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
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;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
一定记得引入p名称空间!
xmlns:p="http://www.springframework.org/schema/p"
applicationContext.xml配置:
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.User;
public class Demo {
@Test
public void fun3(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"user对象
User u = (User) ac.getBean("user3");
//3 打印user对象
System.out.println(u);
}
}
运行JUnit测试输出:
User [name=Jack, age=20, car=Car [name=保时捷, color=红色]]
第四种方式:SpEL注入(Spring Expression Language):
如果存在此种需求:取其他对象的属性值来创建新的对象,就可以使用SpEL。
SpEL类似于EL表达式,格式为 #{ }。
需要注意的是,SpEL不支持引用类型注入。
applicationContext.xml配置:
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.User;
public class Demo {
@Test
public void fun4(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"user对象
User u = (User) ac.getBean("user4");
//3 打印user对象
System.out.println(u);
}
}
运行JUnit测试输出:
User [name=tom, age=18, car=Car [name=保时捷, color=红色]]
复杂类型注入(数组、List、Map、prperties)
创建一个CollectionBean类:
package pers.zhang.bean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class CollectionBean {
private Object[] arr;//数组类型注入
private List list;//list/set 类型注入
private Map map;//map类型注入
private Properties prop;//properties类型注入
public Object[] getArr() {
return arr;
}
public void setArr(Object[] arr) {
this.arr = arr;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", prop=" + prop
+ "]";
}
}
- 数组注入:
- 如果只向数组中注入一个值(对象),可以直接使用 value | ref 即可。
- 如果向数组中注入多个值(对象):
tom
jerry
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.CollectionBean;
public class Demo {
@Test
public void fun5(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"cb对象
CollectionBean cb = (CollectionBean) ac.getBean("cb");
//3 打印cb对象
System.out.println(cb);
}
}
运行JUnit测试输出:
CollectionBean [arr=[tom, jerry, User [name=tom, age=18, car=Car [name=保时捷, color=红色]]],
list=null,
map=null,
prop=null]
- List注入
List与数组相似,如果只注入一个值(对象)使用value | ref 即可。此处不再举例。
注入多个值(对象)配置如下:
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.CollectionBean;
public class Demo {
@Test
public void fun5(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"cb对象
CollectionBean cb = (CollectionBean) ac.getBean("cb");
//3 打印cb对象
System.out.println(cb);
}
}
运行JUnit测试输出:
CollectionBean [arr=null,
list=[jack, rose, User [name=tom, age=18, car=Car [name=保时捷, color=红色]]],
map=null,
prop=null]
- Map注入:
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.CollectionBean;
public class Demo {
@Test
public void fun5(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"cb对象
CollectionBean cb = (CollectionBean) ac.getBean("cb");
//3 打印cb对象
System.out.println(cb);
}
}
运行JUnit测试输出:
CollectionBean [arr=null,
list=null,
map={url=jdbc:mysql:///crm,
user=User [name=tom, age=18, car=Car [name=保时捷, color=红色]],
User [name=Jerry, age=10, car=Car [name=保时捷, color=红色]]=User [name=Jerry, age=10, car=Car [name=保时捷, color=红色]]},
prop=null]
- Property注入
com.jdbc.mysql.Driver
root
1234
测试方法:
package pers.zhang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.zhang.bean.CollectionBean;
public class Demo {
@Test
public void fun5(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器"要"cb对象
CollectionBean cb = (CollectionBean) ac.getBean("cb");
//3 打印cb对象
System.out.println(cb);
}
}
运行JUnit测试输出:
CollectionBean [arr=null,
list=null,
map=null,
prop={driverClass=com.jdbc.mysql.Driver, password=1234, userName=root}]