Search in sources :

Example 1 with PropertySources

use of cn.taketoday.core.env.PropertySources in project today-framework by TAKETODAY.

the class AbstractApplicationEnvironmentTests method propertyResolverIsOptimizedForConfigurationProperties.

@Test
void propertyResolverIsOptimizedForConfigurationProperties() {
    StandardEnvironment environment = createEnvironment();
    ConfigurablePropertyResolver expected = ConfigurationPropertySources.createPropertyResolver(new PropertySources());
    Object propertyResolver = PropertyAccessorFactory.forDirectFieldAccess(environment).getPropertyValue("propertyResolver");
    assertThat(propertyResolver).isInstanceOf(expected.getClass());
}
Also used : ConfigurationPropertySources(cn.taketoday.context.properties.source.ConfigurationPropertySources) PropertySources(cn.taketoday.core.env.PropertySources) ConfigurablePropertyResolver(cn.taketoday.core.env.ConfigurablePropertyResolver) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 2 with PropertySources

use of cn.taketoday.core.env.PropertySources in project today-framework by TAKETODAY.

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySources.

@Test
public void explicitPropertySources() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    PropertySources propertySources = new PropertySources();
    propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("foo");
    assertThat(propertySources.iterator().next()).isEqualTo(ppc.getAppliedPropertySources().iterator().next());
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) MockPropertySource(cn.taketoday.core.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 3 with PropertySources

use of cn.taketoday.core.env.PropertySources in project today-framework by TAKETODAY.

the class StandardEnvironmentTests method loadProperties.

@Test
void loadProperties() throws IOException {
    ConfigurableEnvironment environment = new StandardEnvironment();
    PropertySources propertySources = environment.getPropertySources();
    Properties properties = PropertiesUtils.loadProperties(ResourceUtils.getResource("classpath:info.properties"));
    propertySources.addLast(new PropertiesPropertySource("info", properties));
    assert "https://taketoday.cn".equals(environment.getProperty("site.host"));
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) PropertiesPropertySource(cn.taketoday.core.env.PropertiesPropertySource) Properties(java.util.Properties) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 4 with PropertySources

use of cn.taketoday.core.env.PropertySources in project today-framework by TAKETODAY.

the class PropertySourcesDeducer method getPropertySources.

PropertySources getPropertySources() {
    PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
    if (configurer != null) {
        return configurer.getAppliedPropertySources();
    }
    PropertySources sources = extractEnvironmentPropertySources();
    Assert.state(sources != null, "Unable to obtain PropertySources from PropertySourcesPlaceholderConfigurer or Environment");
    return sources;
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) PropertySourcesPlaceholderConfigurer(cn.taketoday.context.support.PropertySourcesPlaceholderConfigurer)

Example 5 with PropertySources

use of cn.taketoday.core.env.PropertySources in project today-framework by TAKETODAY.

the class ConfigurationPropertySources method get.

/**
 * Return a set of {@link ConfigurationPropertySource} instances that have previously
 * been {@link #attach(Environment) attached} to the {@link Environment}.
 *
 * @param environment the source environment (must be an instance of
 * {@link ConfigurableEnvironment})
 * @return an iterable set of configuration property sources
 * @throws IllegalStateException if not configuration property sources have been
 * attached
 */
public static Iterable<ConfigurationPropertySource> get(Environment environment) {
    Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
    PropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
    ConfigurationPropertySourcesPropertySource attached = (ConfigurationPropertySourcesPropertySource) sources.get(ATTACHED_PROPERTY_SOURCE_NAME);
    if (attached == null) {
        return from(sources);
    }
    return attached.getSource();
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment)

Aggregations

PropertySources (cn.taketoday.core.env.PropertySources)11 Test (org.junit.jupiter.api.Test)6 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)4 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 MockPropertySource (cn.taketoday.core.env.MockPropertySource)3 Properties (java.util.Properties)3 ConfigurationPropertySources (cn.taketoday.context.properties.source.ConfigurationPropertySources)2 PropertiesPropertySource (cn.taketoday.core.env.PropertiesPropertySource)2 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeanFactory (cn.taketoday.beans.factory.BeanFactory)1 DependencyInjector (cn.taketoday.beans.factory.support.DependencyInjector)1 PropertySourcesPlaceholderConfigurer (cn.taketoday.context.support.PropertySourcesPlaceholderConfigurer)1 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)1 ConfigurablePropertyResolver (cn.taketoday.core.env.ConfigurablePropertyResolver)1 MapPropertySource (cn.taketoday.core.env.MapPropertySource)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1