Java Code Examples for org.springframework.context.annotation.PropertySource

The following examples show how to use org.springframework.context.annotation.PropertySource. These examples are extracted from open source projects. 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 Project: geekbang-lessons   Source File: PropertySourceDemo.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    // 扩展 Environment 中的 PropertySources
    // 添加 PropertySource 操作必须在 refresh 方法之前完成
    Map<String, Object> propertiesSource = new HashMap<>();
    propertiesSource.put("user.name", "xiaomage");
    org.springframework.core.env.PropertySource propertySource = new MapPropertySource("first-property-source", propertiesSource);
    context.getEnvironment().getPropertySources().addFirst(propertySource);

    // 注册当前类作为 Configuration Class
    context.register(PropertySourceDemo.class);
    // 启动 Spring 应用上下文
    context.refresh();
    // beanName 和 bean 映射
    Map<String, User> usersMap = context.getBeansOfType(User.class);
    for (Map.Entry<String, User> entry : usersMap.entrySet()) {
        System.out.printf("User Bean name : %s , content : %s \n", entry.getKey(), entry.getValue());
    }
    System.out.println(context.getEnvironment().getPropertySources());
    // 关闭 Spring 应用上下文
    context.close();
}
 
Example 2
Source Project: chassis   Source File: ConfigurationBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private void initModuleConfiguration() {
    if (!scanModuleConfigurations) {
        this.moduleDefaultConfiguration = new ConcurrentMapConfiguration();
        return;
    }
    HashMap<String, Object> base = new HashMap<>();
    Set<Class<?>> types = reflections
            .getTypesAnnotatedWith(PropertySource.class);
    for (Class<?> type : types) {
        PropertySource propertySource = type
                .getAnnotation(PropertySource.class);
        String[] propertiesFiles = propertySource.value();
        for (String propertyFile : propertiesFiles) {
            Properties properties = new Properties();
            try (InputStream is = resourceLoader.getResource(SystemPropertyUtils.resolvePlaceholders(propertyFile))
                    .getInputStream()) {
                properties.load(is);
                LOGGER.debug("Initializing module properties from path " + propertyFile);
            } catch (Exception e) {
                BootstrapException.resourceLoadingFailed(propertyFile, e);
            }
            join(base, properties, propertyFile, propertiesFiles);
        }
    }
    this.moduleDefaultConfiguration = new ConcurrentMapConfiguration(base);
}
 
Example 3
@Test
public void testPrecedence() {
    // 获取 @PropertySource(name = "app", value = "classpath:/app.properties")  的 PropertySource
    org.springframework.core.env.PropertySource propertySource = environment.getPropertySources().get("app");
    System.out.println("[OS 环境变量     ] PATH  = " + System.getenv().get("PATH"));
    System.out.println("[@PropertySource] PATH  = " + propertySource.getProperty("PATH"));
    System.out.println("[Java 系统属性   ] PATH = " + System.getProperty("PATH"));
    System.out.println("[Environment    ] PATH = " + environment.getProperty("PATH"));
}
 
Example 4
@Test
public void testFindPropertyLocation() {
    System.out.println("[Environment    ] PATH = " + environment.getProperty("PATH"));
    for (org.springframework.core.env.PropertySource propertySource : environment.getPropertySources()) {
        if (propertySource.containsProperty("PATH")) {
            System.out.println("PATH = " + propertySource.getProperty("PATH") + " 位于 " + propertySource);
        }
    }
}
 
Example 5
@Override
public org.springframework.core.env.PropertySource<?> createPropertySource(String s,
		EncodedResource encodedResource) throws IOException {

	Properties properties = new XsuaaServicesParser(vcapJsonString).parseCredentials();

	properties.put("clientid", "customClientId");
	properties.put("clientsecret", "customClientSecret");
	properties.put("uaadomain", "overwriteUaaDomain");

	return XsuaaServicePropertySourceFactory.create("custom", properties);
}
 
Example 6
Source Project: seed   Source File: JasyptConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    MutablePropertySources propertySources = ((ConfigurableEnvironment)environment).getPropertySources();
    for(org.springframework.core.env.PropertySource<?> obj : propertySources){
        if(obj instanceof ResourcePropertySource){
            propertySources.replace(obj.getName(), new PropertySourceWrapper((ResourcePropertySource)obj));
        }
    }
}