Bean容器初始化

  • 基础

    • org.springframework.beans
    • org.springframework.context
    • BeanFactory提供配置结构和基本功能,加载并初始化Bean
    • ApplicationContext保存了Bean对象,并且在Spring进行使用
  • ApplicationContext范围
    • 加载本地文件

      FileSystemXmlApplication context = new FileSystemXmlApplicationContext("F://test//context.xml");
    • 加载ClassPath文件

      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*/resources/context.xml");
    • web应用配置文件
  • Spring注入方式
    • 定义:Spring注入是指启动Spring容器启动并加载Bean的过程中,完成对变量的赋值行为
    • 注入方式:
      • 设值注入:是将变量以Set属性的方式注入到Bean中,使用设置注入,在Bean中一定要有变量的Set方法,配置中的<property>标签name的值一定要和属性名称一致!!!

        <?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" > <bean id="InjectionServiceImpl" class="com.jing.spring.bean.InjectionServiceImpl">
        <property name="injectionDao" ref="injectionDao"></property>
        </bean>
        <bean id="injectionDao" class="com.jing.spring.bean.InjectionDaoImpl"></bean><!--??????不懂 程序中是引用接口 为什么却要把实现类注入?-->
        </beans>
        private InjectionDao injectionDao;
        
            public void setInjectionDao(InjectionDao injectionDao) {
        this.injectionDao = injectionDao;
        } public void save(String arg) {
        System.out.print("InjectionServiceImpl中的arg==="+arg+"\n");
        //逻辑处理
        arg=arg+":"+this.hashCode();
        injectionDao.save(arg);
        }
      • 构造注入:是将变量作为Bean构造器的参数传入,在Bean中一个要有一个以变量作为参数的构造函数,配置中的<constructor-arg>标签name的值一定要和有参构造方法参数名称一致!!!
        <?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" > <bean id="InjectionServiceImpl" class="com.jing.spring.bean.InjectionServiceImpl">
        <constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg>
        </bean> <bean id="injectionDao" class="com.jing.spring.bean.InjectionDaoImpl"></bean>
        </beans>
        private InjectionDao injectionDaoss;
        
            InjectionServiceImpl(InjectionDao injectionDao){
        this.injectionDaoss=injectionDao;
        }
        public void save(String arg) {
        System.out.print("InjectionServiceImpl中的arg==="+arg+"\n");
        //逻辑处理
        arg=arg+":"+this.hashCode();
        injectionDaoss.save(arg);
        }

Bean的配置项

  • Id
  • Class
  • Scope
  • Contructor arguments
  • Properties
  • Autowiring mode
  • lazy_initialization mode
  • Initialization/destruction mothod

Bean的作用域

  • singleton:单例。一个Bean容器中只存在一个Bean对象。
  • prototype:每次请求创建实例都会产生一个新的实例,destroy不生效。
  • request:每次请求创建一个新的实例且在当前request内有效。
  • session:同上,每次http请求创建一个新的实例,且在当前session有效。
  • global session:基于portlet的web中有效(portlet定义了 global session)。如果是在web中,同session。使用在多个系统之间,一个session控制不了多个系统。

Bean的生命周期

  • 定义:XML文件中<bean>的定义
  • 初始化:获取一个实例的时候
  • 使用:使用
  • 销毁:销毁
     
  • 初始化/销毁方法:这两个方法是为了 在初始化/销毁Bean容器的时候执行一些额外的方法
    • 针对Bean实例的销毁,只有在Bean是单例singleton模式的前提下,Bean才会随着Bean容器的销毁而销毁。在原型prototype的模式下,Bean实例在创建后,就会交由用户进行管理,所以此时Bean实例的销毁不是由Bean容器决定。举例:在原型模式中,根据电影票类初始化两个实例,也就是两张电影票,这个时候,不能因为一张电影票的作废,另一张电影票也随之作废。电影票的管理应该交由用户来管理。
    • 方式一:XML中每一个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" > <bean id="springLifeDate" class="com.jing.spring.lifedate.SpringLifeDate" init-method="start" destroy-method="end"></bean> </beans>
      package com.jing.spring.lifedate;
      
      public class SpringLifeDate1 {
      
          public void start(){
      System.out.println("SpringLifeDate开始执行。。。。");
      }
      public void excecute(){
      System.out.println("SpringLifeDate执行中。。。。");
      }
      public void end(){
      System.out.println("SpringLifeDate执行结束。。。。");
      }
      }
       

      PS:这种方法针对每个Bean设置的初始化/销毁时执行的方法。在Bean容器初始化时主动执行init-method设置的类中的方法,Bean容器销毁后执行destroy-method设置的方法。如果在XML中进行方法设置,Bean类中创造方法,会报错。

    • 方式二:在Bean的类中实现接口,实现初始化方法/销毁方法
      package com.jing.spring.lifedate;
      
      import org.springframework.beans.factory.DisposableBean;
      import org.springframework.beans.factory.InitializingBean; public class SpringLifeDate2 implements InitializingBean, DisposableBean { public void afterPropertiesSet() throws Exception {
      System.out.println("SpringLifeDate开始执行。。。。");
      }
      public void excecute(){
      System.out.println("SpringLifeDate执行中。。。。");
      }
      public void destroy() throws Exception {
      System.out.println("SpringLifeDate执行结束。。。。");
      }
      }

      PS:这种方法是Bean中实现接口实现Bean初始化/销毁时执行的方法。Bean初始化后执行的方法需要实现InitializingBean接口重写afterPropertiesSet方法,Bean销毁后执行的方法需要实现DisposableBean接口重写destroy方法。

    • 方式三:在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"
      default-init-method="defaultInit" default-destroy-method="defaultDestroy" >
      </beans>
      package com.jing.spring.lifedate;
      
      public class SpringLifeDate3 {
      public void defaultInit() throws Exception {
      System.out.println("SpringLifeDate3开始执行。。。。");
      }
      public void excecute(){
      System.out.println("SpringLifeDate执行中。。。。");
      }
      public void defaultDestroy() throws Exception {
      System.out.println("SpringLifeDate3执行结束。。。。");
      }
      }

      PS:这种方式生命Bean的初始化/销毁方法,在类中没有相对应得方法,也不会报错。

    • 执行顺序
      • 在三种方式同时存在的情况下:只会执行方式一(每一个Bean实例声明)和方式二(实现类方式)中定义的初始化/销毁方法,执行顺序为方式二(实现类方式)>方式一(每一个Bean实例声明)
      • 在方式三(全局配置)和其他任一方式同时存在,方式三(全局配置)都不会执行,只会执行另一种方式中定义的初始化/销毁方法。
  • Next

