类org.springframework.context.annotation.PropertySource源码实例Demo

下面列出了怎么用org.springframework.context.annotation.PropertySource的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: geekbang-lessons   文件: PropertySourceDemo.java
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();
}
 
源代码2 项目: chassis   文件: ConfigurationBuilder.java
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);
}
 
@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"));
}
 
@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);
        }
    }
}
 
@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);
}
 
源代码6 项目: seed   文件: JasyptConfiguration.java
@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));
        }
    }
}
 
 类方法
 同包方法