之前都是说了怎么配置bean以及用法之类的,这篇博文来介绍下spring容器内幕。

内部容器工作机制

Spring中AbstractApplicationContext抽象类的refresh()方法是用来刷新Spring的应用上下文的。

@Override
public void refresh() throws BeansException, IllegalStateException {
//初始化BeanFactory
prepareBeanFactory(beanFactory);
//调用工厂后处理器
invokeBeanFactoryPostProcessors(beanFactory);
//注册bean后处理器
registerBeanPostProcessors(beanFactory);
//初始化消息源
initMessageSource();
//初始化应用上下文事件广播器
initApplicationEventMulticaster();
//初始化其他特殊的bean,由具体子类实现
onRefresh();
//注册事件监听器
registerListeners();
//初始化所有单实例的bean,使用懒加载模式的bean除外
finishBeanFactoryInitialization(beanFactory);
//完成刷新并发布容器刷新事件
finishRefresh();
}

直接看图解吧:

使用外部属性文件

项目中经常有很多配置,例如数据源相关数据、日志相关数据,按照习惯,大部分喜欢配置在properties文件中,那么在项目中如何使用这些属性呢?

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round;

jdbc.username=root

jdbc.password=123456

applicationContext.xml

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:com/ty/jdbc.properties"/> <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <property name="driverClassName"value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}"/> <property name="password"value="${jdbc.password}" /> </bean>

不过这种写法略显麻烦,看下另外一种写法:

<context:property-placeholder location="classpath:com/ty/jdbc.properties""/>
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <property name="driverClassName"value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}"/> <property name="password"value="${jdbc.password}" /> </bean>

注:location中的路径用“/”分隔。

不过这种也不是很方便,来看下最推荐的写法:

@Component
public class DataSource() {
@Value("${jdbc.driverClassName}")
private String driverClassName; @Value("${jdbc.url}")
private String url; @Value("${jdbc.username}")
private String username; @Value("${jdbc.password}")
private String password;
}

引用bean的属性值

当一个bean需要引用另外一个bean的属性值的时候,也可以这么写:

@Component
public class ApplicationManager() {
@Value("#{sysConfig.sessionTimeout}")
private int sessionTimeout; @Value("#{sysConfig.maxTabPageNum}")
private int maxTabPageNum;
}

spring容器事件

applicationContext能够发布事件以及允许注册相应的事件监听器,事件很好理解,就是当容器中出现了什么操作的时候,比如服务器启动、关闭,各个组件需要去怎么做。事件监听器则是负责监听这个事件的发生,来看下事件体系图:

  • 事件源:事件的产生者,任何一个EventObject都必须拥有一个事件源。
  • 事件监听器注册表:容器中有很多事件监听器,事件监听器注册表就是存放所有事件监听器的容器,然后容器统一来管理此注册表。
  • 事件广播器:事件产生后,由事件广播器将事件通知给事件监听器。

1、applicationEvent

2、ApplicationListener

3、事件广播器

4、spring事件体系

5、示例

测试一下:

sendMail方法中,首先创建了一个MailSendEvent,然后调用applicationContext.publishEvent(event)方法将事件发送给所有的事件监听器(内部机制就是基于事件广播器)。因为sendMail中需要用到applicationContext上下文,因此MailSender实现了ApplicationContextAware接口。另外applicationContext在初始化的时候提供了一个默认的事件广播器(用户也可自定义),然后applicationContext根据反射机制,将容器中所有实现了ApplicationListener接口的bean注册到事件监听器注册表中。

spring深入学习(三)-----spring容器内幕的更多相关文章

  1. Spring Boot 项目学习 (三) Spring Boot + Redis 搭建

    0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...

  2. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  3. Spring Cloud 学习 之 Spring Cloud Eureka(搭建)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...

  4. spring深入学习(一)-----IOC容器

    spring对于java程序员来说,重要性不可言喻,可以想象下如果没有他,我们要多做多少工作,下面一个系列来介绍下spring(5.x版本). spring模块 IOC概念 spring中最重要的两个 ...

  5. Spring.Net学习笔记(1)-容器的使用

    一.下载地址: http://www.springframework.net/download.html 二.相关程序集 Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于 ...

  6. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  7. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  8. Spring框架学习03——Spring Bean 的详解

    1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...

  9. Spring框架学习02——Spring IOC 详解

    1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...

随机推荐

  1. CentOS 6.5 64位下安装Redis3.0.2的具体流程

    系统环境:CentOS 6.5 64位 安装方式:编译安装 防火墙:开启 Redis版本:Redis 3.0.2 一.环境准备 1.安装 gcc gcc-c++ [root@iZ94ebgv853Z ...

  2. Virtualbox下克隆CentOS 网络配置

    Virtualbox下克隆虚拟机非常容易,也使得我们在平常搭建测试环境方便了许多.不过克隆以后的虚机并不能够直接联网,这是由于网卡的MAC地址引起的.在"控制->复制"弹出的 ...

  3. Failed to introspect annotated methods on class 异常

    用@enable时出现错误 Failed to introspect annotated methods on class 很可能是库和springboot版本不一致

  4. Game Engine Architecture 4

    [Game Engine Architecture 4] 1.a model of multiple semi-independent flows of control simply matches ...

  5. 使用C编程语言实现AVL树

    本文将介绍AVL树及其插入.删除操作,最后使用C编程语言实现基于平衡因子(balance factor)的AVL树. 什么是AVL树? AVL树(AVL tree)是前苏联计算机科学家Adelson- ...

  6. Html 内容

    1.Html就是超文本标记语言的简写,是最基础的网页语言. 2.Html是通过标签来定义的语言,代码都是由标签组成的. 3.Html代码不用区分大小写. 4.Html代码由<html>开始 ...

  7. POJ-2236.WireleseNetwork.(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 43199   Accepted: 178 ...

  8. python-django(环境配置)

    1.配置虚拟环境 <1>.pip  install  virtualenv     安装创建虚拟环境的工具 <2>.pip  install  virtualenvwrappe ...

  9. Java创建对象的初始化顺序

    1. 初始化块 初始化块通常写在类的构造方法之前,由花括号括起来,通常包含对成员属性进行初始化的语句: 初始化块分为instance初始化块和static初始化块,初始化块在构造方法执行之前被执行: ...

  10. ES查询-match VS match_phrase

    我们以一个查询的示例开始,我们在student这个type中存储了一些学生的基本信息,我们分别使用match和match_phrase进行查询. 首先,使用match进行检索,关键字是“He is”: ...