Search in sources :

Example 26 with Environment

use of org.springframework.core.env.Environment in project pinpoint by naver.

the class ExperimentalConfig method readExperimentalProperties.

private Map<String, Object> readExperimentalProperties(Environment environment) {
    MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
    Map<String, Object> collect = propertySources.stream().filter(ps -> ps instanceof EnumerablePropertySource).map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()).flatMap(Arrays::stream).filter(propName -> propName.startsWith(PREFIX)).collect(Collectors.toMap(Function.identity(), toValue(environment)));
    return collect;
}
Also used : Objects(java.util.Objects) Component(org.springframework.stereotype.Component) Arrays(java.util.Arrays) Environment(org.springframework.core.env.Environment) Map(java.util.Map) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Arrays(java.util.Arrays)

Example 27 with Environment

use of org.springframework.core.env.Environment in project yyl_example by Relucent.

the class SpringExpressionLanguageExample method main.

public static void main(String[] args) {
    ExpressionParser parser = new SpelExpressionParser();
    Environment environment = getEnvironment();
    RootBean root = new RootBean(environment);
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.setVariable("environment", environment);
    Expression expression = parser.parseExpression("environment.getProperty('demo.message')");
    Object value = expression.getValue(context);
    System.out.println(value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) Environment(org.springframework.core.env.Environment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 28 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class OnEndpointElementCondition method getEndpointOutcome.

protected ConditionOutcome getEndpointOutcome(ConditionContext context, String endpointName) {
    Environment environment = context.getEnvironment();
    String enabledProperty = this.prefix + endpointName + ".enabled";
    if (environment.containsProperty(enabledProperty)) {
        boolean match = environment.getProperty(enabledProperty, Boolean.class, true);
        return new ConditionOutcome(match, ConditionMessage.forCondition(this.annotationType).because(this.prefix + endpointName + ".enabled is " + match));
    }
    return null;
}
Also used : Environment(org.springframework.core.env.Environment) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome)

Example 29 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class CacheCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String sourceClass = "";
    if (metadata instanceof ClassMetadata) {
        sourceClass = ((ClassMetadata) metadata).getClassName();
    }
    ConditionMessage.Builder message = ConditionMessage.forCondition("Cache", sourceClass);
    Environment environment = context.getEnvironment();
    try {
        BindResult<CacheType> specified = Binder.get(environment).bind("spring.cache.type", CacheType.class);
        if (!specified.isBound()) {
            return ConditionOutcome.match(message.because("automatic cache type"));
        }
        CacheType required = CacheConfigurations.getType(((AnnotationMetadata) metadata).getClassName());
        if (specified.get() == required) {
            return ConditionOutcome.match(message.because(specified.get() + " cache type"));
        }
    } catch (BindException ex) {
    }
    return ConditionOutcome.noMatch(message.because("unknown cache type"));
}
Also used : ClassMetadata(org.springframework.core.type.ClassMetadata) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) Environment(org.springframework.core.env.Environment) BindException(org.springframework.boot.context.properties.bind.BindException)

Example 30 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class ConfigurationPropertySourcesTests method fromPropertySourceShouldFlattenPropertySources.

@Test
void fromPropertySourceShouldFlattenPropertySources() {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", "bar")));
    environment.getPropertySources().addFirst(new MapPropertySource("far", Collections.singletonMap("far", "far")));
    MutablePropertySources sources = new MutablePropertySources();
    sources.addFirst(new PropertySource<Environment>("env", environment) {

        @Override
        public String getProperty(String key) {
            return this.source.getProperty(key);
        }
    });
    sources.addLast(new MapPropertySource("baz", Collections.singletonMap("baz", "barf")));
    Iterable<ConfigurationPropertySource> configurationSources = ConfigurationPropertySources.from(sources);
    assertThat(configurationSources.iterator()).toIterable().hasSize(5);
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

Environment (org.springframework.core.env.Environment)163 Test (org.junit.jupiter.api.Test)68 StandardEnvironment (org.springframework.core.env.StandardEnvironment)30 EnvironmentVariableUtility (com.synopsys.integration.alert.environment.EnvironmentVariableUtility)26 MockEnvironment (org.springframework.mock.env.MockEnvironment)24 SpringApplication (org.springframework.boot.SpringApplication)21 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)19 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)19 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)19 HashMap (java.util.HashMap)14 ResourceLoader (org.springframework.core.io.ResourceLoader)13 Collectors (java.util.stream.Collectors)12 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)11 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)9 MutablePropertySources (org.springframework.core.env.MutablePropertySources)9 Map (java.util.Map)8 AlertConstants (com.synopsys.integration.alert.api.common.model.AlertConstants)7 ChannelKeys (com.synopsys.integration.alert.descriptor.api.model.ChannelKeys)7 EnvironmentVariableMockingUtil (com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil)7 Predicate (java.util.function.Predicate)7