下面列出了怎么用org.springframework.boot.context.event.ApplicationStartingEvent的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* deducing groupId, artifactId
*
* @param event
*/
private void onApplicationStartingEvent(ApplicationStartingEvent event) {
if (ClassUtils.isPresent("ch.qos.logback.core.Appender",
event.getSpringApplication().getClassLoader())) {
// base package
Class<?> mainClass = event.getSpringApplication().getMainApplicationClass();
if (mainClass != null) {
String basePackage = mainClass.getPackage().getName();
System.setProperty("BASE_PACKAGE", basePackage);
} else {
System.setProperty("BASE_PACKAGE", "");
logger.warn("can not set BASE_PACKAGE correctly");
}
// set logging system impl
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, FormulaLogbackSystem.class.getName());
}
}
private static String getConfigFileHome(ApplicationStartingEvent event) {
File configFile = getConfigFileFromWebroot(event);
if (configFile!=null) {
Properties properties = getProperties(configFile);
if (properties.getProperty(WallRideProperties.HOME_PROPERTY) != null) {
String home = properties.getProperty(WallRideProperties.HOME_PROPERTY);
if (!home.startsWith("file:")) {
home = "file:"+home;
}
return home;
} else {
throw new IllegalStateException(WallRideProperties.HOME_PROPERTY + " not found in config file " + configFile.getAbsolutePath());
}
}
return null;
}
/**
* Try to find a config file where the wallride.home parameter is configured
* Config file must be placed under the webroot directory and can be named wallride.conf or webroot-name.conf
* Example: if webroot is /srv/webapps/myblog the config file can be /srv/webapps/wallride.conf or /srv/webapps/myblog.conf
* @param event
* @return
*/
private static File getConfigFileFromWebroot(ApplicationStartingEvent event) {
URL resource = event.getClass().getClassLoader().getResource("");
File classPath = new File(resource.getPath()); // ROOT/WEB-INF/classes/
File webInfPath = classPath.getParentFile(); // ROOT/WEB-INF/
if (webInfPath.getName().equalsIgnoreCase("WEB-INF")) {
File rootPath = webInfPath.getParentFile(); // ROOT/
File wallrideConfigFile = new File(rootPath.getParentFile(), "wallride.conf");
if (wallrideConfigFile.exists()) { return wallrideConfigFile; }
File configFile = new File(rootPath.getParentFile(), rootPath.getName()+".conf");
if (configFile.exists()) { return configFile; }
} else {
//there is no web-inf directory -> webroot can not be determined
}
return null;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationStartingEvent) {
onApplicationStartingEvent((ApplicationStartingEvent) event);
}
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent(
(ApplicationEnvironmentPreparedEvent) event);
}
}
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
if (!INIT.compareAndSet(false, true)) {
return;
}
RemoteConfigSource sources = event.getSpringApplication().getMainApplicationClass().getAnnotation(RemoteConfigSource.class);
if (sources != null) {
String configPath = Server.getInstance().getServerConfig().getBasePath() + "/conf/";
Path path = Paths.get(configPath);
File configDirectory = path.toFile();
if (configDirectory.isDirectory()) {
File[] files = configDirectory.listFiles();
if (files != null) {
for (File file : files) {
if (!file.delete()) {
throw new RuntimeException("[TARS] delete legacy config failed: " + file.getName());
}
}
}
}
for (String name : sources.value()) {
if (!ConfigHelper.getInstance().loadConfig(name)) {
throw new RuntimeException("[TARS] load config failed: " + name);
} else {
System.out.println("[TARS] load config: " + name);
}
}
}
}
public static void main(String[] args) {
new SpringApplicationBuilder(Object.class)
.listeners((ApplicationListener<ApplicationStartedEvent>) event -> {
System.out.println("监听 Spring Boot 事件 ApplicationStartedEvent");
}, (ApplicationListener<ApplicationStartingEvent>) event -> {
System.out.println("监听 Spring Boot 事件 ApplicationStartingEvent ");
})
.web(false) // 非 Web 应用
.run(args) // 运行 SpringApplication
.close(); // 关闭 Spring 应用上下文
}
public static ConfigurableEnvironment createEnvironment(ApplicationStartingEvent event) {
StandardEnvironment environment = new StandardEnvironment();
String home = environment.getProperty(WallRideProperties.HOME_PROPERTY);
if (!StringUtils.hasText(home)) {
//try to get config-File with wallride.home parameter under webroot
String configFileHome = getConfigFileHome(event);
if (configFileHome!=null) {
home = configFileHome;
} else {
throw new IllegalStateException(WallRideProperties.HOME_PROPERTY + " is empty");
}
}
if (!home.endsWith("/")) {
home = home + "/";
}
String config = home + WallRideProperties.DEFAULT_CONFIG_PATH_NAME;
String media = home + WallRideProperties.DEFAULT_MEDIA_PATH_NAME;
System.setProperty(WallRideProperties.CONFIG_LOCATION_PROPERTY, config);
System.setProperty(WallRideProperties.MEDIA_LOCATION_PROPERTY, media);
event.getSpringApplication().getListeners().stream()
.filter(listener -> listener.getClass().isAssignableFrom(ConfigFileApplicationListener.class))
.map(listener -> (ConfigFileApplicationListener) listener)
.forEach(listener -> listener.setSearchLocations(DEFAULT_CONFIG_SEARCH_LOCATIONS + "," + config));
return environment;
}
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
System.out.println("SpringBoot开始启动-->" + event.getSpringApplication());
}
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
event.getSpringApplication().setEnvironment(createEnvironment(event));
event.getSpringApplication().setResourceLoader(createResourceLoader());
}