Spring框架中的IOC容器及bean管理

这篇文章讲述的是Spring框架中的IOC容器及bean管理,如有错误或者不当之处,还望各位大神批评指正。

什么是IOC容器?

  • IOC即反转控制,创建对象的权利交给容器来完成,而程序要做的仅仅是使用对象。
  • Spring 容器是 Spring 框架的核心。容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans

Spring中的IOC容器的使用

如何在IOC容器中配置bean?

  • 使用标签来将一个类配置到IOC容器中
<!-- bean标签:用来向IOC容器配置一个bean
    id:用来唯一标识这个bean对象
    class:指明该bean对象的全类名
    property标签:向bean中注入属性
 -->
<bean id="student" class="com.cn.cmc.beans.Student" >
    <property name="name" value="叶清逸"></property>
</bean>

如何获取IOC容器?

  • Spring提供两种IOC容器:
    1. ApplicationContext容器
    2. BeanFactory容器(已废弃)

获取ApplicationContext容器

/**
         * ApplicationContext代表IOC容器
         * ClassPathXmlApplicationContext从类路径下读取配置文件
         * FileSystemXmlApplicationContext从文件系统路径下读取配置文件
         * WebXmlApplicationContext:在web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
         */
        ApplicationContext context = new FileSystemXmlApplicationContext("config/beans.xml") ;

获取BeanFactory容器(已废弃)

/**
 * XmlBeanFactory生成工厂bean 
 * ClassPathResource加载配置文件
 */
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config/beans.xml")) ;

bean管理

从IOC容器中获取bean

方式一:通过id获取bean

/*方式一:通过id获取bean*/
Student student = context.getBean(Student.class);
System.out.println(student.getName());

方式二:通过.class获取bean

/*方式二:通过.class文件获取bean*/
Student s = (Student)context.getBean("student") ;
System.out.println(s.getName());

方式三:通过工厂方法来获取bean

  • 静态工厂方法

方法类

package com.cn.cmc.beans;

import java.util.HashMap;
import java.util.Map;

public class StaticStudentFactory {
    private static Map<String,Student> map ;
    static {
        map = new HashMap<String, Student>();
        map.put("叶清逸", new Student(100001,"叶清逸",'M',25)) ;
        map.put("张三", new Student(100002,"张三",'F',22)) ;
    }
    /*通过静态方法获取bean的实例*/
    static Student getBean(String name){
        return map.get(name) ;
    }
}

xml中的配置:

<!-- 通过静态工厂方法来获取实例
    factory-method:对应工厂中配置的获取bean的方法名
    constructor-arg:对用获取bean方法传入的参数
 -->
<bean id="student1" class="com.cn.cmc.beans.StaticStudentFactory" factory-method="getBean">
    <constructor-arg value="叶清逸"></constructor-arg>
</bean>

bean的属性注入

方式一:属性注入(通过setter方法)

<!-- property标签:向bean中注入属性
    name:属性的唯一标识,对应类中的属性
    value:注入属性的值 
    ref:关联bean的id
 -->
<property name="name" value="叶清逸"></property>

方式二:构造方法注入

<bean id="student" class="com.cn.cmc.beans.Student" >
    <!-- constructor-arg标签:使用构造方法来传入参数
        name:对应传入属性的名
        value:对应传入属性的值
        index:对应传入属性在类中的位置
        type:对应传入属性在类中的类型
        ref:bean之间的引用

        注:index和type主要是为了解决传入时产生歧义的问题,可以不写
            在类中要有对应的构造方法,可以没有setter方法
     -->
    <constructor-arg name="id" value="100001"></constructor-arg>
    <constructor-arg name="name">
        <value>叶清逸</value>
    </constructor-arg>
    <constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
    <constructor-arg name="age" value="25"></constructor-arg>
</bean>

注:若传入值有特殊字符,可以使用<[CDATA[]]包裹起来>

注入的一些其他细节

  • 使用ref属性引用其他bean
<bean id="student" class="com.cn.cmc.beans.Student" >
<constructor-arg name="id" value="100001"></constructor-arg>
<constructor-arg name="name">
    <value>叶清逸</value>
</constructor-arg>
<constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
<!-- ref:引用其他已配置的bean -->
<constructor-arg name="phone" ref="phone"></constructor-arg>
</bean>

<bean id="phone" class="com.cn.cmc.beans.Phone">
    <property name="id" value="200001"></property>
    <property name="name" value="苹果手机"></property>
