一、Spring IoC的概念
Ioc指的是控制反转。由开发人员采用 “new 被调用者” 的方式来创建对象变成由Spring容器创建对象,这就叫做控制反转。实现控制反转的就是Spring Ioc容器。
二、Spring Ioc容器
Spring IoC容器有两个接口:BeanFactory接口和ApplicationContext接口。实际开发中,一般使用第二个接口。
SpringIoc的应用过程:
1.创建实体类
2.在src目录下创建Spring的配置文件applicationContext.xml
<bean>
:该标签让spring容器创建对象- id:表示spring创建的对象在spring容器中的唯一标识。
- class:要创建的对象的类的权限名。
<?xml version="1.0" encoding="UTF-8"?>
<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="stu" class="com.spring.ioc.Student"></bean>
</beans>
3.创建容器解析配置文件
public static void main(String[] args) {
//1.创建spring容器
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.通过容器获取实例
Student stu = (Student)app.getBean("stu");
//3.可用实例去调用该实例的方法.....
三、依赖注入(DI)
概念:IoC容器的具体实现方法,用于给Sring容器创建的对象的属性赋值,还可以实现对象之间的依赖关系。
实现方式:使用构造方法注入和使用属性的set方法注入
创建实体类:
@Data
public class Husband {
private String name;
private int age;
private double salary;
private Date date;
private Wife wife;
public Husband(String name, int age,Wife wife) {
super();
this.name = name;
this.age = age;
this.wife = wife;
}
}
1.使用set方法注入
<property>
:该标签用于设置一个属性- name:表示属性名
- value:表示属性值
- ref:需要的数据为bean类型,用ref。如一个对象或一个类。ref的值是对象的id值或类名。
<?xml version="1.0" encoding="UTF-8"?>
<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="husband" class="com.spring.ioc.Husband">
<property name="name" value="Lucas"></property>
<property name="age" value="33"></property>
<property name="salary" value="3000.5"></property>
<property name="date" ref="now"></property>
<property name="wife" ref="w"></property>
</bean>
<bean id="w" class="com.spring.ioc.Wife">
<property name="name" value="Lucy"></property>
<property name="age" value="33"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>
</beans>
2.使用构造方法注入
<constructor-arg>
:该标签用于指定构造方法中的参数- name:参数在构造函数中的名字
- value:参数的值
- index:参数在构造函数中的索引位置,从0开始
<bean id="husband" class="com.spring.ioc.Husband">
<constructor-arg name="name" value="Lucas"></constructor-arg>
<constructor-arg name="age" value="33"></constructor-arg>
<constructor-arg name="wife" ref="w"> </constructor-arg>
</bean>
<bean id="w" class="com.spring.ioc.Wife">
<constructor-arg name="name" value="Lucy" index="0"></constructor-arg>
<constructor-arg name="age" value="33" index="1"></constructor-arg>
</bean>
四.创建Spring容器的方式:
1.ClassPathXmlApplicationContext:从类的根路径下加载配置文件。使用的是配置文件用该类创建spring容器
ApplicationContext app= new ClassPathXmlApplicationContext(“applicationContext.xml”);
2. AnnotationConfigApplicationContext:用注解配置容器对象时,需要使用此类来创建spring容器。使用的是配置类,用该类创建spring容器
ApplicationContext app = new AnnotationConfigApplicationContext(Config.class);