• 依赖注入:本质就是set注入
  1. 依赖:bean对象的创建依赖于容器
  2. 注入:bean对象中的所有属性由容器来注入

1.构造器注入:创建有参构造方法的对象时

首先在pojo包下创建一个实体类User

package pojo;

public class User {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println(name+"name");
}

public User(String name) {
this.name = name;
}
public User(){
System.out.println("User的无参构造");
}
}

1.使用无参构造创建对象(默认方法)
在默认情况下,spring是使用无参构造方法来创建对象的
2.使用有参构造创建对象

  1. 用下标的方式构造:
    官方的例子:
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/> </bean>

User创建对象实例:

<!--有参构造的第一种方式,下标赋值-->
<bean id="user" class="pojo.User">
<constructor-arg index="0" value="张三"/>
</bean>

把构造的参数分为0-n…,然后依次赋值.

  1. 用参数类型的方式构造

官方例子:

<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/> </bean>

User创建对象实例:

<bean id="user" class="pojo.User">
<constructor-arg type="java.lang.String" value="张三"/>
</bean>
  1. 通过参数名来给参数赋值,这也是最为人所接受的
    User创建对象实例
<bean id="user" class="pojo.User">
<constructor-arg name = "name" value="张三"/>
</bean>

当bean一注册的时候,就已经被Spring实例化了,要使用对象的时候直接去容器中取就可以了。在配置文件加载的时候,容器中管理的对象就已经初始化了

xmlns:c="http://www.springframework.org/schema/c"

可以通过上面一行代码在applicationContext.xml上加一个c构造器,这样就可以实现在bean标签内用c:直接代替constructor-arg标签:

<bean id="user" class="pojo.User" c:name="张三"/>

2.通过set方式注入

【环境搭建】
1.复杂类型

package pojo;
@Data
@AllArgsConstructor
public class Address {
private String address;

2.真实测试对象

package pojo;

import java.util.*;
@Data
@NoArgsConstructor
public class Student {
    private String name;
    private Address address;
    private String[] book;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;

下面就是如何把这些值在创建对象的时候全部注射进去。
1.普通类型注入:

<bean id="student" class="pojo.Student">
<property name="name" value="张三"/>
</bean>

这就是普通值的注入,直接用属性值的名字注入即可,这里是"name"
2.bean注入,如果一个类的属性是另一个类的对象就需要用到bean注入,例如Student类的属性里有一个address是Address类的对象

<bean id="student" class="pojo.Student">
<property name="address" ref="address"/>
</bean>
<bean id="address" class="pojo.Address">
<constructor-arg name="address" value="中国"/>
</bean>

3.数组注入:在property标签内,有很多返回类型的标签,数组对应的是array

<bean id="student" class="pojo.Student">
<!--数组注入-->
<property name="book">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国</value>
</array>
</property>
</bean>

4.list列表注入:和数组一样也是在property标签下添加对应标签,然后再加value

<bean id="student" class="pojo.Student">
<property name="hobby">
<list>
<value>听歌</value>
<value>玩游戏</value>
<value>看电影</value>
</list>
</property>
</bean>

5.map注入:map注入比较特殊,因为分为key和value,在map标签下用entry标签将key和value对应注入

<bean id="student" class="pojo.Student">
<property name="card">
<map>
<entry key="身份证" value="12345678"/>
<entry key="学生卡" value="20183333"/>
<entry key="校园卡" value="22222222"/>
</map>
</property>
</bean>

6.set注入:

<bean id="student" class="pojo.Student">
<property name="games">
<set>
<value>英雄联盟</value>
<value>王者荣耀</value>
<value>魔兽世界</value>
</set>
</property>
</bean>

7.null值注入:在property标签下添加null标签即可

<bean id="student" class="pojo.Student">
<property name="wife">
<null/>
</property>
</bean>

8.properties配置文件类型注入:比较特殊用到了prop标签

<bean id="student" class="pojo.Student">
<property name="info">
<props>
<prop key="学号">20186666</prop>
<prop key="姓名">张三</prop>
</props>
</property>
</bean>
xmlns:p="http://www.springframework.org/schema/p"

可以通过在applicationContext内加一个p构造器,就可以在bean标签内替代property标签给普通属性赋值:

<bean id="student" class="pojo.Student" p:card="name"/>