</bean>
  • 属性内配置内部bean
<bean id="student" class="com.cn.cmc.beans.Student" >
    <constructor-arg name="id" value="100001"></constructor-arg>
    <constructor-arg name="name">
        <value>叶清逸</value>
    </constructor-arg>
    <constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
    <constructor-arg name="age" value="25"></constructor-arg>
    <!-- ref:引用其他已配置的bean -->
    <constructor-arg name="phone">
        <bean class="com.cn.cmc.beans.Phone">
            <property name="id" value="200001"></property>
            <property name="name" value="苹果手机"></property>
        </bean>
    </constructor-arg>
</bean>

注:内部 bean不能被外部引用

  • 配置级联属性
<bean id="student" class="com.cn.cmc.beans.Student" >
    <constructor-arg name="id" value="100001"></constructor-arg>
    <constructor-arg name="name">
        <value>叶清逸</value>
    </constructor-arg>
    <constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
    <constructor-arg name="age" value="25"></constructor-arg>
    <!-- ref:引用其他已配置的bean -->
    <constructor-arg name="phone" ref="phone"></constructor-arg>
    <!-- 配置级联属性(要有对应的bean和setter方法) -->
    <property name="phone.name" value="小米手机"></property>
</bean>

<bean id="phone" class="com.cn.cmc.beans.Phone">
    <property name="id" value="200001"></property>
    <property name="name" value="苹果手机"></property>
</bean>
  • Collection的注入(set,map,list,property)
<property name="phoneSet">
    <!-- 使用<set>标签来注入set -->
    <set>
    <!-- ref标签:表示关联的bean
        bean:已配置的bean的id -->
        <ref bean="phone1"/>
        <ref bean="phone2"/>
        <!-- 也可以注入内部bean -->
        <bean class="com.cn.cmc.beans.Phone">
            <property name="id" value="200003"></property>
            <property name="name" value="华为手机"></property>
        </bean>
    </set>
</property>

<property name="phoneList">
    <!-- 使用<list>标签来注入list -->
    <list>
    <!-- ref标签:表示关联的bean
        bean:已配置的bean的id 
    -->
        <ref bean="phone1"/>
        <ref bean="phone2"/>
        <!-- 也可以注入内部bean -->
        <bean class="com.cn.cmc.beans.Phone">
            <property name="id" value="200003"></property>
            <property name="name" value="华为手机"></property>
        </bean>
        <ref bean="phone1"/>
        <ref bean="phone2"/>
    </list>
</property>
<property name="phoneMap">
    <!-- 使用<map>标签来注入map -->
    <map>
        <!-- entry标签:表示map中的元素
            key:注入的键
            value:注入的值
            key-ref:注入键的关联bean的id
            value-ref:祝福值关联bean的id
         -->
        <entry key="1" value-ref="phone1"></entry>
        <entry key="2" value-ref="phone2"></entry>
        <!-- 也可以使用注入内部类的方式 -->
        <entry key="3">
            <bean class="com.cn.cmc.beans.Phone">
                <property name="id" value="200003"></property>
                <property name="name" value="华为手机"></property>
            </bean>
        </entry>
    </map>
</property>
<property name="phoneProp">
    <!-- 使用prop配置property属性 -->
    <props>
        <prop key="1">cmc</prop>
        <prop key="2">root</prop>
    </props>
</property>
  • 使用util标签配置共享的bean
 <!-- 配置命名空间 -->
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<!-- 使用util标签来配置共享的bean -->
    <util:list id="phonelist">
        <ref bean="phone1"/>
        <ref bean="phone2"/>
    </util:list>
  • 使用p命名空间配置bean
<!-- 使用p命名空间来配置bean -->
    <bean id="student2" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
            p:age="22" p:sex="F"></bean>

bean的自动装配

IOC容器可以自动装配bean,我们要做的仅仅是在bean的autowire属性中配置装配方式:

  • byType 按类型自动装配
  • byName 按名称自动装配
  • byConstructor 按构造器自动装配(不推荐)

byName 按名称自动装配bean

<bean id="student2" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
            p:age="22" p:sex="F" autowire="byName"></bean>

注:通过byName自动装配,属性名必须和bean中定义的属性名一致,否则装配为空

byType 按类型自动装配bean

<bean id="student3" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
            p:age="22" p:sex="F" autowire="byType"></bean>

注:通过byType自动装配,已配置的 bean中只有一个该类型的 bean时会装配成功,有多个时会报错

