首先创建两个实体类 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中进行配置:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="pers.zhang.bean.User" >
<property name="name" value="tom" >property>
<property name="age" value="18" >property>
<property name="car" ref="car" >property>
bean>
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
beans>
创建测试方法:
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:用于确定参数的类型
这两个属性可以完全确定一个构造函数。
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
<bean name="user2" class="pers.zhang.bean.User" >
<constructor-arg name="name" value="24" index="0" type="java.lang.Integer" >constructor-arg>
<constructor-arg name="car" ref="car" index="1" >constructor-arg>
bean>
beans>
测试方法:
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=红色]]
其本质还是调空参构造,使用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配置:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
<bean name="user3" class="pers.zhang.bean.User" p:name="Jack" p:age="20" p:car-ref="car" >bean>
beans>
测试方法:
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。
SpEL类似于EL表达式,格式为 #{ }。
需要注意的是,SpEL不支持引用类型注入。
applicationContext.xml配置:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="pers.zhang.bean.User" >
<property name="name" value="tom" >property>
<property name="age" value="18" >property>
<property name="car" ref="car" >property>
bean>
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
<bean name="user4" class="pers.zhang.bean.User" >
<property name="name" value="#{user.name}" >property>
<property name="age" value="#{user.age}" >property>
<property name="car" ref="car" >property>
bean>
beans>
测试方法:
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=红色]]
创建一个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
+ "]";
}
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="cb" class="pers.zhang.CollectionBean" >
<property name="arr" value="tom" >property>
bean>
beans>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="pers.zhang.bean.User" >
<property name="name" value="tom" >property>
<property name="age" value="18" >property>
<property name="car" ref="car" >property>
bean>
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
<bean name="cb" class="pers.zhang.CollectionBean" >
<property name="arr">
<array>
<value>tomvalue>
<value>jerryvalue>
<ref bean="user" />
array>
property>
bean>
beans>
测试方法:
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]
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="pers.zhang.bean.User" >
<property name="name" value="tom" >property>
<property name="age" value="18" >property>
<property name="car" ref="car" >property>
bean>
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
<bean name="cb" class="pers.zhang.CollectionBean" >
<property name="list" >
<list>
<value>jackvalue>
<value>rosevalue>
<ref bean="user" />
list>
property>
bean>
beans>
测试方法:
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]
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="pers.zhang.bean.User" >
<property name="name" value="tom" >property>
<property name="age" value="18" >property>
<property name="car" ref="car" >property>
bean>
<bean name="user2" class="pers.zhang.bean.User" >
<property name="name" value="Jerry" >property>
<property name="age" value="10" >property>
<property name="car" ref="car" >property>
bean>
<bean name="car" class="pers.zhang.bean.Car" >
<property name="name" value="保时捷" >property>
<property name="color" value="红色" >property>
bean>
<bean name="cb" class="pers.zhang.CollectionBean" >
<property name="map" >
<map>
<entry key="url" value="jdbc:mysql:///crm" >entry>
<entry key="user" value-ref="user" >entry>
<entry key-ref="user2" value-ref="user2" >entry>
map>
property>
bean>
beans>
测试方法:
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]
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="cb" class="pers.zhang.CollectionBean" >
<property name="prop" >
<props>
<prop key="driverClass">com.jdbc.mysql.Driverprop>
<prop key="userName">rootprop>
<prop key="password">1234prop>
props>
property>
bean>
beans>
测试方法:
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}]