Spring Cloud使用BootstrapConfiguration配置启动加载项

使用BootstrapConfiguration加载,等同于bootstrap.yml中的配置

使用PropertySourceLocator接口

@Configuration
public class BootstrapConfigurer implements PropertySourceLocator {

    private static final Logger log = LoggerFactory.getLogger(BootstrapConfigurer .class);
    /**
     * @param environment the current Environment
     * @return a PropertySource or null if there is none
     * @throws IllegalStateException if there is a fail fast condition
     */
    @Override
    public PropertySource locate(Environment environment) {
        Map property = setConfig(environment);
        if (property != null && property.size() > 0) {
            return new MapPropertySource("customProperty", property);
        } else {
            return null;
        }
    }

    private Map setConfig(Environment environment) {
        Map property = new HashMap<>();
        property.put("eureka.instance.lease-renewal-interval-in-seconds", "5");
        return property;
    }
}

在META-INF的spring.factories中添加

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.xxx.BootstrapConfigurer

 

你可能感兴趣的:(spring,cloud,Spring,java)