在实际项目开发中咱们常常须要用到一些自定义配置,而且但愿单独配置,方便维护,如今介绍下方式:java
对于一些不变更的配置,写死在项目中维护,以下spring
而后在启动类中加载该配置springboot
'''app
public class AAApplication { public static void main(String[] args) { SpringApplication.run(RecommendApplication.class, args); } @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("siteuid-config.yml")); configurer.setProperties(yaml.getObject()); return configurer; } }
注意:若是是启动配置是properties文件,这里是没法加载yml,须要将配置的siteuid-config.yml转成siteuid-config.propertiesui
集成springcloud config后,默认根据应用名加载,若是须要加载多配置,须要修改相关配置:code
spring: application: name: quote profiles: active: dev cloud: config: profile: dev name: quote,siteuid-config enabled: true discovery: service-id: config-server enabled: true