关于监听器的问题
我想在项目启动时就加载图书分类,写了一个监听器,代码如下:
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
正在回答
同学,你好!你的理解是对的。不过也可以使用这个案例中的方式哈。
1、web.xml中的配置
2、Servlet中的代码
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
同学,你好!因为控制器Controller属于Spring管理的内容,所以它可以使用注解进行属性注入。
下面分别回答同学的几个问题:
1、Web体系就是我们二阶段学习的JSP、Servlet、过滤器和监听器等内容。Spring容器就是我们本阶段学习的内容,包括第一步介绍的内容,以及后面的SpringMVC都属于Spring体系
2、Spring不一定要在Web中应用,我们刚开始学习Spring的时候,代码和Web没有什么关系。如果是和Web一起应用,还是先启动Web内容,和以前没区别,然后在一些位置对spring配置文件进行提取。比如这个案例,老师就是通过在web.xml配置监听器来提取Spring配置文件,而在前面的SM案例中,是在Servlet的init()方法中提取的。
3、没有绝对的依存关系,只是通过上述第2点来加载配置文件,然后在DAO、Service和Controller中对Spring进行使用
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星