关于监听器的问题

关于监听器的问题

我想在项目启动时就加载图书分类,写了一个监听器,代码如下:

package com.imooc.bms.global;

import com.imooc.bms.entity.Category;
import com.imooc.bms.service.CategoryService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;
@Component
public class CategoryListener implements ServletContextListener {

   @Resource(name = "categoryService")
   private CategoryService categoryService;
   public void contextInitialized(ServletContextEvent sce) {
       List<Category> categories=categoryService.getAll();
       sce.getServletContext().setAttribute("categories",categories);
   }

   public void contextDestroyed(ServletContextEvent sce) {

   }
}

xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">


   <servlet>
       <servlet-name>GlobalController</servlet-name>
       <servlet-class>com.imooc.bms.global.GlobalController</servlet-class>
   </servlet>
   <servlet-mapping>
       <servlet-name>GlobalController</servlet-name>
       <url-pattern>*.do</url-pattern>
   </servlet-mapping>

   <filter>
       <filter-name>EncodingFilter</filter-name>
       <filter-class>com.imooc.bms.global.EncodingFilter</filter-class>
       <init-param>
           <param-name>ENCODING</param-name>
           <param-value>UTF-8</param-value>
       </init-param>
   </filter>
   <filter-mapping>
       <filter-name>EncodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

   <listener>
       <listener-class>com.imooc.bms.global.CategoryListener</listener-class>
   </listener>
</web-app>

但是项目启动时就报错,无法运行,请老师看看什么问题,报错信息如下:

D:\javaxx\javatool\apache-tomcat-8.5.43\bin\catalina.bat run

[2020-02-29 12:51:50,526] Artifact bms_web:war exploded: Waiting for server connection to start artifact deployment...

Using CATALINA_BASE:   "C:\Users\pan\.IntelliJIdea2019.3\system\tomcat\Unnamed_bms"

Using CATALINA_HOME:   "D:\javaxx\javatool\apache-tomcat-8.5.43"

Using CATALINA_TMPDIR: "D:\javaxx\javatool\apache-tomcat-8.5.43\temp"

Using JRE_HOME:        "C:\Program Files\Java\jdk1.8.0_191"

Using CLASSPATH:       "D:\javaxx\javatool\apache-tomcat-8.5.43\bin\bootstrap.jar;D:\javaxx\javatool\apache-tomcat-8.5.43\bin\tomcat-juli.jar"

Connected to the target VM, address: '127.0.0.1:55442', transport: 'socket'

29-Feb-2020 12:51:52.136 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.43

29-Feb-2020 12:51:52.138 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jul 4 2019 20:53:15 UTC

29-Feb-2020 12:51:52.138 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.43.0

29-Feb-2020 12:51:52.138 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk1.8.0_191\jre

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_191-b12

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\pan\.IntelliJIdea2019.3\system\tomcat\Unnamed_bms

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:\javaxx\javatool\apache-tomcat-8.5.43

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\pan\.IntelliJIdea2019.3\system\tomcat\Unnamed_bms\conf\logging.properties

29-Feb-2020 12:51:52.139 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

29-Feb-2020 12:51:52.140 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:55442,suspend=y,server=n

29-Feb-2020 12:51:52.140 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -javaagent:C:\Users\pan\.IntelliJIdea2019.3\system\captureAgent\debugger-agent.jar

29-Feb-2020 12:51:52.140 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=

29-Feb-2020 12:51:52.140 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099

29-Feb-2020 12:51:52.140 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false

29-Feb-2020 12:51:52.141 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.password.file=C:\Users\pan\.IntelliJIdea2019.3\system\tomcat\Unnamed_bms\jmxremote.password

29-Feb-2020 12:51:52.141 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\pan\.IntelliJIdea2019.3\system\tomcat\Unnamed_bms\jmxremote.access

29-Feb-2020 12:51:52.141 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1

29-Feb-2020 12:51:52.141 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048

29-Feb-2020 12:51:52.141 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources

29-Feb-2020 12:51:52.141 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=

29-Feb-2020 12:51:52.142 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\pan\.IntelliJIdea2019.3\system\tomcat\Unnamed_bms

29-Feb-2020 12:51:52.142 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\javaxx\javatool\apache-tomcat-8.5.43

29-Feb-2020 12:51:52.143 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\javaxx\javatool\apache-tomcat-8.5.43\temp

29-Feb-2020 12:51:52.143 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0].

29-Feb-2020 12:51:52.143 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].

29-Feb-2020 12:51:52.143 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]

29-Feb-2020 12:51:52.148 信息 [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019]

29-Feb-2020 12:51:52.280 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]

29-Feb-2020 12:51:52.296 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read

29-Feb-2020 12:51:52.306 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]

29-Feb-2020 12:51:52.309 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read

29-Feb-2020 12:51:52.309 信息 [main] org.apache.catalina.startup.Catalina.load Initialization processed in 563 ms

29-Feb-2020 12:51:52.347 信息 [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]

