在Spring里面分为构造器注入和set方式注入
也就是把bean的属性完善一下
我们要强调一下,DI只是IOC的一种方式,IOC不一定要通过Spring里面的DI实现,也可以通过非Spring的写法实现,比如说让用户自己把实现好的类丢进去之类的
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,都由容器来注入
不管你用不用配置里面的bean,它总是存在那里,你用的话也是从里面取(通过反射)
复杂对象如下:
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
package com.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> game;
private Properties infor;
private String wife;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", game=" + game +
", infor=" + infor +
", wife='" + wife + '\'' +
'}';
}
}
包括ref注入,数组[]注入,list注入, map注入, set注入, null注入, Property注入
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.pojo.Address">
<property name="address" value="xian">property>
bean>
<bean id="student" class="com.pojo.Student">
<property name="name" value="hou"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>三国value>
<value>西游value>
<value>水浒value>
array>
property>
<property name="hobbies">
<list>
<value>eatvalue>
<value>drinkvalue>
<value>playvalue>
list>
property>
<property name="card">
<map>
<entry key="1" value="12"/>
<entry key="2" value="23"/>
map>
property>
<property name="game">
<set>
<value>wangzhevalue>
<value>daotavalue>
<value>lolvalue>
set>
property>
<property name="wife">
<null>null>
property>
<property name="infor">
<props>
<prop key="id">20200405prop>
<prop key="name">hdkprop>
props>
property>
bean>
beans>
pojo:
package com.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
使用p标签和c标签要在头文件引用好
p注入就是set注入,c注入就是构造器注入
<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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="use" class="com.pojo.User" p:name="dong" p:age="10">
bean>
<bean id="use2" class="com.pojo.User" c:name="kun" c:age="19">bean>
beans>

1.单例模式(默认):一直都是同一个对象(不适用于某些并发场景)
<bean id="use2" class="com.pojo.User" c:name="kun" c:age="19" scope="singleton">bean>
2.原型模式: 每次从容器中get的时候,都产生一个新对象!
<bean id="use2" class="com.pojo.User" c:name="kun" c:age="19" scope="prototype">bean>
3.其余的request、session、application这些只能在web开放中使用!
在总结完bean的注入方式后,这里又总结了不同类型数据结构的注入
后面的话继续总结自动装配,还有autowird注解的使用,这些才是重点
因为springboot中全是注解