org.springframework.boot.context.event.ApplicationPreparedEvent Java Examples

The following examples show how to use org.springframework.boot.context.event.ApplicationPreparedEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EmbeddedRedisConfiguration.java    From jetlinks-community with Apache License 2.0 7 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
    Environment environment = event.getApplicationContext().getEnvironment();
    if (!environment.getProperty("spring.redis.embedded.enabled", Boolean.class, false)) {
        return;
    }

    String dataPath = environment.getProperty("spring.redis.embedded.data-path", "./data/redis");
    new File(dataPath).mkdirs();
    DBConfig config = new DBConfig();
    config.setPersistenceActive(true);
    config.setAofFile(dataPath.concat("/jetlinks.aof"));
    config.setRdbFile(dataPath.concat("/jetlinks.rdb"));

    RespServer server = ClauDB.builder()
        .port(environment.getProperty("spring.redis.embedded.port", Integer.class, 6379))
        .host(environment.getProperty("spring.redis.embedded.host", "0.0.0.0"))
        .config(config)
        .build();
    server.start();

}
 
Example #2
Source File: ApplicationEventListener.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        LOGGER.debug("初始化环境变量");
    } else if (event instanceof ApplicationPreparedEvent) {
        LOGGER.debug("初始化完成");
    } else if (event instanceof ContextRefreshedEvent) {
        LOGGER.debug("应用刷新");
    } else if (event instanceof ApplicationReadyEvent) {
        LOGGER.debug("应用已启动完成");
    } else if (event instanceof ContextStartedEvent) {
        LOGGER.debug("应用启动,需要在代码动态添加监听器才可捕获");
    } else if (event instanceof ContextStoppedEvent) {
        LOGGER.debug("应用停止");
    } else if (event instanceof ContextClosedEvent) {
        ApplicationContext applicationContext = ((ContextClosedEvent) event).getApplicationContext();
        grpcClient = applicationContext.getBean(GrpcClient.class);
        grpcClient.close();
        LOGGER.debug("应用关闭");
    }

}
 
Example #3
Source File: ApplicationEventListener.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        LOG.debug("初始化环境变量");
    } else if (event instanceof ApplicationPreparedEvent) {
        LOG.debug("初始化完成");
        LOG.debug("初始GameData策划数据");
        ConfigManager.loadGameData(this.configPath);
    } else if (event instanceof ContextRefreshedEvent) {
        LOG.debug("应用刷新");
    } else if (event instanceof ApplicationReadyEvent) {
        LOG.debug("应用已启动完成");
    } else if (event instanceof ContextStartedEvent) {
        LOG.debug("应用启动,需要在代码动态添加监听器才可捕获");
    } else if (event instanceof ContextStoppedEvent) {
        LOG.debug("应用停止");
    } else if (event instanceof ContextClosedEvent) {
        ApplicationContext context = ((ContextClosedEvent) event).getApplicationContext();
        rpcClient = context.getBean(RpcClient.class);
        rpcClient.close();
        LOG.error("应用关闭");
    } else {

    }
}
 
Example #4
Source File: ApplicationEventListener.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        LOG.debug("初始化环境变量");
    } else if (event instanceof ApplicationPreparedEvent) {
        LOG.debug("初始化完成");
        LOG.debug("初始GameData策划数据");
        String path = ApplicationEventListener.class.getResource("/gamedata").getFile();
        ConfigManager.loadGameData(path);
    } else if (event instanceof ContextRefreshedEvent) {
        LOG.debug("应用刷新");
    } else if (event instanceof ApplicationReadyEvent) {
        LOG.debug("应用已启动完成");
    } else if (event instanceof ContextStartedEvent) {
        LOG.debug("应用启动,需要在代码动态添加监听器才可捕获");
    } else if (event instanceof ContextStoppedEvent) {
        LOG.debug("应用停止");
    } else if (event instanceof ContextClosedEvent) {
        ApplicationContext applicationContext = ((ContextClosedEvent) event).getApplicationContext();
        grpcClient = applicationContext.getBean(GrpcClient.class);
        grpcClient.close();
        LOG.debug("应用关闭");
    }
}
 
Example #5
Source File: RestartListener.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent input) {
	if (input instanceof ApplicationPreparedEvent) {
		this.event = (ApplicationPreparedEvent) input;
		if (this.context == null) {
			this.context = this.event.getApplicationContext();
		}
	}
	else if (input instanceof ContextRefreshedEvent) {
		if (this.context != null && input.getSource().equals(this.context)
				&& this.event != null) {
			this.context.publishEvent(this.event);
		}
	}
	else {
		if (this.context != null && input.getSource().equals(this.context)) {
			this.context = null;
			this.event = null;
		}
	}
}
 