29-Feb-2020 12:51:52.347 信息 [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.43

29-Feb-2020 12:51:52.359 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]

29-Feb-2020 12:51:52.371 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]

29-Feb-2020 12:51:52.375 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 65 ms

Connected to server

[2020-02-29 12:51:52,632] Artifact bms_web:war exploded: Artifact is being deployed, please wait...

29-Feb-2020 12:51:52.729 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.startup.SetContextPropertiesRule.begin [SetContextPropertiesRule]{Context} Setting property 'allowLinking' to 'true' did not find a matching property.

29-Feb-2020 12:51:55.068 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

29-Feb-2020 12:51:55.134 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file

29-Feb-2020 12:51:55.135 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors

[2020-02-29 12:51:55,161] Artifact bms_web:war exploded: Error during artifact deployment. See server log for details.

29-Feb-2020 12:52:02.375 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [D:\javaxx\javatool\apache-tomcat-8.5.43\webapps\manager]

29-Feb-2020 12:52:02.578 信息 [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

29-Feb-2020 12:52:02.616 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:\javaxx\javatool\apache-tomcat-8.5.43\webapps\manager] has finished in [241] ms


正在回答

登陆购买课程后可参与讨论,去登陆

5回答

同学,你好!你的理解是对的。不过也可以使用这个案例中的方式哈。

1、web.xml中的配置http://img1.sycdn.imooc.com//climg/5e5b9c96099a8b2313560394.jpg

2、Servlet中的代码http://img1.sycdn.imooc.com//climg/5e5b9cc0093b5ea715160396.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 慕沐8221787 提问者 #1
    好的,谢谢啦!
    2020-03-01 20:41:27
好帮手慕珊 2020-03-01 17:06:30

同学,你好!因为控制器Controller属于Spring管理的内容,所以它可以使用注解进行属性注入。

下面分别回答同学的几个问题:

1、Web体系就是我们二阶段学习的JSP、Servlet、过滤器和监听器等内容。Spring容器就是我们本阶段学习的内容,包括第一步介绍的内容,以及后面的SpringMVC都属于Spring体系

2、Spring不一定要在Web中应用,我们刚开始学习Spring的时候,代码和Web没有什么关系。如果是和Web一起应用,还是先启动Web内容,和以前没区别,然后在一些位置对spring配置文件进行提取。比如这个案例,老师就是通过在web.xml配置监听器来提取Spring配置文件,而在前面的SM案例中,是在Servlet的init()方法中提取的。

http://img1.sycdn.imooc.com//climg/5e5b7a020979e43911520322.jpg

3、没有绝对的依存关系,只是通过上述第2点来加载配置文件,然后在DAO、Service和Controller中对Spring进行使用

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕沐8221787 #1
    那在监听器中如果需要如果需要得到一个bean的实例,是不是也要向Servlet一样,先加载spring的配置文件,得到一个ApplicationContext实例,然后使用getBean方法?
    2020-03-01 18:14:09
好帮手慕珊 2020-03-01 10:03:35

同学,你好!监听器属于Servlet体系的内容,而@Component是Spring的内容,他们是两个不同的体系,在监听器中无法直接获得Spring中的bean

老师在Servlet中也是通过如下选中的代码获取bean的。不知道同学说的Servlet可以获取是怎么完成的,建议贴出来代码看一下

http://img1.sycdn.imooc.com//climg/5e5b1784094c7b1e13560580.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕沐8221787 #1
    我写错了,我的意思是控制器(controller)可以通过注解来注入属性,而监听器为什么不行。还有几个问题请教一下:1、Web容器和Spring容器分别包含哪些东西?2、这两个容器在项目启动时,加载的先后顺序是什么样的,分别加载哪些内容?3、这两个容器之间存在什么样的依存关系?
    2020-03-01 10:45:35
好帮手慕阿满 2020-02-29 19:14:12

同学你好,报错提示是空指针异常

http://img1.sycdn.imooc.com//climg/5e5a42c709ff3c7206850129.jpg

问一下同学第18行是指查询的分类的一行吗?

可能是注入categoryService为null,检查同学检查一下。

祝:学习愉快~


  • 提问者 慕沐8221787 #1
    我也知道是注入为null,但是同样的方式在Servlet里注入是正常的,为什么到监听器就不行呢?是缺少什么配置还是什么原因?还有监听器需要@Component吗?
    2020-02-29 21:25:54
好帮手慕阿满 2020-02-29 17:04:04

同学你好,在贴出来的代码中,并没有看到具体的报错,建议同学查看控制台中后边两个窗口是否有报错,如:

http://img1.sycdn.imooc.com//climg/5e5a28fe096bf11906060194.jpg

祝:学习愉快~

  • 提问者 慕沐8221787 #1
    Server右边窗口提示如下错误信息: java.lang.NullPointerException at com.imooc.bms.global.CategoryListener.contextInitialized(CategoryListener.java:18) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4699) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5165) 截取部分信息
    2020-02-29 17:49:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师