• spring中bean的细节


    配置文件:

    <?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来管理-->
    <!--id:获取是的唯一标志,class:反射要创建的全限定类名-->
    <!--spring对bean的管理细节
    1. 创建bean的三种方式
    2. bean对象的作用范围
    3. bean对象的生命周期
    -->
    <!--创建bean的三种方式-->
    <!--第一种方式:使用默认构造函数创建。
    在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时.
    采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
    -->
    <!--第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
    instanceFactory为工厂对象,accountService使用工厂对象instanceFactory(factory-bean="instanceFactory")
    中的getAccountService方法创建对象(factory-method="getAccountService")
    <bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
    -->

    <!--第三种方法:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)-->

    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>



    </beans>

    第二种方法对应的类:
    package com.itheima.factory;

    /*
    * 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
    *
    * */

    import com.itheima.service.IAccountService;
    import com.itheima.service.impl.AccountServiceImpl;

    public class InstanceFactory {
    public IAccountService getAccountService() {
    return new AccountServiceImpl();
    }
    }

    第三种方法对应的类:
    package com.itheima.factory;

    /*
    * 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
    *
    * */

    import com.itheima.service.IAccountService;
    import com.itheima.service.impl.AccountServiceImpl;

    public class StaticFactory {
    public static IAccountService getAccountService() {
    return new AccountServiceImpl();
    }
    }

    <!--bean的作用范围调整
    bean标签的scope属性:
    作用:用于指定bean的作用范围
    取值:
    singleton:单例的(默认值)
    prototype:多例的
    request:作用于web应用的请求范围
    session:作用于web应用的会话范围
    global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton"></bean>

    -->

    <!--bean对象的生命周期
    单例对象
    出生:当容器创建时对象出生
    活着:只要容器还在对象一直活着
    死亡:容器销毁,对象消亡
    总结:单例对象的生命周期和容器相同
    多例对象
    出生:当我们使用对象时spring框架为我们创建
    活着:对象只要在使用过程就一直活着
    死亡:当对象长时间不用,且没有别的对象引用时,由java的垃圾回收器回收
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
    scope="singleton" init-method="init" destroy-method="destory"></bean>
     




  • 相关阅读:
    SpringBoot-配置Druid-yml方式
    CentOS7下配置Nginx并实现简单的负载均衡
    用私有构造器或者枚举类型强化Singleton
    virtualenv虚拟环境的使用
    Windows平台安装Python
    window平台基于influxdb + grafana + jmeter 搭建性能测试实时监控平台
    easy-mock本地部署成功,访问报错:EADDRNOTAVAIL 0.0.0.0:7300 解决方案
    npm install 报错: WARN checkPermissions Missing write access to 解决方案
    npm install 报错:ERR! code EINTEGRITY 解决方案
    最新版chrome浏览器如何离线安装crx插件?(转载)
  • 原文地址:https://www.cnblogs.com/kingchen/p/12961267.html
Copyright © 2020-2023  润新知