Example #6
Source File: ApplicationEventListener.java    From SpringBoot-Base-System with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 应用程序启动过程监听
 * 
 * @time 2018年4月10日 下午5:05:33
 * @version V1.0
 * @param event
 */
@Override
public void onApplicationEvent(ApplicationEvent event) {
	// 在这里可以监听到Spring Boot的生命周期
	if (event instanceof ApplicationEnvironmentPreparedEvent) { // 初始化环境变量
		log.debug("初始化环境变量");
	} else if (event instanceof ApplicationPreparedEvent) { // 初始化完成
		log.debug("初始化环境变量完成");
	} else if (event instanceof ContextRefreshedEvent) { // 应用刷新,当ApplicationContext初始化或者刷新时触发该事件。
		log.debug("应用刷新");
	} else if (event instanceof ApplicationReadyEvent) {// 应用已启动完成
		log.debug("应用已启动完成");
	} else if (event instanceof ContextStartedEvent) { // 应用启动,Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的
														// Start()方法开始/重新开始容器时触发该事件。
		log.debug("应用启动");
	} else if (event instanceof ContextStoppedEvent) { // 应用停止,Spring2.5新增的事件,当容器调用ConfigurableApplicationContext
														// 的Stop()方法停止容器时触发该事件。
		log.debug("应用停止");
	} else if (event instanceof ContextClosedEvent) { // 应用关闭,当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有
														// 单例Bean都被销毁。
		log.debug("应用关闭");
	} else {
	}
}
 
Example #7
Source File: JobLoggingApplicationListener.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
	String jobLogPath = event.getApplicationContext().getEnvironment().getProperty("batch.joblog.path");
	if (!StringUtils.isEmpty(jobLogPath)) {
		if (!jobLogPath.endsWith(File.separator)) {
			jobLogPath = jobLogPath + File.separator;
		}
		System.setProperty("JOB_LOG_PATH", jobLogPath);
	}
}
 
Example #8
Source File: SpringBootEnvironmentListener.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
    Environment environment = event.getApplicationContext().getEnvironment();
    if (isInit.compareAndSet(false, true)) {
        setManagerConfig(environment);
    }
}
 
Example #9
Source File: CloudConfigApplicationListener.java    From kork with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
  ConfigurableApplicationContext context = event.getApplicationContext();
  ConfigurableEnvironment environment = context.getEnvironment();

  for (PropertySource propertySource : environment.getPropertySources()) {
    if (shouldWrap(propertySource)) {
      CloudConfigAwarePropertySource wrapper =
          new CloudConfigAwarePropertySource(propertySource, context);
      environment.getPropertySources().replace(propertySource.getName(), wrapper);
    }
  }
}
 
Example #10
Source File: RestartEndpoint.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent input) {
	this.event = input;
	if (this.context == null) {
		this.context = this.event.getApplicationContext();
		this.args = this.event.getArgs();
		this.application = this.event.getSpringApplication();
		this.application.addInitializers(new PostProcessorInitializer());
	}
}
 
Example #11
Source File: RestartListener.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
	return ApplicationPreparedEvent.class.isAssignableFrom(eventType)
			|| ContextRefreshedEvent.class.isAssignableFrom(eventType)
			|| ContextClosedEvent.class.isAssignableFrom(eventType);

}
 
Example #12
Source File: PreparedEventApplicationListener.java    From micro-service with MIT License 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
	
	ConfigurableApplicationContext context = event.getApplicationContext();
	getApplicationContext(context);
	
	logger.info("3 上下文创建完成, PreparedEventApplicationListener...");
}
 
Example #13
Source File: OsiamHome.java    From osiam with MIT License 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment();
        configure(environment);
        if(shouldInitializeHome) {
            initialize();
        }
    } else if (event instanceof ApplicationPreparedEvent) {
        writeLogs();
    }
}
 
Example #14
Source File: ClassDefinitionModifier.java    From spring-boot-practice with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationPreparedEvent) {
        Environment environment = ((ApplicationPreparedEvent) event).getApplicationContext().getEnvironment();
        try {
            log.info("Modify classes");
            modifyClasses(environment);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example #15
Source File: ConfigServerBootstrapApplicationListener.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
    ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
    if (StringUtils.hasText(environment.resolvePlaceholders("${dm.config.git.uri:}"))) {
        if (!environment.getPropertySources().contains(this.propertySource.getName())) {
            environment.getPropertySources().addLast(this.propertySource);
        }
    }
}
 
Example #16
Source File: DubboHolderListener.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationPreparedEvent) {
        if (running == Boolean.FALSE)
            running = Boolean.TRUE;
        if (holdThread == null) {
            holdThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    if (LOGGER.isTraceEnabled()) {
                        LOGGER.trace(Thread.currentThread().getName());
                    }
                    while (running && !Thread.currentThread().isInterrupted()) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }, "Dubbo-Holder");
            holdThread.setDaemon(false);
            holdThread.start();
        }
    }
    if (event instanceof ContextClosedEvent) {
        running = Boolean.FALSE;
        if (null != holdThread) {
            holdThread.interrupt();
            holdThread = null;
        }
    }
}
 
