springmvc如何启动(如何让spring mvc web应用启动时就执行特定处理)

:暂无数据 2026-08-07 09:10:12 :0

springmvc如何启动(如何让spring mvc web应用启动时就执行特定处理)

大家好,关于springmvc如何启动很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于如何让spring mvc web应用启动时就执行特定处理的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

本文目录

如何让spring mvc web应用启动时就执行特定处理

一、ApplicationContextAware接口
package org.springframework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.context.ApplicationContext;

public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext var1) throws BeansException;
}
二、ServletContextAware 接口
package org.springframework.web.context;

import javax.servlet.ServletContext;
import org.springframework.beans.factory.Aware;

public interface ServletContextAware extends Aware {
void setServletContext(ServletContext var1);
}
三、InitializingBean 接口
package org.springframework.beans.factory;

public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
四、ApplicationListener《ApplicationEvent》 接口
package org.springframework.context;

import java.util.EventListener;
import org.springframework.context.ApplicationEvent;

public interface ApplicationListener《E extends ApplicationEvent》 extends EventListener {
void onApplicationEvent(E var1);
}
示例程序:
package test.web.listener;

import org.apache.logging.log4j.*;
import org.springframework.beans.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import javax.servlet.ServletContext;

@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
InitializingBean, ApplicationListener《ContextRefreshedEvent》 {

protected Logger logger = LogManager.getLogger();

@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
logger.info("1 =》 StartupListener.setApplicationContext");
}

@Override
public void setServletContext(ServletContext context) {
logger.info("2 =》 StartupListener.setServletContext");
}

@Override
public void afterPropertiesSet() throws Exception {
logger.info("3 =》 StartupListener.afterPropertiesSet");
}

@Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
logger.info("4.1 =》 MyApplicationListener.onApplicationEvent");
if (evt.getApplicationContext().getParent() == null) {
logger.info("4.2 =》 MyApplicationListener.onApplicationEvent");
}
}

}
运行时,输出的顺序如下:
1 =》 StartupListener.setApplicationContext
2 =》 StartupListener.setServletContext
3 =》 StartupListener.afterPropertiesSet
4.1 =》 MyApplicationListener.onApplicationEvent
4.2 =》 MyApplicationListener.onApplicationEvent
4.1 =》 MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。

springmvc服务启动时先初始化一个类

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 ");
}
}
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);
}

Spring mvc 如何启动 socket

public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip.equals("0:0:0:0:0:0:0:1")) {
ip = "本地";
}
if (ip.split(",").length 》 1) {
ip = ip.split(",");
}
return getIpInfo(ip);
}

文章分享结束,springmvc如何启动和如何让spring mvc web应用启动时就执行特定处理的答案你都知道了吗?欢迎再次光临本站哦!

springmvc如何启动(如何让spring mvc web应用启动时就执行特定处理)

本文编辑:admin

更多文章:


withdrawal(withdrawal是什么意思)

withdrawal(withdrawal是什么意思)

今天给各位分享withdrawal是什么意思的知识,其中也会对withdrawal是什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

2026年10月11日 07:00

根据流程图怎么编写程序(用c语言根据流程图写程序)

根据流程图怎么编写程序(用c语言根据流程图写程序)

大家好,今天小编来为大家解答以下的问题,关于根据流程图怎么编写程序,用c语言根据流程图写程序这个很多人还不知道,现在让我们一起来看看吧!

2026年10月11日 06:00

在from子句中可以出现(如何在from 子句中嵌套查询下面的语句在access中出错!)

在from子句中可以出现(如何在from 子句中嵌套查询下面的语句在access中出错!)

大家好,关于在from子句中可以出现很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于如何在from 子句中嵌套查询下面的语句在access中出错!的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望

2026年10月11日 05:20

countif函数统计个数怎么用(countif函数怎么用 详解Excel中countif函数的使用方法)

countif函数统计个数怎么用(countif函数怎么用 详解Excel中countif函数的使用方法)

其实countif函数统计个数怎么用的问题并不复杂,但是又很多的朋友都不太了解countif函数怎么用 详解Excel中countif函数的使用方法,因此呢,今天小编就来为大家分享countif函数统计个数怎么用的一些知识,希望可以帮助到大

2026年10月11日 03:30

正则匹配数字之前的字符(正则表达式如何匹配前面是数字、中间是“/”、后面也是数字,就像2/3专业的模式)

正则匹配数字之前的字符(正则表达式如何匹配前面是数字、中间是“/”、后面也是数字,就像2/3专业的模式)

本篇文章给大家谈谈正则匹配数字之前的字符,以及正则表达式如何匹配前面是数字、中间是“/”、后面也是数字,就像2/3专业的模式对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问

2026年10月11日 03:00

register语言学(register语言学)

register语言学(register语言学)

“register语言学”相关信息最新大全有哪些,这是大家都非常关心的,接下来就一起看看register语言学(register语言学)!

2026年10月11日 01:40

系统架构设计师可以直接考吗(学生可以报名系统架构设计师吗)

系统架构设计师可以直接考吗(学生可以报名系统架构设计师吗)

“系统架构设计师可以直接考吗”相关信息最新大全有哪些,这是大家都非常关心的,接下来就一起看看系统架构设计师可以直接考吗(学生可以报名系统架构设计师吗)!

2026年10月11日 01:00

orlnsertbootmediinselected(我电脑开机显示这个是什么意思or insert boot media in select)

orlnsertbootmediinselected(我电脑开机显示这个是什么意思or insert boot media in select)

大家好,如果您还对orlnsertbootmediinselected不太了解,没有关系,今天就由本站为大家分享orlnsertbootmediinselected的知识,包括我电脑开机显示这个是什么意思or insert boot med

2026年10月10日 23:00

display的用法(display是什么意思 详解display的含义和用法)

display的用法(display是什么意思 详解display的含义和用法)

大家好,如果您还对display的用法不太了解,没有关系,今天就由本站为大家分享display的用法的知识,包括display是什么意思 详解display的含义和用法的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!

2026年10月10日 22:00

html全部居中代码(怎么让网页居中显示,html如何让网页居中)

html全部居中代码(怎么让网页居中显示,html如何让网页居中)

大家好,今天小编来为大家解答以下的问题,关于html全部居中代码,怎么让网页居中显示,html如何让网页居中这个很多人还不知道,现在让我们一起来看看吧!

2026年10月10日 21:10

最近更新

西数5000aakx(西数WD-5000AAKX-001CAO和西数800AAJS-00WAAO哪个做安装系统盘比较好)
2026-10-11 08:30:05 浏览:0
热门文章

标签列表