Spring01_ioc之bean的创建和生命周期

1 首先,新建一个maven项目, 在pom.xml中导入spring相关

<packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
    </dependencies>

2 在resources下面新建bean.xml文件, 配置spring的bean容器. resources即为项目类的根路径

<?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">

    <!--第一种方法: 使用默认的构造函数.
        在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他标签时,使用默认的构造函数创建
        (重写有参构造方法之后,一定要重写无参构造)
        <bean id="accountService" class="com.hr.service.impl.AccountServiceImpl"/>
    -->

    <!--第二种方法(在jar包中的类的某个方法): 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入容器中)-->
    <!--<bean id="instanceFactory" class="com.hr.factory.InstanceFactory"/>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"/>-->

    <!--第三种方法(在jar包中的类的某个方法): 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入容器中)-->
    <!--<bean id="accountService" class="com.hr.factory.StaticFactory" factory-method="getAccountService"/>-->

    <!--
    bean对象的生命周期:
        单例对象:
            出生: 当容器创建时对象出生
            活着: 只要容器还在,对象一直活着
            死亡: 容器销毁,对象消亡
            死亡: 单例对象的生命周期和容器相同
        多例对象:
            出生: 当我们get对象的时候(使用对象)时spring框架为我们创建
            活着: 对象只要是在使用过程中就一直活着
            死亡: 当对象长时间不用,且没有别的对象引用时候,由Java的垃圾回收器回收

    -->

    <bean id="accountService" class="com.hr.service.impl.AccountServiceImpl" scope="prototype"
        init-method="init" destroy-method="destory"
    />

</beans>

AccountServiceImpl实体类: 

public class AccountServiceImpl implements IAccountService {


    public AccountServiceImpl() {
        System.out.println("AccountServiceImpl创建了");
    }

    public void saveAccount() {
        System.out.println("service中的saveAccount执行了 ");
    }

    private void init() {
        System.out.println("对象初始化了");
    }

    private void destory() {
        System.out.println("对象销毁了");
    }
}

测试类: 

package com.hr;

import com.hr.dao.impl.AccountDaoImpl;
import com.hr.service.IAccountService;
import com.hr.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Client {

    public static void main(String[] args) {
//        testIoc01();
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) applicationContext.getBean("accountService");
        //手动关闭容器
        applicationContext.close();

    }

    private static void testIoc01() {
        //        /*-------------ApplicationContext-------------*/
//        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//        AccountServiceImpl accountService = (AccountServiceImpl) ac.getBean("accountService");
//        AccountDaoImpl accountDao = (AccountDaoImpl) ac.getBean("accountDao");
//        System.out.println(accountService);
//        System.out.println(accountDao);


        //        /*-------------BeanFactory-------------*/
        Resource resource = new ClassPathResource("bean.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        AccountServiceImpl accountService = (AccountServiceImpl) factory.getBean("accountService");
        System.out.println(accountService);
    }
}

笔记: 

两个容器:
    ApplicationContext: 顶级父类是BeanFactory, 但是创建对象的策略是立即加载的方式,只要一读完配置文件就马上创建配置文件中
                        的对象 (单例对象适用: Servlet, Service等)
    BeanFactory: :延迟加载, 什么时候根据id获取对象了, 什么时候才真正的创建对象 (多例对象适用)

Spring对bean的管理细节:
    1 创建bean的三种方式
        第一种方法: 使用默认构造函数创建
        第二种方法: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入容器中)
        第二种方法: 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入容器中)
    2 bean对象的作用范围:
        默认是单例的, bean标签的scope可以调整
        scope取值:
            singleton: 单例的(默认值)
            prototype: 多例的
            request: 作用于web应用的请求范围
            session: 作用于web应用的会话范围
            global-session: 作用于集群环境的会话范围(全局会话范围), 当不是集群环境时,它就是session
    3 bean对象的生命周期:
              单例对象:
                  出生: 当容器创建时对象出生
                  活着: 只要容器还在,对象一直活着
                  死亡: 容器销毁,对象消亡
                  死亡: 单例对象的生命周期和容器相同
              多例对象:
                  出生: 当我们get对象的时候(使用对象)时spring框架为我们创建
                  活着: 对象只要是在使用过程中就一直活着
                  死亡: 当对象长时间不用,且没有别的对象引用时候,由Java的垃圾回收器回收
        

代码托管地址: 

https://github.com/2402zmybie/spring01

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章