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

The following examples show how to use org.springframework.boot.context.event.ApplicationStartingEvent. 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: LoggingEnvironmentApplicationListener.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
    }
}
 
Example #2
Source File: WallRideInitializer.java    From wallride with Apache License 2.0 6 votes vote down vote up
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;
	}
 
Example #3
Source File: WallRideInitializer.java    From wallride with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: LoggingEnvironmentApplicationListener.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationStartingEvent) {
        onApplicationStartingEvent((ApplicationStartingEvent) event);
    }
    else if (event instanceof ApplicationEnvironmentPreparedEvent) {
        onApplicationEnvironmentPreparedEvent(
                (ApplicationEnvironmentPreparedEvent) event);
    }
}
 
Example #5
Source File: ConfigSourceListener.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
            }
        }
    }
}
 
Example #6
Source File: ApplicationStartedEventListenerBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
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 应用上下文
}
 
Example #7
Source File: WallRideInitializer.java    From wallride with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: ApplicationStartingEventListener.java    From seed with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
    System.out.println("SpringBoot开始启动-->" + event.getSpringApplication());
}
 
Example #9
Source File: WallRideInitializer.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
	event.getSpringApplication().setEnvironment(createEnvironment(event));
	event.getSpringApplication().setResourceLoader(createResourceLoader());
}