Spring框架中context-param与servlet中init-param的contextConfigLocation的区别
来源:互联网 发布:网易域名邮箱smtp 编辑:程序博客网 时间:2024/05/21 15:54
最近在使用spring这一个框架做定时任务的时候,发现一个问题。当我们不在浏览器中调用一下我们的接口,是不会自动运行定时文件的,这个原因是什么呢?刚开始的时候,使我费解了很长事件,不知道问题出现再哪里。但是当冷静下来思考一下的时候,发现了问题出现再servlet中。接着,对它进行分析一下。
在springmvc这个框架中,一般spring默认存在两个配置文件,一个是applicationContext.xml,另一个是spring-servlet.xml。一般情况下,我们会把注解中的自动加载,定时器的自动加载等内容写在spring-servlet.xml。然后把spring-servlet.xml,放在web.xml文件下的servlet中进行初始化。例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
而将applicationContext.xml配置成全局的形式,例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
对比一下,就会发现问题所在。spring-servlet.xml因为被配置在servlet的初始化过程中,所以如果不初始化servlet,那么spring-servlet.xml是不会被发现的。如果我们将定时器的注解加载过程放在spring-servlet.xml中,如果不进行页面的刷新,是不会经过DispatcherServlet这个跳转器的,所以也就不会加载spring-servlet.xml文件,所以定时任务可能就不会被触发。如果经过了这个DispatcherServlet这个跳转器,即被初始化。所以定时任务才会被执行。
而如果将注解的自动加载内容,例如:
- 1
- 1
放在了applicationContext.xml中。因为当服务器运行起来之后,就会自动加载applicationContext.xml文件,所以定时任务会立即被执行。
如果想要避免这种错误的发生,最简单的方法就是。将需要启动服务器之后,立刻就要知道的内容,例如
- 1
- 2
- 1
- 2
,加到applicationContext.xml文件中即可。
另一种方法就是,将spring的所有配置文件都放在context-param(上下文参数)中进行加载,例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
这样所有的问题就可以解决。
总结
从上面出现的问题,我们不难发现。spring框架在加载web配置文件的时候。首先加载的是context-param配置的内容,而并不会去初始化servlet。只有进行了网站的跳转,经过了DispatcherServlet的导航的时候,才会初始化servlet,从而加载init-param中的内容。
- Spring框架中context-param与servlet中init-param的contextConfigLocation的区别
- Spring框架中context-param与servlet中init-param的contextConfigLocation的区别
- Spring框架中context-param与servlet中init-param的contextConfigLocation的区别
- Servlet中init-param与context-param的区别
- Spring中<context-param>与<init-param>的区别与作用
- <context-param>与<init-param>的区别
- <context-param>与<init-param>的区别
- <context-param>与<init-param>的区别
- JAVA基础(二) web.xml中<context-param>与<init-param>的区别与作用
- web.xml中<context-param>与<init-param>的区别与作用
- <context-param>与<init-param>的区别与在分别存在哪些对象中。
- web.xml文件中context-param与init-param的区别
- web.xml中<context-param>的作用,及与init-param区别
- <context-param>与<init-param>的区别与作用
- <context-param>与<init-param>的区别与作用
- <context-param>与<init-param>的区别与作用
- <context-param>与<init-param>的区别与作用
- <context-param>与<init-param>的区别与作用
- android 进程相关
- matplotlib画图plot线型设置
- 多线程与静态方法及其中的局部变量关系
- pthread_exit()函数
- PHP 的array_push
- Spring框架中context-param与servlet中init-param的contextConfigLocation的区别
- 文章标题HDOJ_P2013
- 分布式架构学习之持续集成:008--实用软件和架构图
- LeetCode 152. Maximum Product Subarray
- 移植系统时思路记录
- 卷积神经网络CNN(LeNet)的Theano实现
- 如鹏java学习进程 模拟小球弹跳
- python基础知识-元组和字符串
- View