Spring MVC应用程序在tomcat中多次部署

时间:2018-03-15 19:47:09

标签: java spring spring-mvc spring-annotations spring-web

我有弹簧mvc注释配置的应用程序。在我的本地环境(IDE)中,它运行良好。当我在prod(tomcat 8.5)中部署时,我的应用程序监听器多次调用。由于这是我的执行程序任务,调度程序多次调用。

AppWebAppInitializer.java

package com.app.config;

public class AppWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppMvcConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("appServlet",new DispatcherServlet(ctx));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
        dispatcher.setAsyncSupported(true);
        servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE)); //uses cookies instead of jsessionId in the url.

    }
}

AppMvcConfig.java

package com.app.config;

@Configuration
@EnableWebMvc
@EnableScheduling
@EnableCaching
@ComponentScan("com.app")
@Import({
        AppPropertyInitConfig.class,
        AppSecurityConfig.class,
        WebSocketConfig.class
})
public class AppMvcConfig  extends WebMvcConfigurerAdapter implements ApplicationContextAware{

    private ApplicationContext applicationContext;


    @Value("${app.upload.dir}")
    private String uploadDir;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

ApplicationStartupListener.java

package com.app.listener;

@Component
public class ApplicationStartupListener {

    private final Logger logger = LoggerFactory.getLogger(ApplicationStartupListener.class);

    @Autowired
    private LookupHolder lookupHolder;

    @EventListener
   public void handleContextRefreshEvent(ContextRefreshedEvent event) {
        logger.info("ApplicationStartupListener initializing lookups");

        Runnable lookupInitRunnable = () -> lookupHolder.init(); //initialize the lookups
        new Thread(lookupInitRunnable).start();

   }
}

在上面的代码中ApplicationStartupListener.java - > handleContextRefreshEvent()多次调用。以及我的春季默认时间表也多次调用

在我的应用程序中,我没有web.xml

0 个答案:

没有答案