NutzCN Logo
问答 关于Listener的nutzIoc注入问题
发布于 2791天前 作者 蛋蛋的忧伤 2266 次浏览 复制 上一个帖子 下一个帖子
标签:

我写了 个Listener,我想在里面得到Dao对象,然后我写了如下代码

@IocBean
public class TomcatListener implements ServletContextListener{

	@Inject
	Dao dao;
	
	//tomcat关闭前触发
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("Tomcat destroyed...");
	}

	//Servlet启动后触发
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("Tomcat init...");
		
	}

}

不管是在tomcat启动时,还是关闭时,dao都是为null,注入失败.请问应该如何做?

14 回复

private Dao dao= Mvcs.ctx().getDefaultIoc().get(Dao.class);

我这么写了,然后在tomcat启动时报错了,貌似nutz还没加载,所以空指针了

public class TomcatListener implements ServletContextListener{

	private Dao dao= Mvcs.ctx().getDefaultIoc().get(Dao.class);
	
	//tomcat关闭前触发
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("Tomcat destroyed...");
		//dao.update("user",Chain.make("status", "hide"),Cnd.where("1","=",1));
	}

	//Servlet启动后触发
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("Tomcat init...");
	}

}

报错信息如下:

五月 03, 2017 1:41:15 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mychat' did not find a matching property.
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version:        Apache Tomcat/8.0.36
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built:          Jun 9 2016 13:55:50 UTC
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number:         8.0.36.0
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name:               Windows 10
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version:            10.0
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture:          amd64
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home:             D:\java\jre
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version:           1.8.0_91-b15
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor:            Oracle Corporation
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE:         D:\apache-tomcat-8.0.36
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME:         D:\apache-tomcat-8.0.36
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:56687
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=D:\apache-tomcat-8.0.36
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=D:\apache-tomcat-8.0.36
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=D:\apache-tomcat-8.0.36\webapps
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=D:\apache-tomcat-8.0.36\endorsed
五月 03, 2017 1:41:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=UTF-8
五月 03, 2017 1:41:15 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\java\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\java\jre\bin;D:/java/jre/bin/server;D:/java/jre/bin;D:/java/jre/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\java\jdk\bin;F:\?????????\eclipse;;.
五月 03, 2017 1:41:16 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
五月 03, 2017 1:41:16 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
五月 03, 2017 1:41:16 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-nio-8009"]
五月 03, 2017 1:41:16 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
五月 03, 2017 1:41:16 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1095 ms
五月 03, 2017 1:41:16 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
五月 03, 2017 1:41:16 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.0.36
五月 03, 2017 1:41:17 下午 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.
socket初始化成功...2
五月 03, 2017 1:41:18 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Error configuring application listener of class com.mychat.listener.TomcatListener
java.lang.NullPointerException
	at com.mychat.listener.TomcatListener.<init>(TomcatListener.java:15)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4764)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

五月 03, 2017 1:41:18 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Skipped installing application listeners due to previous error(s)
五月 03, 2017 1:41:18 下午 org.apache.catalina.core.StandardContext startInternal
严重: One or more listeners failed to start. Full details will be found in the appropriate container log file
五月 03, 2017 1:41:18 下午 org.apache.catalina.core.StandardContext startInternal
严重: Context [/mychat] startup failed due to previous errors
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\docs
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\docs has finished in 53 ms
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\examples
五月 03, 2017 1:41:18 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
五月 03, 2017 1:41:18 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\examples has finished in 362 ms
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\host-manager
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\host-manager has finished in 41 ms
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\manager
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\manager has finished in 44 ms
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\ROOT
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\ROOT has finished in 31 ms
五月 03, 2017 1:41:18 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
五月 03, 2017 1:41:18 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
五月 03, 2017 1:41:18 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2440 ms

public class TomcatListener implements ServletContextListener{

	private Dao dao;
	
	//tomcat关闭前触发
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("Tomcat destroyed...");
		//dao.update("user",Chain.make("status", "hide"),Cnd.where("1","=",1));
	}

	//Servlet启动后触发
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
    dao = Mvcs.ctx().getDefaultIoc().get(Dao.class);
		System.out.println("Tomcat init...");
	}

}

按照你说的写法,tomcat启动时报了如下错误:

五月 03, 2017 1:49:46 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mychat' did not find a matching property.
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version:        Apache Tomcat/8.0.36
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built:          Jun 9 2016 13:55:50 UTC
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number:         8.0.36.0
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name:               Windows 10
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version:            10.0
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture:          amd64
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home:             D:\java\jre
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version:           1.8.0_91-b15
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor:            Oracle Corporation
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE:         D:\apache-tomcat-8.0.36
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME:         D:\apache-tomcat-8.0.36
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=D:\apache-tomcat-8.0.36
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=D:\apache-tomcat-8.0.36
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=D:\apache-tomcat-8.0.36\webapps
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=D:\apache-tomcat-8.0.36\endorsed
五月 03, 2017 1:49:46 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=UTF-8
五月 03, 2017 1:49:46 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\java\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/java/jre/bin/server;D:/java/jre/bin;D:/java/jre/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\java\jdk\bin;F:\开发工具包\eclipse;;.
五月 03, 2017 1:49:46 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
五月 03, 2017 1:49:47 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
五月 03, 2017 1:49:47 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-nio-8009"]
五月 03, 2017 1:49:47 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
五月 03, 2017 1:49:47 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 824 ms
五月 03, 2017 1:49:47 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
五月 03, 2017 1:49:47 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.0.36
五月 03, 2017 1:49:49 下午 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.
socket初始化成功...2
五月 03, 2017 1:49:49 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class com.mychat.listener.TomcatListener
java.lang.NullPointerException
	at com.mychat.listener.TomcatListener.contextInitialized(TomcatListener.java:27)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

五月 03, 2017 1:49:49 下午 org.apache.catalina.core.StandardContext startInternal
严重: One or more listeners failed to start. Full details will be found in the appropriate container log file
五月 03, 2017 1:49:49 下午 org.apache.catalina.core.StandardContext startInternal
严重: Context [/mychat] startup failed due to previous errors
Tomcat destroyed...
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\docs
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\docs has finished in 39 ms
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\examples
五月 03, 2017 1:49:49 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
五月 03, 2017 1:49:49 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\examples has finished in 363 ms
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\host-manager
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\host-manager has finished in 50 ms
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\manager
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\manager has finished in 43 ms
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.36\webapps\ROOT
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.36\webapps\ROOT has finished in 28 ms
五月 03, 2017 1:49:49 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
五月 03, 2017 1:49:49 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
五月 03, 2017 1:49:49 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2755 ms

老大啊,报空指针啊= =该咋办呀

话说,

contextInitialized的时候, NutFilter还没初始化

而contextDestroyed的时候, NutFilter已经销毁

拿到与否都死

只是为了记录点东西?? 用Setup就好啦

我的需求是关闭服务器时,我的所有用户表的status都置为offline

Setup可否在tomcat关闭时执行?或者说tomcat死机时...

关闭的时候会触发

死机的话, jvm都挂了,你还能做啥?

怕断电,请购买UPS

啊哈哈,涨姿势了.你个盗版tomcat~

添加回复
请先登陆
回到顶部