bean之间的关系(继承,依赖)

bean之间的关系有:

  • 继承:子bean会继承父bean中的属性
  • 依赖:实例化bean时要配置依赖的bean,否则无法实例化该bean

继承与依赖的例子

<!-- 配置bean之间的关系:继承,依赖
    parent:配置继承关系,会继承夫bean的属性
    depend-on:配置依赖关系若没有depend-on中没有对应的bean则该bean不能被实例化
    abstract:指明该bean能不能够被实例化,只能当做模板,此时bean的class属性可以省略
 -->
 <bean id="phone" class="com.cn.cmc.beans.Phone"
        p:id="200001" p:name="苹果手机"></bean>
 <bean id="student" class="com.cn.cmc.beans.Student" 
        p:id="100003" p:name="李四" p:age="24" p:sex="M" abstract="true"></bean>
 <bean id="student1" class="com.cn.cmc.beans.Student"
        parent="student" p:name="王五" p:id="100004" p:phone-ref="phone" 
        depends-on="phone"></bean>

bean的作用域配置

Spring中可以使用scope属性来配置bean的作用域:

  • singleton:单例,在初始化配置文件时生成单例bean对象
  • prototype:原型的,在初始化配置文件时不生成bean对象,使用时返回不同的bean对象
  • request:web环境下每一个request请求都会返回一个不同的bean,只在本次请求中有效
  • session:web环境下每一个request请求都会返回一个不同的bean,在session中有效
<!-- 使用scope属性来配置bean的作用域:
    singleton:单例,在初始化配置文件时生成单例bean对象
    prototype:原型的,在初始化配置文件时不生成bean对象,使用时返回不同的bean对象
    request:web环境下每一个request请求都会返回一个不同的bean,只在本次请求中有效
    session:web环境下每一个request请求都会返回一个不同的bean,在session中有效
 -->
 <bean id="student" class="com.cn.cmc.beans.Student" 
        p:id="100001" p:name="叶清逸" p:age="25" p:sex="M" scope="singleton"></bean>

IOC容器中bean的生命周期

通过init-method和destroy-method

  • 先看例子
<!-- 查看bean的生命周期
    方法一:通过bean标签中的init-method和destroy-method(在bean中要写对应的方法)

 -->
<bean id="student" class="com.cn.cmc.beans.Student" 
        p:id="100001" p:name="叶清逸" p:age="25" p:sex="M" scope="singleton"
        init-method="init" destroy-method="destroy"></bean>
  • 执行结果:
七月 08, 2018 4:21:13 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@11028347: startup date [Sun Jul 08 16:21:13 CST 2018]; root of context hierarchy
七月 08, 2018 4:21:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [C:\Users\Mr_Chuan\workspace\HelloSpring\config\bean-cycle.xml]
bean初始化...
七月 08, 2018 4:21:14 下午 org.springframework.context.support.FileSystemXmlApplicationContext doClose
信息: Closing org.springframework.context.support.FileSystemXmlApplicationContext@11028347: startup date [Sun Jul 08 16:21:13 CST 2018]; root of context hierarchy
bean销毁...

由上例可以看出bean的生命周期为
1. 通过构造方法生成bean的实例
2. 为bean注入属性
3. 调用初始化方法
4. bean的使用
5. IOC容器关闭时,调用销毁方法

通过bean的后置处理器BeanPostProcessor

  • 新建处理器类实现BeanPostProcessor接口
package com.cn.cmc.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * postProcessBeforeInitialization在init-method之后调用
     * 参数arg0表示实例bean本身
     * 参数arg1表示IOC容器中已配置bean的名字
     * 返回值实际上为返回给用户的bean
     */
    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessAfterInitialization"+arg0+" "+arg1);
        return arg0;
    }
    /**
     * postProcessAfterInitialization在init-method之前调用
     */
    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessBeforeInitialization"+arg0+" "+arg1);
        return arg0;
    }
}
  • 在配置文件中配置BeanPostProcessor
<bean class="com.cn.cmc.test.MyBeanPostProcessor"></bean>

由上例可以看出加了后置处理器之后执行流程为:
1. 通过构造方法生成bean的实例
2. 为bean注入属性
3. 将bean传给后置处理器的postProcessBeforeInitialization方法
4. 调用初始化方法
5. 将bean传给后置处理器的postProcessAfterInitialization方法
6. bean的使用
7. IOC容器关闭时,调用销毁方法

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值