未调用Spring Boot应用程序侦听器

时间:2018-03-31 00:24:27

标签: java spring spring-boot

为什么我的应用程序监听器没有被调用?

@SpringBootApplication
public class Application
{
    @Component
    public static class T implements ApplicationListener<ContextRefreshedEvent>
    {
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event)
        {
            System.out.println( "test" );
        }
    }

    public static void main(String[] args) throws UnknownHostException
    {
        SpringApplication application = new SpringApplication( Application.class );
        ConfigurableApplicationContext run = application.run( args );
    }
}

我在该方法中设置了一个断点并且它从未到达过。我在日志中看到:

  

2018-03-30 20:20:57.496 INFO [bootstrap ,,,] 84538 --- [main] ationConfigEmbeddedWebApplicationContext:刷新org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ce9e05a:启动日期[Fri Mar 30美国东部时间2018年20:20:57]; parent:org.springframework.context.annotation.AnnotationConfigApplicationContext@78383390

1 个答案:

答案 0 :(得分:0)

原因: 您需要将以下内容添加到pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>