下面列出了怎么用org.springframework.boot.context.config.ConfigFileApplicationListener的API类实例代码及写法,或者点击链接到github查看源代码。
public static Properties parseSofaArkSupportProperties(String appName) {
Properties properties = new Properties();
String configsStr = System.getProperty(SOFA_ARK_CONFIGS);
if (configsStr != null) {
JSONObject configs = JSONObject.parseObject(configsStr);
String configDirStr = configs.getString("configDir");
// set CONFIG_ADDITIONAL_LOCATION_PROPERTY to ${configDir}/${appName}
if (configDirStr != null) {
Path configDir = Paths.get(configDirStr);
String appConfigDir = configDir.resolve(appName).toAbsolutePath().toUri().toString();
properties.setProperty(ConfigFileApplicationListener.CONFIG_ADDITIONAL_LOCATION_PROPERTY, appConfigDir);
}
}
String property = getProperty(appName + ".config-additional-location");
if (property != null) {
String oldValue = properties.getProperty(ConfigFileApplicationListener.CONFIG_ADDITIONAL_LOCATION_PROPERTY);
String newValue = oldValue != null ? (oldValue + "," + property) : property;
properties.setProperty(ConfigFileApplicationListener.CONFIG_ADDITIONAL_LOCATION_PROPERTY, newValue);
}
return properties;
}
private Set<String> addIncludedProfilesTo(Set<String> profiles,
PropertySource<?> propertySource) {
if (propertySource instanceof CompositePropertySource) {
for (PropertySource<?> nestedPropertySource : ((CompositePropertySource) propertySource)
.getPropertySources()) {
addIncludedProfilesTo(profiles, nestedPropertySource);
}
}
else {
Collections.addAll(profiles, getProfilesForValue(propertySource.getProperty(
ConfigFileApplicationListener.INCLUDE_PROFILES_PROPERTY)));
}
return profiles;
}
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 Environment findOne(String config, String profile, String label,
boolean includeOrigin) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class);
ConfigurableEnvironment environment = getEnvironment(profile);
builder.environment(environment);
builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF);
if (!logger.isDebugEnabled()) {
// Make the mini-application startup less verbose
builder.logStartupInfo(false);
}
String[] args = getArgs(config, profile, label);
// Explicitly set the listeners (to exclude logging listener which would change
// log levels in the caller)
builder.application()
.setListeners(Arrays.asList(new ConfigFileApplicationListener()));
try (ConfigurableApplicationContext context = builder.run(args)) {
environment.getPropertySources().remove("profiles");
return clean(new PassthruEnvironmentRepository(environment).findOne(config,
profile, label, includeOrigin));
}
catch (Exception e) {
String msg = String.format(
"Could not construct context for config=%s profile=%s label=%s includeOrigin=%b",
config, profile, label, includeOrigin);
String completeMessage = NestedExceptionUtils.buildMessage(msg,
NestedExceptionUtils.getMostSpecificCause(e));
throw new FailedToConstructEnvironmentException(completeMessage, e);
}
}
@Override
public int getOrder() {
// Apply after ConfigFileApplicationListener has called EnvironmentPostProcessors
return ConfigFileApplicationListener.DEFAULT_ORDER + 10;
}