Java Code Examples for org.springframework.boot.bind.RelaxedPropertyResolver#getProperty()

The following examples show how to use org.springframework.boot.bind.RelaxedPropertyResolver#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RelaxedPropertyResolverBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    ConfigurableApplicationContext context =
            new SpringApplicationBuilder(
                    RelaxedPropertyResolverBootstrap.class)
                    .properties("user.city.postCode=0571")
                    .web(false) // 非 Web 应用
                    .run(args);
    // 获取 Environment,也是 PropertyResolver 对象
    PropertyResolver environment = context.getEnvironment();
    // 属性名称前缀
    String prefix = "user.city.";
    // 创建 RelaxedPropertyResolver 实例
    RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, prefix);
    // 获取松散化配置属性
    String postCode = propertyResolver.getProperty("post-code");
    System.out.println("postCode = " + postCode);
    // 关闭上下文
    context.close();
}
 
Example 2
Source File: SpringPropertyAction.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
private String getValue(String source, String defaultValue) {
	if (this.environment == null) {
		addWarn("No Spring Environment available to resolve " + source);
		return defaultValue;
	}
	String value = this.environment.getProperty(source);
	if (value != null) {
		return value;
	}
	int lastDot = source.lastIndexOf(".");
	if (lastDot > 0) {
		String prefix = source.substring(0, lastDot + 1);
		RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(this.environment, prefix);
		return resolver.getProperty(source.substring(lastDot + 1), defaultValue);
	}
	return defaultValue;
}
 
Example 3
Source File: DataSourceConfig.java    From tinyid with Apache License 2.0 6 votes vote down vote up
@Bean
public DataSource getDynamicDataSource() {
    DynamicDataSource routingDataSource = new DynamicDataSource();
    List<String> dataSourceKeys = new ArrayList<>();
    RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "datasource.tinyid.");
    String names = propertyResolver.getProperty("names");
    String dataSourceType = propertyResolver.getProperty("type");

    Map<Object, Object> targetDataSources = new HashMap<>(4);
    routingDataSource.setTargetDataSources(targetDataSources);
    routingDataSource.setDataSourceKeys(dataSourceKeys);
    // 多个数据源
    for (String name : names.split(SEP)) {
        Map<String, Object> dsMap = propertyResolver.getSubProperties(name + ".");
        DataSource dataSource = buildDataSource(dataSourceType, dsMap);
        buildDataSourceProperties(dataSource, dsMap);
        targetDataSources.put(name, dataSource);
        dataSourceKeys.add(name);
    }
    return routingDataSource;
}
 
Example 4
Source File: AbstractProxyTargetResolver.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 5 votes vote down vote up
protected URI resolveUri(ServiceInstance serviceInstance) {
    Map<String, Object> metadata = new HashMap<>();
    for (Map.Entry<String, String> entry : serviceInstance.getMetadata().entrySet()) {
        metadata.put(entry.getKey(), entry.getValue());
    }

    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(serviceInstance.getUri());
    RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(new MapPropertyResolver(metadata));
    String configPath = propertyResolver.getProperty("configPath");
    if (configPath != null) {
        uriBuilder.path(configPath);
    }

    return uriBuilder.build().toUri();
}