使用监听器来启动spring -------使用监听器初始化上下文参数
问题:
数据初始化监听器要注入spring容器的对象,必须先启动spring容器才能使用监听器初始化数据。
解决:
使用监听器来启动spring框架
问题:spring框架启动需要哪些参数?
1.需要指定配置文件或者配置类的位置
2.如果是使用注解的配置累,需要修改容器的类型
问题:监听器是不支持初始化参数的。参数如何传递到监听器里面?
答:通过设置再全局上下文参数里面,ContextLoaderListener监听器是通过获得上下文参数来解决这个问题的
<context-param>
<param-name>contextConfigLocation</param-name> //注意这里对应的spring前端控制器的<param-value>不用写
<param-value>org.chu.config</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
自定义数据初始化监听器
public class DataInitializeListener implements ServletContextListener {
//问题:Spring的依赖注入只能在Spring容器中的对象使用。而监听器不是Spring容器的对象。
//解决方法:在非容器里的对象获得容器里面的对象,容器对象.getBean();
/**
* 上下文初始化的时候调用的方法
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("==启动啦==");
ServletContext context = sce.getServletContext();
//ApplicationContext
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
RoleService roleService = applicationContext.getBean(RoleService.class);
DictionaryService dictionaryService = applicationContext.getBean(DictionaryService.class);
ModularService modularService=applicationContext.getBean(ModularService.class);
PermissionService permissionService=applicationContext.getBean(PermissionService.class);
UserService userService = applicationContext.getBean(UserService.class);
AreaService areaService = applicationContext.getBean(AreaService.class);
CustomerSerivce customerSerivce = applicationContext.getBean(CustomerSerivce.class);
}
/**
* 上下文销毁的时候调用的方法
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("==关闭了==");
ServletContext context = sce.getServletContext();
context.removeAttribute("global_roles");
context.removeAttribute("global_dictionarys");
}
}
使用监听器来启动spring -------使用监听器初始化上下文参数的更多相关文章
- 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...
- 配置spring的监听器 让spring随项目的启动而启动
<!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...
- 配置Spring的用于初始化容器对象的监听器
<!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...
- 48、[源码]-Spring容器创建-初始化事件派发器、监听器等
48.[源码]-Spring容器创建-初始化事件派发器.监听器等 8.initApplicationEventMulticaster();初始化事件派发器: 获取BeanFactory 从BeanFa ...
- spring listener监听器
1.Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listen ...
- 使用spring的监听器来完成系统超级管理员的注册
1.注入 2“util类 package com.liveyc.mgrsite.util; import org.springframework.beans.factory.annotation.Au ...
- 监听器如何获取Spring配置文件
我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...
- Spring MVC源码(一) ----- 启动过程与组件初始化
SpringMVC作为MVC框架近年来被广泛地使用,其与Mybatis和Spring的组合,也成为许多公司开发web的套装.SpringMVC继承了Spring的优点,对业务代码的非侵入性,配置的便捷 ...
- Spring的监听器ContextLoaderListener
一.作用 ContextLoaderListener监听器的作用就是启动web容器时,自动装配ApplicationContext的配置信息.它实现了ServletContextListener接口, ...
随机推荐
- 软工团队项目之团队展示&选题(OnTime——S.L.N)
软工团队项目之团队展示&选题(OnTime——S.L.N) 一.团队展示 队名:『S.L.N』即Seigelion——乃“攻城狮”之意. 队员学号: 团队项目描述:(项目名称:OnTime) ...
- 【转】Windows10删除文件时却提示文件不存在的解决方案
Windows10系统使用一段时间后用户都会定期进行删除清理系统垃圾,减少系统盘的容量占用,但在删除的过程中许多用户都有可能遇到无法删除的情况,如下为删除文件时却提示文件不存在的解决方案. 1.新建一 ...
- python - 常用数据清洗方法-重复项处理
在数据的处理过程中,一般都需要进行数据清洗工作,如数据集是否存在重复,是否存在缺失,数据是否具有完整性和一致性,数据中是否存在异常值等.发现诸如此类的问题都需要针对性地处理,下面我们一起学习常用的数据 ...
- ElasticSearch生命周期管理-索引策略配置与操作
概述 本文是在本人学习研究ElasticSearch的生命周期管理策略时,发现官方未提供中文文档,有的也是零零散散,此文主要是翻译官方文档Policy phases and actions模块. 注: ...
- 图数据库对比:Neo4j vs Nebula Graph vs HugeGraph
本文系腾讯云安全团队李航宇.邓昶博撰写 图数据库在挖掘黑灰团伙以及建立安全知识图谱等安全领域有着天然的优势.为了能更好的服务业务,选择一款高效并且贴合业务发展的图数据库就变得尤为关键.本文挑选了几款业 ...
- WebApis中BOM的学习
1.1. 常用的键盘事件 1.1.1 键盘事件 <script> // 常用的键盘事件 //1. keyup 按键弹起的时候触发 document.addEventListener('ke ...
- 关于bat批处理的一些操作,如启动jar 关闭进程等
先说一下学习这个的前提: 公司要写个生成uid的工具,整完了之后就又整批处理工具,出于此目的,也是为了丰富自己的知识,就学习了一下,下面是相关的批处理脚本 我花了半天的时间找了相关的bat批处理,但是 ...
- CodeForces - 1114D-Flood Fill (区间dp)
You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ...
- PHP check 的一些绕过技术
绕过空格 $_GET[str]=str_replace(" ","",$_GET[str]); ${IFS} 但不能写作 $IFS $IFS$ % 绕过ech ...
- linux下P2P协议(BitTorrent)-libtorrent库编译,测试
1.libtorrent 简介,下载和编译 libtorrent简介 libtorrent是功能齐全的C ++ bittorrent的p2p协议实现,专注于效率和可伸缩性.它可以在嵌入式设备和台式机上 ...