问题:

  数据初始化监听器要注入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 -------使用监听器初始化上下文参数的更多相关文章

  1. 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean

    1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...

  2. 配置spring的监听器 让spring随项目的启动而启动

    <!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...

  3. 配置Spring的用于初始化容器对象的监听器

    <!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...

  4. 48、[源码]-Spring容器创建-初始化事件派发器、监听器等

    48.[源码]-Spring容器创建-初始化事件派发器.监听器等 8.initApplicationEventMulticaster();初始化事件派发器: 获取BeanFactory 从BeanFa ...

  5. spring listener监听器

    1.Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listen ...

  6. 使用spring的监听器来完成系统超级管理员的注册

    1.注入 2“util类 package com.liveyc.mgrsite.util; import org.springframework.beans.factory.annotation.Au ...

  7. 监听器如何获取Spring配置文件

    我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...

  8. Spring MVC源码(一) ----- 启动过程与组件初始化

    SpringMVC作为MVC框架近年来被广泛地使用,其与Mybatis和Spring的组合,也成为许多公司开发web的套装.SpringMVC继承了Spring的优点,对业务代码的非侵入性,配置的便捷 ...

  9. Spring的监听器ContextLoaderListener

    一.作用 ContextLoaderListener监听器的作用就是启动web容器时,自动装配ApplicationContext的配置信息.它实现了ServletContextListener接口, ...

随机推荐

  1. composer安装包的时候触发PHP fatal error,提示允许的内存耗光

    在composer require之前加COMPOSER_MEMORY_LIMIT=-1

  2. 牛客网PAT练兵场-部分A+B

    题解:简单循环 题目地址:https://www.nowcoder.com/questionTerminal/fb581ea099a14f5d97c6149cbeee249f /** * *作者:Yc ...

  3. 修改docker0网桥的IP段

    关闭docker进程 systemctl stop docker 修改/etc/docker/daemon.json { "bip": "100.96.2.1/24&qu ...

  4. luogu_P3373 solution

    luogu_P3373 solution Problme Description  Now, you have a known series, there are three operations: ...

  5. Excel—TIME函数简介与用法

    问题场景 算员工饱和度时,需要从实际考勤打卡时间中减去午休时间1.5个小时: 导出的时间时分秒是分开的,连接时分秒. 场景一 计算员工饱和度,需要减去午休时间,用下班打卡时间减去午休的1.5小时算出的 ...

  6. Python中的协程,为什么说它的底层是生成器?

    我们曾经在golang关于goroutine的文章当中简单介绍过 协程 的概念,我们再来简单review一下.协程又称为是微线程,英文名是Coroutine.它和线程一样可以调度,但是不同的是线程的启 ...

  7. Google谷歌在根据流量统计分析当年的2008年汶川大地震

    这是一张2008年的老图,Google当时的博文说道:"当我们依照惯例整理和分析谷歌搜索引擎的流量数据时,一条从未见过的曲线出现在我们面前.当意识到发生了什么事情时,我们的眼睛湿润了.&qu ...

  8. stf-多设备管理平台搭建

    项目地址: https://github.com/openstf/stf 安装.使用命令 # 安装stfbrew install rethinkdb graphicsmagick zeromq pro ...

  9. 几个Graphics函数

    1.Graphics.Blit:Copies source texture into destination render texture with a shader 声明: 1.public sta ...

  10. [BUUOJ记录] [BJDCTF2020]Easy MD5

    各种关于md5的Bypass操作,都是基本操作,考察数组绕过.弱类型比较绕过以及md5($password,true) ByPass 1.利用md5($password,true)实现SQL注入 F1 ...