java - 为什么方法 contextInitialized 不调用?

标签 java spring servlets spring-mvc listener

我编写了以下 java 类:

@Configuration
public class UiConfig {


    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
    @Bean
    public AppListener getAppListener(){
        AppListener appListener = new AppListener();
        System.out.println();
        System.out.println();
        System.out.println(" appListener method ");
        System.out.println();
        System.out.println();
        return appListener;
    }
    @Bean
    public MyListener getMyListener(){
        MyListener myListener = new MyListener();
        System.out.println();
        System.out.println();
        System.out.println(" myListener method ");
        System.out.println();
        System.out.println();
        return myListener;
    }
}
class MyListener extends ContextLoaderListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println();
        System.out.println();
        System.out.println(" hello ContextLoaderListener ");
        System.out.println();
        System.out.println();
    }
}
class AppListener implements ApplicationListener<ContextRefreshedEvent> {

        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            System.out.println();
            System.out.println();
            System.out.println(" hello AppListener");
            System.out.println();
            System.out.println();
        }

    }

我运行我的 tomcat 并查看下一个日志:

21.10.2013 18:56:18 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in

...

org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,messageSource,getAppListener,getMyListener]; root of factory hierarchy


 appListener method 




 myListener method 




 hello AppListener


INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 4424 ms
21.10.2013 18:56:34 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appServlet'

...

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/updateVacancy],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.epam.hhsystem.web.controllers.VacancyMenuController.updateVacancyPRG(org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes,java.lang.Integer)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/addVacancy],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.epam.hhsystem.web.controllers.VacancyMenuController.addVacancy(com.epam.hhsystem.model.vacancy.Vacancy,org.springframework.validation.BindingResult,org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'


 appListener method 




 myListener method 




 hello AppListener




 hello AppListener


INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 926 ms
21.10.2013 18:56:34 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
21.10.2013 18:56:34 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
21.10.2013 18:56:34 org.apache.catalina.startup.Catalina start
INFO: Server startup in 15692 ms
INFO : com.epam.hhsystem.web.controllers.CustomAuthentificationProvider - User with name 'Nikolay_tkachev' log in
/index
21.10.2013 19:01:01 org.apache.jasper.compiler.TldLocationsCache tldScanJar
INFO: 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.
21.10.2013 19:01:41 org.apache.catalina.core.StandardServer await
INFO: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
21.10.2013 19:01:41 org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8080"]
21.10.2013 19:01:41 org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-bio-8009"]
21.10.2013 19:01:41 org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
21.10.2013 19:01:41 org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'appServlet'
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Closing WebApplicationContext for namespace 'appServlet-servlet': startup date [Mon Oct 21 18:56:34 MSK 2013]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5dd22889: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,baseMenuController,candidateMenuController,provider,eventMenuController,homeController,uiConfig,vacancyMenuController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,messageSource,getAppListener,getMyListener]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@19a072a6
21.10.2013 19:01:41 org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Closing Root WebApplicationContext: startup date [Mon Oct 21 18:56:29 MSK 2013]; root of context hierarchy

....

21.10.2013 19:01:41 org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-bio-8080"]
21.10.2013 19:01:41 org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-bio-8009"]
21.10.2013 19:01:41 org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-bio-8080"]
21.10.2013 19:01:41 org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-bio-8009"]

我不明白为什么我在此日志中看不到字符串 "hello ContextLoaderListener " ? 你能解释一下吗?

附注

在 web.xml 中我有这个条目

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

最佳答案

Why method contextInitialized doesn't invoke?

你的类(class)

class MyListener extends ContextLoaderListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println();
        System.out.println();
        System.out.println(" hello ContextLoaderListener ");
        System.out.println();
        System.out.println();
    }
}

ServletContextListener尚未在 Servlet 注册负责调用contextInitialized的容器。因此,该方法永远不会被调用。

一个ServletContextListener通常会在容器的 web.xml 中注册与 <listener>条目。

关于java - 为什么方法 contextInitialized 不调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19498187/

相关文章:

spring - jersey-spring3 实例化 Spring 管理的 bean (null!)

Spring 和多模块设置

java - 选择值时 hibernate 出错

java - 更有效地存储(字符串,整数)元组并应用二分搜索

java - Selendroid 尝试启动 session 时进入无限循环

java - com.sun.jdi.InvocationException 发生调用方法

spring - 注册过滤器时 "addMappingForServletNames()"的含义是什么?

java - 在Servlet内部调用RestFul Web Service

java - Java 中的多线程 FTP 服务器

java - 使用SingleThreadModel时servlet的生命周期管理