Search in sources :

Example 6 with YamlPropertiesFactoryBean

use of org.springframework.beans.factory.config.YamlPropertiesFactoryBean in project alien4cloud by alien4cloud.

the class LdapCondition method matches.

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    YamlPropertiesFactoryBean propertiesFactoryBean = AlienYamlPropertiesFactoryBeanFactory.get(context.getResourceLoader());
    Object ldapEnabled = propertiesFactoryBean.getObject().get("ldap.enabled");
    if (ldapEnabled != null && ldapEnabled instanceof Boolean) {
        return ((Boolean) ldapEnabled).booleanValue();
    }
    return false;
}
Also used : YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean)

Example 7 with YamlPropertiesFactoryBean

use of org.springframework.beans.factory.config.YamlPropertiesFactoryBean in project Asqatasun by Asqatasun.

the class AsqatasunTestConfig method propertySourcesPlaceholderConfigurer.

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    yaml.setResources(new ClassPathResource("application-test.yml"));
    propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
    return propertySourcesPlaceholderConfigurer;
}
Also used : YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer) ClassPathResource(org.springframework.core.io.ClassPathResource) YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 8 with YamlPropertiesFactoryBean

use of org.springframework.beans.factory.config.YamlPropertiesFactoryBean in project swift by luastar.

the class ClassLoaderUtils method getProperties.

/**
 * 将资源文件转化为Properties对象
 * 基于classloader实现
 *
 * @param resource 资源文件
 * @return
 */
/*
    public static Properties getProperties(String resource) {
        try {
            Properties properties = new Properties();
            InputStream is = getClassLoader().getResourceAsStream(resource);
            if (is != null) {
                properties.load(new BufferedReader(new InputStreamReader(is)));
            }
            return properties;
        } catch (IOException e) {
            return new Properties();
        }
    }
    */
/**
 * 将资源文件转化为Properties对象
 * 基于spring实现
 *
 * @param resource 资源文件
 * @return
 */
public static Properties getProperties(String... resource) {
    Properties properties = new Properties();
    if (resource == null || resource.length == 0) {
        return properties;
    }
    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    for (String resPath : resource) {
        try {
            Resource[] resourceArray = getResourcePatternResolver().getResources(resPath);
            if (resourceArray != null) {
                for (Resource res : resourceArray) {
                    if (FilenameUtils.isExtension(resPath, "yml")) {
                        logger.info("加载配置文件:{}", res.getDescription());
                        yaml.setResources(res);
                        properties.putAll(yaml.getObject());
                    } else if (FilenameUtils.isExtension(resPath, "properties")) {
                        logger.info("加载配置文件:{}", res.getDescription());
                        PropertiesLoaderUtils.fillProperties(properties, new EncodedResource(res, "UTF-8"));
                    } else {
                        logger.warn("忽略不识别的文件类型,目前仅支持properties和yml文件:{}", res.getDescription());
                    }
                }
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        logger.info("配置文件变量:{} = {}", entry.getKey(), entry.getValue());
    }
    return properties;
}
Also used : EncodedResource(org.springframework.core.io.support.EncodedResource) Resource(org.springframework.core.io.Resource) YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) IOException(java.io.IOException) Properties(java.util.Properties) Map(java.util.Map) EncodedResource(org.springframework.core.io.support.EncodedResource)

Example 9 with YamlPropertiesFactoryBean

use of org.springframework.beans.factory.config.YamlPropertiesFactoryBean in project spring-boot by spring-projects.

the class SpringProfileDocumentMatcherTests method getProperties.

private Properties getProperties(String values) throws IOException {
    YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
    ByteArrayResource resource = new ByteArrayResource(values.getBytes());
    yamlPropertiesFactoryBean.setResources(resource);
    yamlPropertiesFactoryBean.afterPropertiesSet();
    return yamlPropertiesFactoryBean.getObject();
}
Also used : YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) ByteArrayResource(org.springframework.core.io.ByteArrayResource)

Example 10 with YamlPropertiesFactoryBean

use of org.springframework.beans.factory.config.YamlPropertiesFactoryBean in project spring-cloud-config by spring-cloud.

the class VaultEnvironmentRepository method findOne.

@Override
public Environment findOne(String application, String profile, String label) {
    String state = request.getHeader(STATE_HEADER);
    String newState = this.watch.watch(state);
    String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
    List<String> scrubbedProfiles = scrubProfiles(profiles);
    List<String> keys = findKeys(application, scrubbedProfiles);
    Environment environment = new Environment(application, profiles, label, null, newState);
    for (String key : keys) {
        // read raw 'data' key from vault
        String data = read(key);
        if (data != null) {
            // data is in json format of which, yaml is a superset, so parse
            final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new ByteArrayResource(data.getBytes()));
            Properties properties = yaml.getObject();
            if (!properties.isEmpty()) {
                environment.add(new PropertySource("vault:" + key, properties));
            }
        }
    }
    return environment;
}
Also used : Environment(org.springframework.cloud.config.environment.Environment) YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Aggregations

YamlPropertiesFactoryBean (org.springframework.beans.factory.config.YamlPropertiesFactoryBean)17 Properties (java.util.Properties)6 Bean (org.springframework.context.annotation.Bean)5 ByteArrayResource (org.springframework.core.io.ByteArrayResource)5 Resource (org.springframework.core.io.Resource)4 PropertySourcesPlaceholderConfigurer (org.springframework.context.support.PropertySourcesPlaceholderConfigurer)3 FileSystemResource (org.springframework.core.io.FileSystemResource)3 Map (java.util.Map)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 lombok.val (lombok.val)1 OperationException (org.apache.servicecomb.config.kie.client.exception.OperationException)1 ValueType (org.apache.servicecomb.config.kie.client.model.ValueType)1 Environment (org.springframework.cloud.config.environment.Environment)1 PropertySource (org.springframework.cloud.config.environment.PropertySource)1 EncodedResource (org.springframework.core.io.support.EncodedResource)1