use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.
the class EnvironmentConverterTests method convertedEnvironmentHasSameConversionService.
@Test
void convertedEnvironmentHasSameConversionService() {
AbstractEnvironment originalEnvironment = new MockEnvironment();
ConfigurableConversionService conversionService = mock(ConfigurableConversionService.class);
originalEnvironment.setConversionService(conversionService);
StandardEnvironment convertedEnvironment = this.environmentConverter.convertEnvironmentIfNecessary(originalEnvironment, StandardEnvironment.class);
assertThat(convertedEnvironment.getConversionService()).isEqualTo(conversionService);
}
use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.
the class ConfigTreePropertySourceTests method getPropertyViaEnvironmentSupportsConversion.
@Test
void getPropertyViaEnvironmentSupportsConversion() throws Exception {
StandardEnvironment environment = new StandardEnvironment();
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
environment.setConversionService((ConfigurableConversionService) conversionService);
environment.getPropertySources().addFirst(getFlatPropertySource());
assertThat(environment.getProperty("a")).isEqualTo("A");
assertThat(environment.getProperty("b")).isEqualTo("B");
assertThat(environment.getProperty("c", InputStreamSource.class).getInputStream()).hasContent("C");
assertThat(environment.getProperty("c", byte[].class)).contains('C');
assertThat(environment.getProperty("one", Integer.class)).isEqualTo(1);
}
use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.
the class LoggingSystemPropertiesTests method environment.
private Environment environment(String key, Object value) {
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value)));
return environment;
}
use of org.springframework.core.env.StandardEnvironment in project dubbo by alibaba.
the class EnvironmentUtilsTest method testExtraProperties.
@Test
public void testExtraProperties() {
System.setProperty("user.name", "mercyblitz");
StandardEnvironment environment = new StandardEnvironment();
Map<String, Object> map = new HashMap<>();
map.put("user.name", "Mercy");
MapPropertySource propertySource = new MapPropertySource("first", map);
CompositePropertySource compositePropertySource = new CompositePropertySource("comp");
compositePropertySource.addFirstPropertySource(propertySource);
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(compositePropertySource);
Map<String, Object> properties = EnvironmentUtils.extractProperties(environment);
Assert.assertEquals("Mercy", properties.get("user.name"));
}
use of org.springframework.core.env.StandardEnvironment in project canal by alibaba.
the class CanalAdapterLoader method loadAdapter.
private void loadAdapter(OuterAdapterConfig config, List<OuterAdapter> canalOutConnectors) {
try {
OuterAdapter adapter;
adapter = loader.getExtension(config.getName(), StringUtils.trimToEmpty(config.getKey()));
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// 替换ClassLoader
Thread.currentThread().setContextClassLoader(adapter.getClass().getClassLoader());
Environment env = (Environment) SpringContext.getBean(Environment.class);
Properties evnProperties = null;
if (env instanceof StandardEnvironment) {
evnProperties = new Properties();
for (PropertySource<?> propertySource : ((StandardEnvironment) env).getPropertySources()) {
if (propertySource instanceof EnumerablePropertySource) {
String[] names = ((EnumerablePropertySource<?>) propertySource).getPropertyNames();
for (String name : names) {
Object val = env.getProperty(name);
if (val != null) {
evnProperties.put(name, val);
}
}
}
}
}
adapter.init(config, evnProperties);
Thread.currentThread().setContextClassLoader(cl);
canalOutConnectors.add(adapter);
logger.info("Load canal adapter: {} succeed", config.getName());
} catch (Exception e) {
logger.error("Load canal adapter: {} failed", config.getName(), e);
}
}
Aggregations