Example #17
Source File: Application.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
private static SpringApplicationBuilder doConfigure(SpringApplicationBuilder application) {
    // Handle HSQLDB database upgrades from 1.8 to 2.x before any beans are started.
    application.application().addListeners((ApplicationListener<ApplicationPreparedEvent>) event -> {
        if (event.getApplicationContext().getEnvironment().acceptsProfiles("legacy")) {
            LegacyHsqlUtil.upgradeHsqldbDatabaseSafely();
        }
    });

    // Customize the application or call application.sources(...) to add sources
    // Since our example is itself a @Configuration class (via @SpringBootApplication)
    // we actually don't need to override this method.
    return application.sources(Application.class).web(WebApplicationType.SERVLET);
}
 
Example #18
Source File: ExtendPropertySourcesApplicationListener.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        onApplicationEvent((ApplicationEnvironmentPreparedEvent) event);
    } else if (event instanceof ApplicationPreparedEvent) {
        onApplicationEvent((ApplicationPreparedEvent) event);
    }
}
 
Example #19
Source File: ExtendPropertySourcesApplicationListener.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
public void onApplicationEvent(ApplicationPreparedEvent event) {
    // 从事件获取 Environment 对象
    ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
    Map<String, Object> source = new HashMap<>();
    // 内部化配置设置 user.name 属性
    source.put("user.name", "马昕曦(小马哥)");
    MapPropertySource propertySource = new MapPropertySource("ApplicationPreparedEvent", source);
    // 添加至最高优先级
    environment.getPropertySources().addFirst(propertySource);
}
 
Example #20
Source File: ApplicationPreparedEventListener.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
    // 获取 Spring 应用上下文
    ConfigurableApplicationContext context = event.getApplicationContext();
    // 调整 Spring 应用上下文的 ID
    context.setId("context-mercyblitz");
    System.out.println("当前 Spring 应用上下文 ID 调整为:" + context.getId());
}
 
Example #21
Source File: DSSSpringApplication.java    From DataSphereStudio with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    RuntimeDelegate.setInstance(new org.glassfish.jersey.internal.RuntimeDelegateImpl());
    final SpringApplication application = new SpringApplication(DSSSpringApplication.class);
    application.addListeners(new ApplicationListener<ApplicationPreparedEvent>(){
        public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent) {
            logger.info("add config from config server...");
            if(applicationContext == null) {
                applicationContext = applicationPreparedEvent.getApplicationContext();
                try {
                    setApplicationContext(applicationContext);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
            addRemoteConfig();
            logger.info("initialize DataWorkCloud spring application...");
            initDWCApplication();
        }
    });
    application.addListeners(new ApplicationListener<RefreshScopeRefreshedEvent>() {
        public void onApplicationEvent(RefreshScopeRefreshedEvent applicationEvent) {
            logger.info("refresh config from config server...");
            updateRemoteConfig();
        }
    });
    String listeners = ServerConfiguration.BDP_SERVER_SPRING_APPLICATION_LISTENERS().getValue();
    if(StringUtils.isNotBlank(listeners)) {
        for (String listener : listeners.split(",")) {
            application.addListeners((ApplicationListener<?>) Class.forName(listener).newInstance());
        }
    }
    applicationContext = application.run(args);
    setApplicationContext(applicationContext);
}
 
Example #22
Source File: CfEnvironmentPostProcessor.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ApplicationPreparedEvent) {
		DEFERRED_LOG
				.switchTo(CfEnvironmentPostProcessor.class);
	}
}
 
Example #23
Source File: CfDataSourceEnvironmentPostProcessor.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ApplicationPreparedEvent) {
		DEFERRED_LOG.switchTo(CfDataSourceEnvironmentPostProcessor.class);
	}

}
 
Example #24
Source File: WireMockApplicationListener.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
	registerPort(event.getApplicationContext().getEnvironment());
}
 
Example #25
Source File: ApplicationPreparedEventListener.java    From seed with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
    ConfigurableApplicationContext cac = event.getApplicationContext();
    System.out.println("SpringBoot上下文Context创建完成,但此时Spring中的Bean是没有完全加载完成的,得到ApplicationContext-->" + cac.getDisplayName());
}
 
Example #26
Source File: ApplicationStartup.java    From spring-boot-aws-mock with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
    ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
    environment.setRequiredProperties("sqs.mock.port=20000");
}
 
Example #27
Source File: ExtendPropertySourcesApplicationListener.java    From thinking-in-spring-boot-samples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
    return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType)
            || ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}