给User对象的name属性注入值
值类型注入:
User.java
package cn.jjs.bean;
public class User {
public User() {
System.out.println("User空参构造器");
}
private String name;
private Integer age;
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 +
'}';
}
}
applicationContext.xml
<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-3.0.xsd">
<bean name="user" class="cn.jjs.bean.User">
<property name="name" value="Tom">property>
<property name="age" value="18">property>
bean>
beans>
Demo.java
package cn.jjs.c_injection;
import cn.jjs.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
@Test
public void Test() {
//1. 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("cn/jjs/c_injection/applicationContext.xml");
//2. 向容器"要"user对象
User u = (User) ac.getBean("user");
//3. 打印对象
System.out.println(u);
}
}
引用类型注入:
Car.java
package cn.jjs.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 "User{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
User.java
package cn.jjs.bean;
public class User {
public User() {
System.out.println("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;
}
}
applicationContext.xml
<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-3.0.xsd">
<bean name="user" class="cn.jjs.bean.User">
<property name="name" value="Tom">property>
<property name="age" value="18">property>
<property name="car" ref="carRef">property>
bean>
<bean name="carRef" class="cn.jjs.bean.Car">
<property name="name" value="Toyota">property>
<property name="color" value="white">property>
bean>
beans>
结果
User空参构造器
User{name='Tom', age=18, car=Car{name='Toyota', color='white'}}
生成User(String name, Car car)有参构造器。
User.java
package cn.jjs.bean;
public class User {
public User() {
System.out.println("User空参构造器");
}
private String name;
private Integer age;
private Car car;
public User(String name, Car car) {
System.out.println("(String 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 +
'}';
}
}
<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-3.0.xsd">
<bean name="carRef" class="cn.jjs.bean.Car">
<property name="name" value="Toyota">property>
<property name="color" value="white">property>
bean>
<bean name="user1" class="cn.jjs.bean.User">
<constructor-arg name="name" value="Tom">constructor-arg>
<constructor-arg name="car" ref="carRef">constructor-arg>
bean>
beans>
结果
User空参构造器
(String name, Car car)
User{name='Tom', age=null, car=Car{name='Toyota', color='white'}}
⚠️
如果遇到构造器的形参列表相同,顺序不同,比如:
User.java
package cn.jjs.bean;
public class User {
public User() {
System.out.println("User空参构造器");
}
private String name;
private Integer age;
private Car car;
public User(Car car, String name) {
this.car = car;
this.name = name;
}
public User(String name, Car car) {
System.out.println("(String 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 +
'}';
}
}
可以通过index
<bean name="user1" class="cn.jjs.bean.User">
<constructor-arg name="name" value="Tom" index="0">constructor-arg>
<constructor-arg name="car" ref="carRef" index="1">constructor-arg>
bean>
则选择:
public User(String name, Car car) {
System.out.println("(String name, Car car)");
this.name = name;
this.car = car;
}
这条构造器。
⚠️
如果遇到形参名相同,则:
User.java
package cn.jjs.bean;
public class User {
public User() {
System.out.println("User空参构造器");
}
private String name;
private Integer age;
private Car car;
public User(Integer name, Car car) {
System.out.println("(Integer name, Car car)");
this.name = name + "";
this.car = car;
}
public User(String name, Car car) {
System.out.println("(String 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 +
'}';
}
}
需要通过给出完整类名type属性,定位构造器。
<bean name="user1" class="cn.jjs.bean.User">
<constructor-arg name="name" value="Tom" index="0" type="java.lang.Integer"> constructor-arg>
<constructor-arg name="car" ref="carRef" index="1">constructor-arg>
bean>
导入p名称空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
使用 p: 完成注入:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="carRef" class="cn.jjs.bean.Car">
<property name="name" value="Toyota">property>
<property name="color" value="white">property>
bean>
<bean name="user2" class="cn.jjs.bean.User" p:name="jack" p:age="30" p:car-ref="carRef">bean>
beans>
本质是set注入。
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="user" class="cn.jjs.bean.User">
<property name="name" value="Tom">property>
<property name="age" value="18">property>
<property name="car" ref="carRef">property>
bean>
<bean name="carRef" class="cn.jjs.bean.Car">
<property name="name" value="Toyota">property>
<property name="color" value="white">property>
bean>
<bean name="user2" class="cn.jjs.bean.User" p:name="jack" p:age="30" p:car-ref="carRef">bean>
<bean name="user3" class="cn.jjs.bean.User">
<property name="name" value="#{user.name}">property>
<property name="age" value="#{user2.age}">property>
<property name="car" ref="carRef">property>
bean>
beans>