Skip to content

maven包

xml
<!--spring核心包-->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>5.0.2.RELEASE</version>
</dependency>

配置文件

创建spring配置文件 applicationContext.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
			
</beans>

让spring管理资源可以使用<bean>标签

xml
<bean id="xxx" class="xxx"></bean>

bean标签:用于配置让 spring 创建对象,并且存入 ioc 容器之中

id属性:对象的唯一标识

class属性:指定要创建对象的全限定类名

init-method:创建之前要执行的方法

destroy-method:关闭容器的时候执行的方法

scope:指定对象的作用范围

* singleton :默认值,单例的

* prototype :多例的

* request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中

* session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中

* global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么

引入

使用ClassPathXmlApplicationContext接口

java
// 使用ApplicationContext接口,就是在获取spring容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 根据类获取对象
XXX xxx = context.getBean(XXX.class);

getBean("xxx"):根据配置文件的ID获取

getBean("xxx",XXX.class):根据配置文件ID获取,后面是返回的类型

getBean(XXX.class):根据类回去实例,但是只能是在单例模式的前提下

依赖注入

注入普通属性

以下内容都是书写在bean标签内

xml
<bean id="" class="">
	……
</bean>

构造器

xml
<constructor-arg name="" value=""/>

constructor-arg:这个就是构造器的意思

name:就是属性名

value:要注入的值

Get/Set

xml
<property name="" value=""/>

property:通着标签使用get/set方法进行注入

name:填写属性名

value:填写要注入的值

除了value之外还有一个叫ref的属性,它用于引用其他bean标签,值写bean标签的id,如:

xml
<!--注入日期-->
<bean id="xxx" class="xxx">
 <property name="xxx" ref="now"/>
</bean>
<bean name="now" class="java.util.Date"/>

自动装配

设置Bean属性autowire,有两种byNamebyType

byName:设置该属性之后会根据配置文件中的id为属性名称的bean进行自动赋值

byType:根据需要的类型去配置文件中寻找相同类型的bean进行赋值

自动装配是基于set方法的

引入外部属性文件

xml
<context:property-placeholder location="classpath:xxx.properties" ignore-unresolvable="true"/>

加载近来配置文件之后可以使用${}的方式进行一个取值

一般在文件设置键的时候不建议直接写键名,因为spring加载配置文件的时候会先加载系统的变量,一般写键的方式为 xxx.xxx

注入特殊属性

数组、list、map、set、properties

注入集合,只要结构相同,标签可互换

数组

xml
<property name="">
    <array>
        <value>xxx</value>
        ……
    </array>
</property>

list

xml
<property name="">
    <list>
        <value>xxx</value>
        ……
    </list>
</property>

set

xml
<property name="">
    <set>
        <value>xxx</value>
        ……
    </set>
</property>

map

xml
<property name="">
    <map>
        <!--第一种-->
        <entry key="" value=""/>
        <!--第二种-->
        <entry key="">
            <value></value>
        </entry>
        <!--第三种-->
        <entry>
            <key></key>
            <value></value>
        </entry>
    </map>
</property>

properties

xml
<property name="">
    <props>
        <prop key=""></prop>
        ……
    </props>
</property>

实例化Bean

三种方式

无参构造

在默认情况下,会使用无参构造来创建类对象,如果Bean没有无参构造方法,将会创建失败

xml
<bean id="" class=""/>

剩余两种:spring静态工厂、spring管理实力工厂

Bean的生命周期

五步骤版

  1. 实例化
  2. 依赖注入
  3. 初始化
  4. 使用Bean
  5. 销毁Bean

七步骤版

  1. 实例化
  2. 依赖注入
  3. 初始化前 — BeanPostProcessor — before()方法
  4. 初始化
  5. 初始化后 — BeanPostProcessor — after()方法
  6. 使用Bean
  7. 销毁Bean

七步骤需要实现 BeanPostProcessor 接口,重写其中的两个方法,完事之后把类交给spring管理

十步骤版

  1. 实例化
  2. 依赖注入
  3. Aware相关接口
  4. 初始化前 — BeanPostProcessor — before()方法
  5. InitializingBean接口
  6. 初始化
  7. 初始化后 — BeanPostProcessor — after()方法
  8. 使用Bean
  9. DisposableBean接口
  10. 销毁Bean

Aware(感知)接口包括:

  • BeanNameAware
  • BeanClassLoaderAware
  • BeanFactoryAware

spring会判断你是否实现了这些接口,如果实现了就在对应位置调用对应方法

注:7步和10步的添加是有区别的,7步的是 重新添加一个bean 这个bean要实现处理器接口 方法会作用与整个配置文件;10步是 在原本的bean基础上实现对应接口,它只作用与单个类创建的bean

spring只对单例Bean进行完整的生命周期管理

多例的bean在客户端获取bean到后spring不再对Bean进行管理