- 浏览: 491835 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (124)
- hibernate (7)
- spring (18)
- java se (32)
- jdbc (5)
- js (4)
- 数据库 (1)
- ide (2)
- Struts2 (1)
- 有用的链接 (2)
- 开源框架 (2)
- j2ee (2)
- spring mvc (4)
- weblogic (0)
- spring security (4)
- web服务器 (5)
- maven (2)
- 多线程 (12)
- linux (3)
- NIO (1)
- webservice (1)
- guava (5)
- restful (1)
- jenkins (1)
- ios (1)
- ehcache (1)
- jvm (1)
- 深入jvm (2)
- akka发 (1)
- disruptor (3)
- spring boot,异常处理, protobuf (1)
- curator (1)
- zookeeper (1)
最新评论
-
abc08010051:
张延龙地盘 写道多实例下就不行了吧是的,多实例直接上分布式锁
java高并发下的唯一性验证 -
张延龙地盘:
多实例下就不行了吧
java高并发下的唯一性验证 -
a12345531:
s3.getResourceUrl(bucketName, k ...
Amazon AWS S3 上传文件 并获取公用url -
544483342:
楼主请问WorkerEventHandler和EventHan ...
disruptor(一) 单一生产者和WorkPool消费者源码阅读 -
544483342:
请问楼主使用的是什么UML工具啊?
disruptor(一) 单一生产者和WorkPool消费者源码阅读
Spring MVC中的IoC容器初始化
2012-03-02 09:29:20| 分类: springMVC |字号 订阅
Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext,使得拥有web功能。那么,Spring MVC是如何在web环境中创建IoC容器呢?web环境中的IoC容器的结构又是什么结构呢?web环境中,spring IoC容器是怎么启动呢?
先看一下WebApplicationContext是如何扩展ApplicationContext来添加对Web环境的支持的。WebApplicationContext接口定义如下:
[java] view plaincopy
public interface WebApplicationContext extends ApplicationContext {
//根上下文在ServletContext中的名称
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
//取得web容器的ServletContext
ServletContext getServletContext();
}
对于web容器中创建IoC容器的过程,我们从web.xml配置文件讲起。看一下Spring MVC的web.xml中的相关配置:
[xhtml] view plaincopy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Maps all /app requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
在web.xml配置文件中,有两个主要的配置:ContextLoaderListener和DispatcherServlet。同样的关于 spring配置文件的相关配置也有两部分:context-param和DispatcherServlet中的init-param。那么,这两部分 的配置有什么区别呢?它们都担任什么样的职责呢?
在Spring MVC中,Spring Context是以父子的继承结构存在的。Web环境中存在一个ROOT Context,这个Context是整个应用的根上下文,是其他context的双亲Context。同时Spring MVC也对应的持有一个独立的Context,它是ROOT Context的子上下文。
对于这样的Context结构在Spring MVC中是如何实现的呢?下面就先从ROOT Context入手,ROOT Context是在ContextLoaderListener中配置的,ContextLoaderListener读取context-param中 的contextConfigLocation指定的配置文件,创建ROOT Context。下面看一下ContextLoaderListener中创建context的源码:
[java] view plaincopy
/**
* Initialize Spring's web application context for the given servlet context,
* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//PS : ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE=WebApplicationContext.class.getName() + ".ROOT" 根上下文的名称
//PS : 默认情况下,配置文件的位置和名称是: DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"
//在整个web应用中,只能有一个根上下文
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
// 在这里执行了创建WebApplicationContext的操作
this.context = createWebApplicationContext(servletContext, parent);
//PS: 将根上下文放置在servletContext中
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
再看一下WebApplicationContext对象是如何创建的:
[java] view plaincopy
protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
//根据web.xml中的配置决定使用何种WebApplicationContext。默认情况下使用XmlWebApplicationContext
//web.xml中相关的配置context-param的名称“contextClass”
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
//实例化WebApplicationContext的实现类
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
// Assign the best possible id value.
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
// Servlet <= 2.4: resort to name specified in web.xml, if any.
String servletContextName = sc.getServletContextName();
if (servletContextName != null) {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName);
}
else {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX);
}
}
else {
// Servlet 2.5's getContextPath available!
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath());
}
wac.setParent(parent);
wac.setServletContext(sc);
//设置spring的配置文件
wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
customizeContext(sc, wac);
//spring容器初始化
wac.refresh();
return wac;
}
以上是web容器中根上下文的加载与初始化,下面介绍一下Spring MVC对应的上下文是如何加载的。
Spring MVC中核心的类是DispatcherServlet,在这个类中完成Spring context的加载与创建,并且能够根据Spring Context的内容将请求分发给各个Controller类。DispatcherServlet继承自HttpServlet,关于Spring Context的配置文件加载和创建是在init()方法中进行的,主要的调用顺序是 init-->initServletBean-->initWebApplicationContext。
先来看一下initWebApplicationContext的实现
[java] view plaincopy
/**
* Initialize and publish the WebApplicationContext for this servlet.
* <p>Delegates to {@link #createWebApplicationContext} for actual creation
* of the context. Can be overridden in subclasses.
* @return the WebApplicationContext instance
* @see #setContextClass
* @see #setContextConfigLocation
*/
protected WebApplicationContext initWebApplicationContext() {
//先从web容器的ServletContext中查找WebApplicationContext
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
// No fixed context defined for this servlet - create a local one.
//从ServletContext中取得根上下文
WebApplicationContext parent =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//创建Spring MVC的上下文,并将根上下文作为起双亲上下文
wac = createWebApplicationContext(parent);
}
if (!this.refreshEventReceived) {
// Apparently not a ConfigurableApplicationContext with refresh support:
// triggering initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
// 取得context在ServletContext中的名称
String attrName = getServletContextAttributeName();
//将Spring MVC的Context放置到ServletContext中
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
通过initWebApplicationContext方法的调用,创建了DispatcherServlet对应的context,并将其放置到ServletContext中,这样就完成了在web容器中构建Spring IoC容器的过程。
最后,在分别给出ContextLoaderListener和DispatcherServlet构建context的时序。
ContextLoaderListener构建Root Context时序图:
ContextLoaderListener
DispatcherServlet创建context时序图:
DispatcherServlet创建context时序图
(转载:http://xuzhaoshancm.blog.163.com/blog/static/5111889120122292920389/)
2012-03-02 09:29:20| 分类: springMVC |字号 订阅
Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext,使得拥有web功能。那么,Spring MVC是如何在web环境中创建IoC容器呢?web环境中的IoC容器的结构又是什么结构呢?web环境中,spring IoC容器是怎么启动呢?
先看一下WebApplicationContext是如何扩展ApplicationContext来添加对Web环境的支持的。WebApplicationContext接口定义如下:
[java] view plaincopy
public interface WebApplicationContext extends ApplicationContext {
//根上下文在ServletContext中的名称
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
//取得web容器的ServletContext
ServletContext getServletContext();
}
对于web容器中创建IoC容器的过程,我们从web.xml配置文件讲起。看一下Spring MVC的web.xml中的相关配置:
[xhtml] view plaincopy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Maps all /app requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
在web.xml配置文件中,有两个主要的配置:ContextLoaderListener和DispatcherServlet。同样的关于 spring配置文件的相关配置也有两部分:context-param和DispatcherServlet中的init-param。那么,这两部分 的配置有什么区别呢?它们都担任什么样的职责呢?
在Spring MVC中,Spring Context是以父子的继承结构存在的。Web环境中存在一个ROOT Context,这个Context是整个应用的根上下文,是其他context的双亲Context。同时Spring MVC也对应的持有一个独立的Context,它是ROOT Context的子上下文。
对于这样的Context结构在Spring MVC中是如何实现的呢?下面就先从ROOT Context入手,ROOT Context是在ContextLoaderListener中配置的,ContextLoaderListener读取context-param中 的contextConfigLocation指定的配置文件,创建ROOT Context。下面看一下ContextLoaderListener中创建context的源码:
[java] view plaincopy
/**
* Initialize Spring's web application context for the given servlet context,
* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//PS : ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE=WebApplicationContext.class.getName() + ".ROOT" 根上下文的名称
//PS : 默认情况下,配置文件的位置和名称是: DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"
//在整个web应用中,只能有一个根上下文
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
// 在这里执行了创建WebApplicationContext的操作
this.context = createWebApplicationContext(servletContext, parent);
//PS: 将根上下文放置在servletContext中
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
再看一下WebApplicationContext对象是如何创建的:
[java] view plaincopy
protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
//根据web.xml中的配置决定使用何种WebApplicationContext。默认情况下使用XmlWebApplicationContext
//web.xml中相关的配置context-param的名称“contextClass”
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
//实例化WebApplicationContext的实现类
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
// Assign the best possible id value.
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
// Servlet <= 2.4: resort to name specified in web.xml, if any.
String servletContextName = sc.getServletContextName();
if (servletContextName != null) {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName);
}
else {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX);
}
}
else {
// Servlet 2.5's getContextPath available!
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath());
}
wac.setParent(parent);
wac.setServletContext(sc);
//设置spring的配置文件
wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
customizeContext(sc, wac);
//spring容器初始化
wac.refresh();
return wac;
}
以上是web容器中根上下文的加载与初始化,下面介绍一下Spring MVC对应的上下文是如何加载的。
Spring MVC中核心的类是DispatcherServlet,在这个类中完成Spring context的加载与创建,并且能够根据Spring Context的内容将请求分发给各个Controller类。DispatcherServlet继承自HttpServlet,关于Spring Context的配置文件加载和创建是在init()方法中进行的,主要的调用顺序是 init-->initServletBean-->initWebApplicationContext。
先来看一下initWebApplicationContext的实现
[java] view plaincopy
/**
* Initialize and publish the WebApplicationContext for this servlet.
* <p>Delegates to {@link #createWebApplicationContext} for actual creation
* of the context. Can be overridden in subclasses.
* @return the WebApplicationContext instance
* @see #setContextClass
* @see #setContextConfigLocation
*/
protected WebApplicationContext initWebApplicationContext() {
//先从web容器的ServletContext中查找WebApplicationContext
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
// No fixed context defined for this servlet - create a local one.
//从ServletContext中取得根上下文
WebApplicationContext parent =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//创建Spring MVC的上下文,并将根上下文作为起双亲上下文
wac = createWebApplicationContext(parent);
}
if (!this.refreshEventReceived) {
// Apparently not a ConfigurableApplicationContext with refresh support:
// triggering initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
// 取得context在ServletContext中的名称
String attrName = getServletContextAttributeName();
//将Spring MVC的Context放置到ServletContext中
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
通过initWebApplicationContext方法的调用,创建了DispatcherServlet对应的context,并将其放置到ServletContext中,这样就完成了在web容器中构建Spring IoC容器的过程。
最后,在分别给出ContextLoaderListener和DispatcherServlet构建context的时序。
ContextLoaderListener构建Root Context时序图:
ContextLoaderListener
DispatcherServlet创建context时序图:
DispatcherServlet创建context时序图
(转载:http://xuzhaoshancm.blog.163.com/blog/static/5111889120122292920389/)
发表评论
-
spring 事件机制 异步操作
2018-01-07 09:50 14461 spring 事件机制的好处:解耦合,对于一对多的操作, ... -
spring ehcache 集群配置
2014-08-06 10:09 14811本篇文章是个人实践证明的,下面是步骤: 1 ... -
spring cache ehcache 所需jar包
2014-03-20 15:54 8301对于spring cache的配置和使用网上说了 ... -
spring 面试题
2014-02-10 15:24 01 spring 注入方式:接口注入;set注入,构造函数注 ... -
spring mvc 源码(一)web容器创建
2013-11-20 17:51 1510spring mvc是一个mvc开源框架,由于与sprin ... -
spring 事务传播特性 测试 发现的问题
2013-11-19 16:47 1436测试spring事务传播特性: @Ove ... -
spring 事务(常规)
2013-11-19 15:02 14811 本地事务,全局事务 本地事务:在单个 EIS ... -
spring aop
2013-11-15 17:24 991本文没有去探寻源码,只是根据现象去推理一下: 1 定义切面 ... -
spring aop 常规使用
2013-11-14 14:49 2360spring aop使用有两种方式,一种是基于XML的,一 ... -
spring bean配置
2013-11-13 09:12 864spring bean配置记录 scope: singl ... -
spring 资源访问 Resource
2013-11-11 15:34 1709在jdk的api中,资源访问基本上都是通过URL类和I ... -
jdk Observer 和 spring Observer的比较
2013-11-08 16:42 0分析了spring的Observer之后,想对比一下jdk ... -
Spring 使用的设计模式(二) Observer(观察者)
2013-11-08 10:53 7172spring在容器中使用了观察者模式: spring ... -
spring 3 dataSource 事务管理
2013-11-04 10:03 3230以前在项目中使用spring管理hibernate,配置s ... -
spring Context 创建
2013-10-30 22:34 2104使用spring框架的时候,在web.xml的配置文件 ... -
HibernateTemplate 理解(对Callback继续理解)
2013-10-30 16:08 1435经常使用spring封装hibernate的同学, ... -
Spring 使用的设计模式(一) CallBack(回调)
2013-10-28 10:30 10057一 回调模式(CallBack) 1 类A持有一 ... -
Spring IOC 父子容器
2013-03-28 18:11 5360通过HierarchicalBeanFactory接口,Spr ... -
spring 事务详解(转载)
2013-03-18 22:31 833spring事务管理全解析 文章出处:DIY部落(http: ...
相关推荐
Spring的IoC容器初始化源码解析,包括资源定位、加载、注册3个过程
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
本文深入探讨了Spring IoC容器的加载过程及其源码实现,揭示了Spring中最为根本的概念之一。这包括从AnnotationConfigApplicationContext的实例化开始,到DefaultListableBeanFactory工厂的建立,再到...
本文深入探讨了Spring框架中IoC容器的源码机制,涵盖了容器的初始化、Bean工厂的实例化、Bean定义的读取及Spring Bean的生命周期管理。通过精细的分析,本文揭示了AnnotationConfigApplicationContext的实例化过程,...
3、源码分析-IOC容器的初始化 4、源码分析-IOC容器的依赖注入 5、源码分析-IOC容器的高级特性 三阶段 Spring AOP的涉及原理及具体实践 SpringJDBC的涉及原理及二次开发 SpringMVC框架设计原理及手写实现 四阶段 ...
Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务。 Spring IOC容器对Bean的生命周期进行管理的过程如下: 通过构造器或工厂方法创建Bean实例 为Bean的属性设置值和对其它Bean的...
1、通过分析 Spring 源码,深刻掌握核心原理和设计思想 2、通过本课的学习,完全掌握 SpringIOC 容器的初始化细节,并手绘时序图 3、掌握看源码不
它们被Spring IOC容器初始化,装配,和管理。这些beans通过容器中配置的元数据创建。比如,以XML文件中<bean/> 的形式定义。Spring 框架定义的beans都是单件beans。在bean tag中有个属性”singleton”,如果它被赋为...
多重对象嵌套,IoC容器就可以有效地帮我们去避免在程序中编写复杂的初始化逻辑,使开发 更便捷。 • IoC叫做控制反转,也就是我们失去了对于对象的创建权利的控制,IoC容器帮我们维护了所有 项⽬中对象的⽣命周期...
SSH笔记-IOC容器中 Bean 的生命周期,通过实现BeanPostProcessor接口,来定义初始化方法和销毁方法时的逻辑
Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22 注:后面的...
目录 Spring IoC容器的两个接口 依赖注入的类型 Bean的配置 …IOC:控制反转依赖注入。它使程序组件或类之间尽量形成一种松耦合的结构,开发者在使用类的实列之前,需要先创建...BeanFactory在初始化的时候,不会被
作者:钱安川(Moxie) ...Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22
Bean的作用域Bean的作用域SingletonPrototypeRequestSessionGlobal Session 相关文章 跟着官网学spring—快速入门指南 跟着官网学Spring—构建RESTful Web服务 ...简单地讲,bean就是由IoC容器初始化、装配及管理的
初始化阶段:重写Servlet的init方法,实现加载配置文件,读取配置信息, 扫描所有相关的类, 初始化相关类的实例并保存到IOC容器, 从IOC容器取出对应的实例给字段赋值,即依赖注入, 最后将url和Method进行关联。...
第一天内容:Spring框架简介(EJB、JMX、Spring核心功能、Spring模块详解、Spring重要概念(容器)、Spring容器初始化的整个流程、Spring后处理器),IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性...
@Bean注解用于指示方法实例化、配置和初始化由Spring IoC容器管理的新对象。对于那些熟悉Spring的 XML配置的人来说,@Bean注解的作用与元素相同。你可以对任何Spring @Component使用@Bean注解的方法,但是,它们最...
一个很小的IoC容器是指Spring。 关于 tiny-spring是为了学习Spring的而开发的,可以认为是一个Spring的精简版。Spring的代码很多,层次复杂,阅读起来费劲。我尝试从使用功能的角度出发,参考Spring的实现,一步一步...
为了便于这些工作的设计,Spring IoC 容器提供了相关的功能,可以让应用定制 Bean 的初始化和销毁过程。 Bean 生命周期 的 init-method 属性或 @Bean 注解的 initMethod 属性),那么将调用该方法。 如果存在与 ...