使用监听器来启动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接口, ...
随机推荐
- CentOS 桥接网卡配置
[root@controller ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 ONBOOT=yes TYPE=Bridge ...
- ReplayingDecoder 解码器:别以为我有多厉害,也只不过是使用了一下装饰器模式而已~
原文地址 一.设计模式为啥老是用不好? 想要写出更屌的代码,提高代码的健壮性和可扩展性,那么设计模式可谓是必学的技能. 关于学习设计模式,大家可能都觉得设计模式的概念太过于抽象,理解起来有点费劲:又或 ...
- ACM集训第一次积分赛赛前复习+day4
不知不觉4天过去了,我们迎来了我们第一次积分赛,赛前的四天我们学了以下知识点吧: day 1.排序 之前一直想用qsort,但是总是写不明白,STL的sort()可以说是很方便了. 先写一个最基础的数 ...
- 牛客网PAT练兵场-德才论
题解:用sort排序即可 题目地址:https://www.nowcoder.com/questionTerminal/97b6a49a85944650b2e3d0660b91c324 /** * C ...
- 通过索引优化sql
sql语句的优化最重要的一点就是要合理使用索引,下面介绍一下使用索引的一些原则: 1.最左前缀匹配原则.mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹 ...
- java23种设计模式——三、工厂模式
源码在我的github和gitee中获取 工厂模式 工厂模式介绍 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在J ...
- Qt 改变鼠标形状
Qt 改变鼠标形状(转载) 改变鼠标形状,在绘制坐标系的时候有用到,特此记下: 1 this->setMouseTracking(true); //设置为不按下鼠标键触发moveEvent 2 ...
- 23种设计模式 - 状态变化(Memento备忘录 - State)
其他设计模式 23种设计模式(C++) 每一种都有对应理解的相关代码示例 → Git原码 ⌨ 状态变化 在组件构建过程中,某些对象的状态经常面临变化,如何对这些变化进行有效的管理?同时又维持高层模块的 ...
- Rakefile
Rakefile https://www.jianshu.com/p/b18a6e42455a RAKE – Ruby Make http://docs.seattlerb.org/rake/ bun ...
- 详解 Python 的二元算术运算,为什么说减法只是语法糖?
原题 | Unravelling binary arithmetic operations in Python 作者 | Brett Cannon 译者 | 豌豆花下猫("Python猫&q ...