BeanFactory和ApplicationContext的区别
ApplicationContext 立即加载,读取完配置文件后立即创建配置文件中的对象
BeanFactory 延迟加载,什么时候根据id获取对象,什么时候才创建对象

三种创建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"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 把创建对象交给spring管理 -->
<!-- 1.使用bean标签创建 ,使用默认构造函数创建
<bean id="accountServerice" class="com.test.AccountServiceImp"></bean>-->
<!-- 使用普通工厂中的方法创建对象
<bean id="instanceFactory" class="com.test.IntanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory"" factory-method="getAccountService"></bean> -->
<!-- 使用工厂中的静态方法 -->
<bean id="accountService" class="com.test.StaticFactory" factory-method="getAccountService"/>
</beans>
/*** bean的三种创建方式*/
package com.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模拟一个表现层,用户调用业务层* @author Administrator**//*** 获取spring的IOC核心容器* ApplicationContext的三个常用实现类*  ClassPathXmlApplicationContext:可以加载类路径下的配置文件  常用*    FileSystemXmlApplicationContext:加载任意磁盘下的配置文件,必须有访问权限*    AnnotationConfigApplicationContext:用于读取注解创建容器的*/
public class Test{public static void main(String[] args){//1.获取核心容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根据id获取bean对象IAccountService as = (IAccountService)ac.getBean("accountService");System.out.println(as);}
}
/*** 创建bean对象的工厂* Bean 在计算机术语中,含义为可重用组件* JavaBean,用java语言编写的可重用组件*        JavaBean >实体类*       具体到项目中,就是service和dao对象*      第一个:配置一个文件来配置service和dao,配置的内容是唯一标识的权限定内名*        第二个:通过读取配置文件内容,反射创建对象* *      配置文件可以是propertie也可以使xml*/class IntanceFactory{public IAccountService getAccountService(){return new AccountServiceImp();}
}class StaticFactory{public static IAccountService getAccountService(){return new AccountServiceImp();}
}interface IAccountService{/*** 模拟保存账户*/public void saveAccount();
}/*** 业务层实现类*/
class AccountServiceImp implements IAccountService{public void saveAccount(){System.out.println("save a account");}
}

Bean的作用范围

singleton 单例
prototype 多例
request 作用于web应用的request范围
session 作用于web应用的session范围
global 作用于集群环境的会话范围

Bean的生命周期

singleton 容器创建时对象出生
容器在,对象在
容器亡,对象亡

prototype 第一次使用对象出生
,使用对象过程中一直活着,垃圾回收器回收

<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 把创建对象交给spring管理 -->
<!-- 1.使用bean标签创建 ,使用默认构造函数创建 -->
<bean id="accountService" class="com.test.AccountServiceImp"scope="singleton" init-method="init" destroy-method="destroy"></bean>
</beans>
/*** bean生命周期*/
package com.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模拟一个表现层,用户调用业务层* @author Administrator**//*** 获取spring的IOC核心容器* ApplicationContext的三个常用实现类*  ClassPathXmlApplicationContext:可以加载类路径下的配置文件  常用*    FileSystemXmlApplicationContext:加载任意磁盘下的配置文件,必须有访问权限*    AnnotationConfigApplicationContext:用于读取注解创建容器的*/
public class Test{public static void main(String[] args){//1.获取核心容器对象ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根据id获取bean对象IAccountService as = (IAccountService)ac.getBean("accountService");System.out.println(as);as.saveAccount();//手动关闭容器ac.close();}
}interface IAccountService{/*** 模拟保存账户*/public void saveAccount();
}/*** 业务层实现类*/
class AccountServiceImp implements IAccountService{public AccountServiceImp(){System.out.println("accountService对象创建了");}public void init(){System.out.println("对象初始化了");}public void destroy(){System.out.println("对象销毁了");}public void saveAccount(){System.out.println("save a account");}
}

2020-3-24 22:11:24 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Tue Mar 24 22:11:24 CST 2020]; root of context hierarchy 2020-3-24 22:11:24 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [bean.xml] 2020-3-24 22:11:25 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1175422 2020-3-24 22:11:25 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1175422: defining beans [accountService]; root of factory hierarchy accountService对象创建了 对象初始化了 com.test.AccountServiceImp@102799c save a account 2020-3-24 22:11:25 org.springframework.context.support.AbstractApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Tue Mar 24 22:11:24 CST 2020]; root of context hierarchy 2020-3-24 22:11:25 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1175422: defining beans [accountService]; root of factory hierarchy 对象销毁了

