springboot + ApplicationListener


ApplicationListener自定義偵聽器類


@Component
public class InstantiationTracingBeanPostProcessor implements
        ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOG = Logger.getLogger(InstantiationTracingBeanPostProcessor.class);
    private static boolean initialized;
    
    @Autowired
    private ManageResolver manageResolver;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            //只在初始化「根上下文」的時候執行
            final ApplicationContext app = event.getApplicationContext();
            if (null == app.getParent()
                    && ("Root WebApplicationContext".equals(app.getDisplayName())
                            || app.getDisplayName().contains("AnnotationConfigEmbeddedWebApplicationContext"))
                    && "/xweb".equals(app.getApplicationName())
                    ) { // 當存在父子容器時,此判斷頗有用
                LOG.info("*************:" + event.getSource());
                LOG.info("*************:" + app.getDisplayName());
                LOG.info("*************:" + app.getApplicationName());
                LOG.info("*************:" + app.getBeanDefinitionCount());
                LOG.info("*************:" + app.getEnvironment());
                LOG.info("*************:" + app.getParent());
                LOG.info("*************:" + app.getParentBeanFactory());
                LOG.info("*************:" + app.getId());
                LOG.info("*************:" + app.toString());
                LOG.info("*************:" + app);
                if(!initialized && !manageResolver.IsInitialCompleted()) {
                    manageResolver.initLater();
                    initialized = true;
                }
            }
        } catch (Exception e) {
            LOG.error("((XmlWebApplicationContext) event.getSource()).getDisplayName() 執行失敗,請檢查Spring版本是否支持");
        }
    }

}

SpringBoot應用程序啓動類web


@SpringBootApplication
@ImportResource({"classpath:config/applicationContext-xweb-dubbo.xml","classpath:config/applicationContext-xweb.xml"})
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, RedisAutoConfiguration.class})
public class XwebApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication springApplication =new SpringApplication(XwebApplication.class);
        springApplication.addListeners(new InstantiationTracingBeanPostProcessor());
        springApplication.run(args);
    }  
    
    /**
     * 上傳附件容量限制
     * @return
     */
    @Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("102400KB");  
        factory.setMaxRequestSize("112400KB");  
        return factory.createMultipartConfig();  
    } 
    
    /**
     * 配置攔截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多個攔截器組成一個攔截器鏈
        // addPathPatterns 用於添加攔截規則
        // excludePathPatterns 用戶排除攔截
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
    
}  
相關文章
相關標籤/搜索