问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

spring初始化

发布网友 发布时间:2022-04-22 04:31

我来回答

3个回答

热心网友 时间:2023-12-17 08:22

1、都知道SpringMVC项目启动的时候都会初始化一个类:DispatcherServlet,看这个类的源码我们可以发现他其实就是一个servlet,

为什么这么说呢?请看:

DispatcherServlet extends FrameworkServlet
FrameworkServlet extends HttpServletBean
HttpServletBean extends HttpServlet
初始化这个DispatcherServlet的时候我们可以看到这个类里面有一个静态代码块:根据代码可以看出代码块里面会去读取DispatcherServlet.properties配置文件里面的配置,load到Properties集合里面
private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties";
private static final Properties defaultStrategies;

static {
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());
}
}

2、我们都知道一个servlet初始化阶段 ,会调用init()方法,那么这边会先调用HttpServlet的子类HttpServletBean里面的init方法:org.springframework.web.servlet.HttpServletBean#init,这个方法里面调用了initServletBean这个方法:org.springframework.web.servlet.FrameworkServlet#initServletBean

@Override
public final void init() throws ServletException {
if (logger.isDebugEnabled()) {
logger.debug("Initializing servlet '" + getServletName() + "'");
}

// Set bean properties from init parameters.
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
if (!pvs.isEmpty()) {
try {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {
if (logger.isErrorEnabled()) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
}
throw ex;
}
}

// Let subclasses do whatever initialization they like.
initServletBean();

if (logger.isDebugEnabled()) {
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}
}
org.springframework.web.servlet.FrameworkServlet#initServletBean里面调用了org.springframework.web.servlet.FrameworkServlet#initWebApplicationContext
@Override
protected final void initServletBean() throws ServletException {
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
if (this.logger.isInfoEnabled()) {
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
}
long startTime = System.currentTimeMillis();

try {
this.webApplicationContext = initWebApplicationContext();
initFrameworkServlet();
}
catch (ServletException | RuntimeException ex) {
this.logger.error("Context initialization failed", ex);
throw ex;
}

if (this.logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
elapsedTime + " ms");
}
}
org.springframework.web.servlet.FrameworkServlet#initWebApplicationContext

protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;

if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}

if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}

if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}

return wac;
}
里面有一个onRefresh方法:里面做了一些列的初始化操作,具体哪个方法对应什么功能呢?请大家自行看里面的源码,我这边主要说的是

initHandlerMappings(context);
initHandlerAdapters(context);
这两个方法就是初始化我们的映射器和我们的适配器的,到此我们的初始化就结束了;
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}

protected void initStrategies(ApplicationContext context) {
//此方法主要做文件上传处理的
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
//此方法主要是获取配置文件DispatcherServlet.properties里面的handlerMapping类型对象
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}

热心网友 时间:2023-12-17 08:22

都知道SpringMVC项目启动的时候都会初始化一个类:DispatcherServlet,看这个类的源码我们可以发现他其实就是一个servlet,
为什么这么说呢?请看:
DispatcherServlet extends FrameworkServlet
FrameworkServlet extends HttpServletBean
HttpServletBean extends HttpServlet
初始化这个DispatcherServlet的时候我们可以看到这个类里面有一个静态代码块:根据代码可以看出代码块里面会去读取DispatcherServlet.properties配置文件里面的配置,load到Properties集合里面
private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties";
private static final Properties defaultStrategies;
static {
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());

热心网友 时间:2023-12-17 08:23

因为我们一般在web.xml中配置DispatcherServlet的时候load-on-startup设置为立即执行,那么在容器启动后(即Spring初始化完成后),那么执行servlet的init方法进入SpringMVC初始化入口
可以看到这里初始化了WebApplicationContext亦即SpringMVC的容器,我们看一下在容器初始化的时候做了哪些工作(FrameworkServlet.initWebApplicationContext());
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
单位高温防护欠缺致员工中暑如何对待 狗狗为什么爱看视频 360浏览器怎么设置倍速播放 ...先讲女主的灵魂飘荡了一段时间,然后重生,请问是那本? 拯救者散热器怎么开 电脑如何一键还原系统电脑一键还原怎么操作 神舟笔记本电脑怎么重新设置神舟战神bios恢复出厂设置 神舟电脑恢复出厂设置神舟战神怎么恢复原厂系统 水泥楼梯如何铺木楼梯 家里面楼梯是水泥的不想铺地毯或者地砖还能铺什么 思维导图培训教程,思维导图软件怎么设置成中文版... 扇贝单词不能打卡,除非做两个任务,不做行吗 怎样判断Spring容器初始化完成 spring初始化时,向一个bean注入ApplicationContext. 思维导图培训教程,苏教版小学语文五年级下册思维导图 如何告诉spring初始化指定配置文件中的javabean 请问spring注解bean初始化顺序? spring怎么使用注解在初始化bean的时候init-method... spring是怎么初始化的 4000元左右能买什么笔记本电脑 4000元能买个手机吗,推荐一下, 4000元左右的手机买什么好 现在4000块买什么手机好,谢谢大家了! 黑坑钓鲤鱼防止跑鱼的办法 各位大虾,高德导航上的帐号登陆是什么意思? 买什么手机?4000元备用金 钓鲤鱼黑坑什么鱼竿好,飞磕。。如何选择?具备哪... 高德地图注册账号的好处?有没必要注册账号? 黑坑钓鲤鱼爆护绝招 窍门? 现在买什么手机好,4000元左右的 spring 注解注入怎么注入属性 怎样让spring重新初始化所有的bean 谈谈你们对 beyond灰色轨迹和海阔天空这两首歌尾奏... 普通类如何初始化Spring的bean? spring初始化bean 成员变量初始化吗 怎样在spring初始化完成后执行一些操作 Spring bean初始化与销毁的几种方式和区别 伍佰的《 挪威的森林》尾奏和黄家驹的《灰色轨迹》... spring xml文件 bean 初始化 灰色轨迹的歌曲鉴赏 如何判断spring所有bean初始化完了 spring bean初始化的完整生命周期是怎样的 灰色轨迹尾奏怎么样 spring @Service()中初始化方法怎么在@Service()的... 那位给客观评价一下beyond乐队成员的技术。 有什么办法在spring初始化之前执行一个方法 懂吉他的进。。灰色轨迹尾奏 如何初始化spring容器后不要关闭,始终运 和同事出租车拼车,司机给了2张发票,发票号码不一... 关于出租车拼车