spring5.0学习笔记3相关推荐

  1. mysql5.0镜像_Mysql5.0学习笔记(一)

    Mysql5.0学习笔记(一) -基本sql语句与支持字符集 1.登录 mysql -h localhost -u root 2.创建用户firstdb(密码firstdb)和数据库,并赋予权限于fi ...

  2. Zabbx6.0(学习笔记)

    Zabbx6.0(学习笔记) 目录导航 Zabbx6.0(学习笔记) 一.为什么 需要监控系统 二.如何选择监控 三.Zabbix概述 四.Zabbix安装哪个版本? Zabbix安装要求 1.硬件 ...

  3. flink1.12.0学习笔记第2篇-流批一体API

    flink1.12.0学习笔记第 2 篇-流批一体API flink1.12.0学习笔记第1篇-部署与入门 flink1.12.0学习笔记第2篇-流批一体API flink1.12.0学习笔记第3篇- ...

  4. CCC3.0学习笔记_认证和隐私保护

    CCC3.0学习笔记_Authentication and Privacy Keys 系列文章目录 文章目录 系列文章目录 前言 1. 手机端和车厂服务器端的密钥存储 2. 密钥的产生和使用的说明 3 ...

  5. TensorFlow2.0 学习笔记(三):卷积神经网络(CNN)

    欢迎关注WX公众号:[程序员管小亮] 专栏--TensorFlow学习笔记 文章目录 欢迎关注WX公众号:[程序员管小亮] 专栏--TensorFlow学习笔记 一.神经网络的基本单位:神经元 二.卷 ...

  6. 《TP5.0学习笔记---配置篇》

    TP5.0学习笔记 TP5目录结构介绍 application目录是应用目录,我们整个应用所有的内容都写在这个目录中,在后续开发中,我们更多的时候都是在编写这个目录中的文件.在它里边有一个index文 ...

  7. Tensorflow2.0学习笔记(一)

    Tensorflow2.0学习笔记(一)--MNIST入门 文章目录 Tensorflow2.0学习笔记(一)--MNIST入门 前言 一.MNIST是什么? 二.实现步骤及代码 1.引入库 2.下载 ...

  8. Tensorflow2.0学习笔记(二)

    Tensorflow2.0学习笔记(二)--Keras练习 文章目录 Tensorflow2.0学习笔记(二)--Keras练习 前言 二.使用步骤 1.实现步骤及代码 2.下载 Fashion MN ...

  9. CCC3.0学习笔记_数字密钥数据结构

    CCC3.0学习笔记_数字密钥数据结构 系列文章目录 文章目录 系列文章目录 前言 4.1 Applet Instance Layout 4.2 Digital Key Structure 4.2.1 ...

  10. Tensorflow2.0学习笔记(一)北大曹健老师教学视频1-4讲

    Tensorflow2.0学习笔记(一)北大曹健老师教学视频1-4讲 返回目录 这个笔记现在是主要根据北京大学曹健老师的视频写的,这个视频超级棒,非常推荐. 第一讲 常用函数的使用(包含了很多琐碎的函 ...

最新文章

  1. redis常用优化及持久化到硬盘
  2. Brocade光纤交换机FOS升级
  3. 看到腾讯反驳360的文章,笑死我了。
  4. java面向对象-------类属性和方法,不同类之间调用
  5. 在子线程中更改主线程中的控件的信息,在子线程中用toast
  6. 苹果 macOS 再曝漏洞,输任意密码可进入 App Store 首选项
  7. 初中计算机ps教程,初中信息技术《认识Photoshop CS2的工作界面》教案
  8. mysql添加表字段脚本_mysql数据库修改字段及新增字段脚本
  9. WML语言基础(WAP建站)三
  10. Flutter 淡入淡出与逐渐出现动画
  11. 嵌入式linux开发,对pcf8563时钟操作报错:rtc-pcf8563 0-0051: low voltage detected, date/time is not reliable.
  12. java生成excel表格
  13. Excel表格之——某一列生成UUID
  14. 统计学学习日记:L5-离散趋势分析之异众比率与四分位差
  15. 办公软件操作(小技巧1)
  16. 软件测试入门第一步:编写测试报告
  17. Word安装Math Type后ctrl+V不能使用解决方法
  18. 277、H3C无线组网内部培训培训资料
  19. big mac sur 免驱显卡_解决了升级macOS big sur后外置显卡坞失效识别不了的情况
  20. python 可视化界面学习简易教程

热门文章

  1. java 通过System.getProperties()获取系统参数
  2. Windows之远程访问×××的简单部署
  3. Fast DDS Fast DDS主要包括以下内容DDS API、Fast DDS-Gen、RTPS Wire Protocol
  4. 4月23 nuTonomy的语义层(人行横道,人行道,交通信号灯,停车线,车道等)的扩展包
  5. 命名实体识别研究综述
  6. 论如何优雅的处理回文串 - 回文自动机详解.
  7. [ROS]1 小乌龟
  8. ftp和http转参数的使用(转)
  9. 第四届中国云计算大会——123
  10. windows server 2008 NLB 集群