org.springframework.core.env.ConfigurableEnvironment#getActiveProfiles ( )源码实例Demo

下面列出了org.springframework.core.env.ConfigurableEnvironment#getActiveProfiles ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private String[] getActiveProfile(ConfigurableEnvironment appEnvironment) {
    List<String> serviceProfiles = new ArrayList<>();

    for (String profile : appEnvironment.getActiveProfiles()) {
        if (validLocalProfiles.contains(profile)) {
            serviceProfiles.add(profile);
        }
    }

    if (serviceProfiles.size() > 1) {
        throw new IllegalStateException("Only one active Spring profile may be set among the following: " +
                validLocalProfiles.toString() + ". " +
                "These profiles are active: [" +
                StringUtils.collectionToCommaDelimitedString(serviceProfiles) + "]");
    }

    if (serviceProfiles.size() > 0) {
        return createProfileNames(serviceProfiles.get(0), "local");
    }

    return null;
}
 
源代码2 项目: gazpachoquest   文件: EnviromentDiscovery.java
@Override
public void initialize(ConfigurableApplicationContext ctx) {
    ConfigurableEnvironment environment = ctx.getEnvironment();
    String activeProfiles[] = environment.getActiveProfiles();

    if (activeProfiles.length == 0) {
        environment.setActiveProfiles("test,db_hsql");
    }

    logger.info("Application running using profiles: {}", Arrays.toString(environment.getActiveProfiles()));

    String instanceInfoString = environment.getProperty(GAZPACHO_APP_KEY);

    String dbEngine = null;
    for (String profile : activeProfiles) {
        if (profile.startsWith("db_")) {
            dbEngine = profile;
            break;
        }
    }
    try {
        environment.getPropertySources().addLast(
                new ResourcePropertySource(String.format("classpath:/database/%s.properties", dbEngine)));
    } catch (IOException e) {
        throw new IllegalStateException(dbEngine + ".properties not found in classpath", e);
    }

    PropertySourcesPlaceholderConfigurer propertyHolder = new PropertySourcesPlaceholderConfigurer();

    Map<String, String> environmentProperties = parseInstanceInfo(instanceInfoString);
    if (!environmentProperties.isEmpty()) {
        logger.info("Overriding default properties with {}", instanceInfoString);
        Properties properties = new Properties();
        for (String key : environmentProperties.keySet()) {
            String value = environmentProperties.get(key);
            properties.put(key, value);
        }
        environment.getPropertySources().addLast(new PropertiesPropertySource("properties", properties));

        propertyHolder.setEnvironment(environment);
        // ctx.addBeanFactoryPostProcessor(propertyHolder);
        // ctx.refresh();
    }
}
 
@Override
public PropertySource<?> locate(Environment environment) {
	if (environment instanceof ConfigurableEnvironment) {
		final ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
		final String[] profiles = env.getActiveProfiles();
		final List<String> contexts = new ArrayList<>();

		setupContext(contexts, profiles, this.properties.getPrefix(),
				this.properties.getDefaultContext());

		setupContext(contexts, profiles, this.properties.getPrefix(),
				env.getProperty(EtcdConstants.PROPERTY_SPRING_APPLICATION_NAME));

		CompositePropertySource composite = new CompositePropertySource(
				EtcdConstants.NAME);
		Collections.reverse(contexts);

		for (String context : contexts) {
			EtcdPropertySource propertySource = new EtcdPropertySource(context, etcd, properties);
			propertySource.init();

			composite.addPropertySource(propertySource);
		}

		return composite;
	}

	return null;
}
 
源代码4 项目: the-app   文件: ProfileInitializer.java
private boolean hasActiveProfile(ConfigurableEnvironment environment) {
    return environment.getActiveProfiles().length > 0;
}
 
源代码5 项目: AppStash   文件: ProfileInitializer.java
private boolean hasActiveProfile(ConfigurableEnvironment environment) {
    return environment.getActiveProfiles().length > 0;
}
 
源代码6 项目: spring-javaformat   文件: SpringApplication.java
/**
 * Configure which profiles are active (or active by default) for this application
 * environment. Additional profiles may be activated during configuration file
 * processing via the {@code spring.profiles.active} property.
 * @param environment this application's environment
 * @param args arguments passed to the {@code run} method
 * @see #configureEnvironment(ConfigurableEnvironment, String[])
 * @see org.springframework.boot.context.config.ConfigFileApplicationListener
 */
protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
	environment.getActiveProfiles(); // ensure they are initialized
	// But these ones should go first (last wins in a property key clash)
	Set<String> profiles = new LinkedHashSet<>(this.additionalProfiles);
	profiles.addAll(Arrays.asList(environment.getActiveProfiles()));
	environment.setActiveProfiles(StringUtils.toStringArray(profiles));
}