@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
@ServletComponentScan
publicclassApp{
publicstaticvoidmain(String[]args){
SpringApplication.run(App.class,args);
}
/**
*整合Servlet方法二
*注册Servlet
*/
@Bean
publicServletRegistrationBeangetServletRegistrationBean(){
ServletRegistrationBeanbean=newServletRegistrationBean(newSecondServlet());
bean.addUrlMappings("/second.do");
returnbean;
}
/**
*整合Filter方法二
*注册Filter
*/
@Bean
publicFilterRegistrationBeangetFilterRegistrationBean(){
FilterRegistrationBeanfilter=newFilterRegistrationBean(newSecondFilter());
filter.addUrlPatterns("/second.do");
returnfilter;
}
/**
*整合Listener方法二
*注册Listener
*/
@Bean
publicServletListenerRegistrationBean<SecondListener>getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener>lis=newServletListenerRegistrationBean<SecondListener>(newSecondListener());
returnlis;
}
}
/**
*SpringBoot整合Listener方式一
*
*@authorwanglu
*/
@WebListener
publicclassFirstListenerimplementsServletContextListener{
@Override
publicvoidcontextInitialized(ServletContextEventsce){
System.out.println("FirstListenerstarting.................................");
System.out.println("FirstListenerstarted..................................");
}
@Override
publicvoidcontextDestroyed(ServletContextEventsce){
}
}
/**
*SpringBoot整合Listener方式二
*
*@authorwanglu
*/
publicclassSecondListenerimplementsServletContextListener{
@Override
publicvoidcontextInitialized(ServletContextEventsce){
System.out.println("SecondListenerstarting.................................");
System.out.println("SecondListenerstarted..................................");
}
@Override
publicvoidcontextDestroyed(ServletContextEventsce){
}
}
启动时控制台打印结果:
总结:
从打印结果可以看出,方法注册的SeconListener优先于注解注册FirstListener启动,且分别测试Servlet和Filter也是如此,故此可以得出结论:
方法注册优先于注解注册启动。