下面列出了怎么用org.springframework.boot.context.event.ApplicationPreparedEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@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();
}
@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("应用关闭");
}
}
@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 {
}
}
@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("应用关闭");
}
}
/**
* 应用程序启动过程监听
*
* @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 {
}
}
@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;
}
}
}
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
Environment environment = event.getApplicationContext().getEnvironment();
if (isInit.compareAndSet(false, true)) {
setManagerConfig(environment);
}
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationPreparedEvent) {
DEFERRED_LOG.switchTo(CfDataSourceEnvironmentPostProcessor.class);
}
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationPreparedEvent) {
DEFERRED_LOG
.switchTo(CfEnvironmentPostProcessor.class);
}
}
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);
}
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
// 获取 Spring 应用上下文
ConfigurableApplicationContext context = event.getApplicationContext();
// 调整 Spring 应用上下文的 ID
context.setId("context-mercyblitz");
System.out.println("当前 Spring 应用上下文 ID 调整为:" + context.getId());
}
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);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEvent((ApplicationEnvironmentPreparedEvent) event);
} else if (event instanceof ApplicationPreparedEvent) {
onApplicationEvent((ApplicationPreparedEvent) event);
}
}
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);
}
@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;
}
}
}
@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);
}
}
}
@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);
}
}
}
@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();
}
}
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
ConfigurableApplicationContext context = event.getApplicationContext();
getApplicationContext(context);
logger.info("3 上下文创建完成, PreparedEventApplicationListener...");
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationPreparedEvent.class.isAssignableFrom(eventType)
|| ContextRefreshedEvent.class.isAssignableFrom(eventType)
|| ContextClosedEvent.class.isAssignableFrom(eventType);
}
@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());
}
}
@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);
}
}
}
@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);
}
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType)
|| ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
environment.setRequiredProperties("sqs.mock.port=20000");
}
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
ConfigurableApplicationContext cac = event.getApplicationContext();
System.out.println("SpringBoot上下文Context创建完成,但此时Spring中的Bean是没有完全加载完成的,得到ApplicationContext-->" + cac.getDisplayName());
}
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
registerPort(event.getApplicationContext().getEnvironment());
}