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应用程序启动类

@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);
    }

}  
时间: 2024-11-03 22:31:35

springboot + ApplicationListener的相关文章

springboot SpringApplication 的十大特性

1.启动失败 如果你启动项目失败,你通过注册FailureAnalyzers 来获取错误信息和解决办法.比如你启动应用的8080端口被占用了,你将看到如下信息: *************************** APPLICATION FAILED TO START *************************** Description: Embedded servlet container failed to start. Port 8080 was already in use

springboot源码解析 - 构建SpringApplication

1 package com.microservice.framework; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class MySpringAplication { 8 9 public void run(Str

SpringBoot(1.5.6.RELEASE)源码解析(二)

上一篇分析了@SpringBootApplication注解,接下来从SpringApplication.run(Application.class, args);代码开始一行行DEBUG进行分析 1 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 2 public class Application { 3 public static void main(String[] args) { 4 Sprin

springboot启动过程(1)-初始化

1   springboot启动时,只需要调用一个类前面加了@SpringBootApplication的main函数,执行SpringApplication.run(DemoApplication.class, args)即可,这里初始化了一个SpringApplication实例,然后调用run启动springboot.run方法中,调用了initialize,如下 @SuppressWarnings({ "unchecked", "rawtypes" }) p

Springboot基础篇

Springboot可以说是当前最火的java框架了,非常适合于"微服务"思路的开发,大幅缩短软件开发周期. 概念 过去Spring充满了配置bean的xml文件,随着spring2.x和JDK1.5带来注解,配置大量减少,到了目前的Spring4.x和Spring Boot,更是推荐java配置的方式(这点和.NET很相似). 总体来说,Spring框架是轻量级企业开发一个完整解决方案,通过Ioc容器来管理pojo对象,其具有模块化的特点,基本的模块如下: 核心容器:Spring-C

SpringBoot启动流程

SpringBoot核心启动类的SpringApplication.从SpringApplication.run()开始先创建SpringApplication对象,并调用该对象的run方法. public static ConfigurableApplicationContext run(Object source, String... args) { return run(new Object[] { source }, args); } public static Configurable

Springboot启动源码详解

我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 从上面代码可以看出,Annotation定义(@SpringBootApplication)和类定义(SpringApplication.run)最

转载--springboot 开发入门,及问题汇总

原文地址:https://my.oschina.net/jkcui/blog/388400 1 . springboot简单介绍(http://projects.spring.io/spring-boot/) 现在的web项目几乎都会用到spring框架,而要使用spring难免需要配置大量的xml配置文件,而springboot的出现解   决了这一问题,一个项目甚至不用部署到服务器上直接开跑,真像springboot所说:"just run". springboot的很多默认编码方

&gt;&gt;&gt;&gt;&gt; 附3 springboot源码解析 - 构建SpringApplication

package com.microservice.framework; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MySpringAplication { public void run(String[] args) { Spri