Spring 学习——Bean容器的更多相关文章

  1. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  2. Spring学习-Bean的基本概念知识

    4月份开始复习一遍spring相关知识.让自己巩固一下spring大法的深奥益处,所以就看了大佬的博客,转载留下来日后继续研读.认为重点的标记为红色 转载自:http://www.cnblogs.co ...

  3. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  4. 【转】Spring学习---SpringIOC容器的初始化过程

    [原文]https://www.toutiao.com/i6594400249429623304/ SpringIOC容器的初始化过程 简单来说,IoC容器的初始化是由refresh()方法来启动的, ...

  5. Spring学习-- Bean 的作用域

    Bean 的作用域: 在 Spring 中 , 可以在 <bean> 元素的 scope 属性里设置 bean 的作用域. 默认情况下 , Spring 只为每个在 IOC 容器里声明的 ...

  6. Spring学习--Bean 之间的关系

    Bean 之间的关系:继承.依赖. Bean 继承: Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean. 子 ...

  7. spring的bean容器加载

    1.在单独使用的时候可以通过ClassPathXmlApplicationContext(“配置文件.xml”);来启动容器. 2.在MVC下是通过启动servlet容器,初始化DispatcherS ...

  8. 黑马Spring学习 bean

  9. spring学习(二)---依赖注入

    spring第二个特性是依赖注入. 学习依赖注入,首先应该明白两个问题:1,谁依赖谁:2,谁注入,注入什么? 首先还是看代码: 还是这个bean: package testSpring.busines ...

随机推荐

  1. SpringMVC.入门篇《二》form表单

    SpringMVC.入门篇<二>form表单 项目工程结构: 在<springmvc入门篇一.HelloWorld>基础上继续添加代码,新增:FormController.ja ...

  2. MYSQL5.6.X 非在线安装版(解压版)安装过程

    一.卸载以前旧版本(本人5.5版本) 1.关闭MySQL服务 以管理员身份运行cmd,执行以下命令: net stop mysql 或者右键我的电脑,在管理——服务——停止MySQL 2.卸载MySQ ...

  3. Day7 错误和异常

    一.异常 1.异常基础 1.为了让我们的代码在出现异常的时候,整个项目依然是可以正常运行的,所以我们引入了异常处理机制! 2.在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户 ...

  4. Mysql常规优化

    一.SQL语句优化 (1)使用limit对查询结果的记录进行限定(2)避免select *,将需要查找的字段列出来(3)使用连接(join)来代替子查询(4)拆分大的delete或insert语句 二 ...

  5. 多线程实现Thread.Start()与ThreadPool.QueueUserWorkItem两种方式对比

    Thread.Start(),ThreadPool.QueueUserWorkItem都是在实现多线程并行编程时常用的方法.两种方式有何异同点,而又该如何取舍? 写一个Demo,分别用两种方式实现.观 ...

  6. 【Hadoop学习之七】Hadoop YARN

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4       jdk8       hadoop-3.1.1 YARN: ...

  7. python 测试文件或者文件目录是否存在 测试文件类型,获取文件大小,获取修改日期

    ----测试一个文件或目录是否存在 >>> import os >>> os.path.exists('/etc/passwd') True >>> ...

  8. Presto 学习

    Presto 基础知识与概念学习可以参考这些博客: presto 0.166概述 https://www.cnblogs.com/sorco/p/7060166.html Presto学习-prest ...

  9. 禁用HTTP.sys,导致80端口被禁用和IIS服务无法启动解决办法

    由于端口被占用,使用 win+r 运行 cmd 输入netstat -ano 可以看到端口被PIF 4占用,可以找到对应的进程NT kernel& System 该进程是Http.sys,是h ...

  10. 以太坊ETH中智能合约消耗的Gas、Gas Limit是什么?

    以太坊ETH中智能合约消耗的Gas.Gas Limit是什么? 数字货币交易一般是有交易费的,比特币(BTC)的交易费很容易理解,就是直接支付一定额度的BTC作为手续费.而以太坊(ETH)的交易费表面 ...