文章目录
Spring IoC 容器组件,可以完成对象的创建、对象属性赋值、对象管理.
1. 框架部署
1.1 创建一个空的Maven项目
1.2 添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
1.3 自定义模板
自定义一个名为 spring-ioc-xml
的文件模板,后缀为 xml
,文件内容如下
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 对于一个xml文件如果作为框架的配置文件,需要遵守框架的配置规则 -->
<!-- 通常一个框架为了让开发者能够正确的配置,都会提供xml的规范文件(dtd\xsd) -->
</beans>
1.4 创建Spring配置文件
通过配置文件"告诉"Spring容器创建什么对象,给对象属性赋什么值。
在 resource
目录下用 spring-ioc-xml
模板创建一个名为 applicationContext.xml
的配置文件。
2. SpringIoC使用
使用 SpringIoC组件创建并管理对象。
2.1 创建实体类
- 添加
lombok
依赖,以便快速生成构造方法和getter,setter
方法。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
- 创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
private String stuNum;
private String stuName;
private String stuGender;
private int stuAge;
private Date enterenceTime; //入学日期
}
2.2 配置实体类
在 applicationContext.xml
中对实体类配置如下:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 对于一个xml文件如果作为框架的配置文件,需要遵守框架的配置规则 -->
<!-- 通常一个框架为了让开发者能够正确的配置,都会提供xml的规范文件(dtd\xsd) -->
<!--通过bean将实体类配置给Spring进行管理,id表示实体类的唯一表示-->
<bean id="stu" class="com.mr.bean.Student">
<property name="stuNum" value="10002"/>
<property name="stuName" value="李斯"/>
<property name="stuGender" value="女"/>
<property name="stuAge" value="20"/>
</bean>
</beans>
2.3 测试创建实体对象
新建一个 Test
目录,在目录下建立 Test1.java
来进行测试。
public class Test1 {
public static void main(String[] args) {
//1.初始化Spring容器,加载Spring配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.通过Spring容器获取Student对象
Student student1 = (Student) context.getBean("stu");
System.out.println(student1);
}
}
输出如下所示:
而之所以 enterenceTime
属性是 null
,是因为我们并未对其进行赋值。
3. Ioc和DI
3.1 Ioc控制反转
当我们需要通过Spring对象工厂创建某个类的对象时候,需要将这个交给Spring管理——通过bean标签配置,即下面的代码:
<bean id="stu" class="com.mr.bean.Student"></bean>
3.2 DI依赖注入
通过Spring容器给创建的对象属性赋值,如下
<bean id="stu" class="com.mr.bean.Student" autowire="byName">
<property name="stuNum" value="10001"/>
</bean>
3.3 DI注入的方式,set注入
DI
的注入方式有三种,set
方法注入、构造器注入、接口注入,在这里我介绍一下 set
方法的注入。
- 简单类型和字符串类型
直接使用property
标签, 使用property
标签的过程相当于就是调用对应属性的set
方法。
<bean id="stu" class="com.mr.bean.Student" autowire="byName">
<property name="stuNum" value="10001"/>
</bean>
- 日期类型和自定义对象类型
对日期和自定义的对象赋值时,需要使用ref
指定注入的依赖的id
。
上面的对象赋值时并未对日期进行赋值,在这里我们将其补上。
<bean id="stu" class="com.mr.bean.Student">
<property name="enterenceTime" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"></bean>
- List类型
List
中是简单类型时,如下所示:
<!--方式一-->
<property name="hobbies" value="旅游,电影"/>
<!--方式二-->
<property name="hobbies" >
<list>
<value>旅游</value>
<value>电影</value>
<value>Java</value>
</list>
</property>
List
中是对象类型时时,如下所示:
<property name="hobbies" >
<list>
<ref bean="book"></ref> <!--引用容器中的备案-->
<ref bean="book"></ref>
</list>
</property>
- Map类型
<property name="maps">
<map>
<entry>
<key>
<value>k1</value>
</key>
<value>123</value>
</entry>
<entry>
<key>
<value>k2</value>
</key>
<value>456</value>
</entry>
</map>
</property>
- Properties类型
Properties
也是以key-value
形式存在的,但是其键必须是String
类型。
<property name="properties">
<props>
<prop key="k1">aaa</prop>
<prop key="k2">bbb</prop>
</props>
</property>
4. Bean作用域
4.1 单例、多例模式
在bean标签可以通过scope属性指定对象的的作用域
scope="singleton"
表示当前bean是单例模式(默认饿汉模式,Spring容器初始化阶段就会完成此对象的创建)
<bean id="book" class="com.mr.bean.Book" scope="singleton"></bean>
scope="prototype"
表示当前bean为非单例模式,每次通过Spring容器获取此bean的对象时都会创建一个新的对象
<bean id="book" class="com.mr.bean.Book" scope="prototype"></bean>
4.2 懒加载
当在bean标签中设置 lazy-init="true"
变为懒加载模式,即使用的时候才会创建对象,不使用即不创建,在很多时候能够很好的提升运行速度。
4.3 Bean的声明周期方法
在 bean
标签中通过 init-method
属性指定当前 bean
的初始化方法,初始化方法在构造器执行之后执行,通过 destroy-method
属性指定当前 bean
的销毁方法,销毁方法在对象销毁之前执行。
-
Bean类
public class Book { private int bookId; private String bookName; //初始化方法:在创建当前类对象时调用的方法,进行一些资源准备工作 public void init(){ System.out.println("-------init"); } //销毁方法:在Spring容器销毁对象时调用此方法,进行一些资源回收性的操作 public void destory(){ System.out.println("-------destory"); } }
-
Spring配置文件
<bean id="book" class="com.qfedu.ioc.bean.Book" scope="prototype"init-method="init" destroy-method="destory" ></bean>
4.4 自动装配
自动装配:Spring在实例化当前bean的时候从Spring容器中找到匹配的实例赋值给当前bean的属性。
自动装配策略有两种:
byName
根据当前Bean
的属性名在Spring
容器中寻找匹配的对象 ,如果根据name
找打了bean
但是类型不匹配则抛出异常
<bean id="stu" class="com.mr.bean.Student" autowire="byName"></bean>
byType
根据当前Bean
的属性类型在Spring
容器中寻找匹配的对象,如果根据类型找到了多个bean
也会抛出异常
<bean id="stu" class="com.mr.bean.Student" autowire="byType"></bean>