为什么我的CustomPropertySourceLocator中无法获得ConfigurationProperties的值?

时间:2019-05-05 07:55:33

标签: spring-boot

在CustomPropertySourceLocator中,我将尝试获取配置属性的某些值,但是它始终返回null,那么很明显,这些属性未完全初始化。我该怎么办?

由于我指的是spring-cloud-config https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java的源代码, 所以我很困惑,为什么我无法在PropertySourceLocator中获得ConfigurationProperties的值。

use std::collections::HashMap;

const MVPS: &str = concat!(
        "2017-18     James Harden, Houston Rockets (Voting)\n",
        "2016-17     Russell Westbrook, Oklahoma City Thunder (Voting)\n",
        ... omitted for brevity
);

fn main() {

    let lines: Vec<&str> = MVPS.split('\n')
        .map(|line| &line[12..line.find(',').unwrap_or(line.len())])
        .collect();

    let mut mvp_map = HashMap::new();
    for name in &lines {
        mvp_map.insert(name, mvp_map.get(name).unwrap_or(&0) + 1);
    }

    let mut mvp_entry_list = Vec::new();
    for entry in &mvp_map {
        mvp_entry_list.push(entry);
    }
    mvp_entry_list.sort_by(|a, b| (b.1).cmp(a.1));

    for e in &mvp_entry_list {
        println!("{}: {}", e.0, e.1);
    }

}
gcccli:
  enable: false
  accessId: FWuOfOz08p6NJOxavecU

public class GccPropertySourceLocator implements PropertySourceLocator {

    private GccConfigProperties gccConfigProperties;

    public GccPropertySourceLocator(GccConfigProperties gccConfigProperties) {
        this.gccConfigProperties = gccConfigProperties;
    }

    @Override
    public PropertySource<?> locate(Environment environment) {
        if (environment instanceof ConfigurableEnvironment) {
            boolean enable = gccConfigProperties.isEnable();
            System.out.println(enable);
            String accessId = gccConfigProperties.getAccessId();
            System.out.println(accessId);

        }

        Map<String, String> gccProperties = new HashMap<>();
        return new GccPropertySource("gccPropertySource", gccProperties);
    }
}
@ConfigurationProperties("gcccli")
public class GccConfigProperties {

    /**
     * Whether to enable gcc ,default true
     */
    private boolean enable = true;
    /**
     * The accessId of gcc
     */
    private String accessId;

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public String getAccessId() {
        return accessId;
    }

    public void setAccessId(String accessId) {
        this.accessId = accessId;
    }


}
@Configuration
public class GccBootstrapConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public GccPropertySourceLocator gccPropertySourceLocator(
        GccConfigProperties gccConfigProperties) {
        return new GccPropertySourceLocator(gccConfigProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public GccConfigProperties gccConfigProperties() {
        return new GccConfigProperties();
    }
}
@SpringBootApplication
public class GccApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(GccApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(GccApplication.class);
    }
}

0 个答案:

没有答案