springboot学习----加载application.property
2017-09-05 14:55
459 查看
起因
spring boot启动会自动加载资源目录下的application.property
部分属性
默认加载路径 "classpath:/,classpath:/config/,file:./,file:./config/"; 默认文件名称 application 修改默认文件名称属性 "spring.config.name" 修改默认加载路径属性 "spring.config.location" profile属性 spring.profiles.active
分析
spring boot启动SpringApplication的run方法 prepareEnvironment初始化 environment 发布ApplicationEnvironmentPreparedEvent事件 触发ConfigFileApplicationListener监听的onApplicationEnvironmentPreparedEvent方法 postProcessEnvironment方法 addPropertySources方法 创建Loader,执行load方法 在load方法中加载配置,优先加载用户配置,没有配置使用默认配置
源码
SpringApplication类部分代码public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //加载application配置文件入口 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); Banner printedBanner = printBanner(environment); context = createApplicationContext(); analyzers = new FailureAnalyzers(context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } return context; } catch (Throwable ex) { handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); } }
ConfigFileApplicationListener部分代码
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/"; private static final String DEFAULT_NAMES = "application"; public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location"; public static final String CONFIG_NAME_PROPERTY = "spring.config.name"; public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active"; public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include"; private void onApplicationEnvironmentPreparedEvent( ApplicationEnvironmentPreparedEvent event) { List<EnvironmentPostProcessor> postProcessors = loadPostProcessors(); postProcessors.add(this); AnnotationAwareOrderComparator.sort(postProcessors); for (EnvironmentPostProcessor postProcessor : postProcessors) { //加载点 postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication()); } } List<EnvironmentPostProcessor> loadPostProcessors() { return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader()); } @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { //加载点 addPropertySources(environment, application.getResourceLoader()); configureIgnoreBeanInfo(environment); bindToSpringApplication(environment, application); } protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { RandomValuePropertySource.addToEnvironment(environment); //加载点 new Loader(environment, resourceLoader).load(); } //加载配置location、name public void load() { this.propertiesLoader = new PropertySourcesLoader(); this.activatedProfiles = false; this.profiles = Collections.asLifoQueue(new LinkedList<Profile>()); this.processedProfiles = new LinkedList<Profile>(); // Pre-existing active profiles set via Environment.setActiveProfiles() // are additional profiles and config files are allowed to add more if // they want to, so don't call addActiveProfiles() here. Set<Profile> initialActiveProfiles = initializeActiveProfiles(); this.profiles.addAll(getUnprocessedActiveProfiles(initialActiveProfiles)); if (this.profiles.isEmpty()) { for (String defaultProfileName : this.environment.getDefaultProfiles()) { Profile defaultProfile = new Profile(defaultProfileName, true); if (!this.profiles.contains(defaultProfile)) { this.profiles.add(defaultProfile); } } } // The default profile for these purposes is represented as null. We add it // last so that it is first out of the queue (active profiles will then // override any settings in the defaults when the list is reversed later). this.profiles.add(null); while (!this.profiles.isEmpty()) { Profile profile = this.profiles.poll(); for (String location : getSearchLocations()) { if (!location.endsWith("/")) { // location is a filename already, so don't search for more // filenames load(location, null, profile); } else { for (String name : getSearchNames()) { load(location, name, profile); } } } this.processedProfiles.add(profile); } addConfigurationProperties(this.propertiesLoader.getPropertySources()); } //具体实现location private Set<String> getSearchLocations() { Set<String> locations = n b066 ew LinkedHashSet<String>(); // User-configured settings take precedence, so we do them first if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) { for (String path : asResolvedSet( this.environment.getProperty(CONFIG_LOCATION_PROPERTY), null)) { if (!path.contains("$")) { path = StringUtils.cleanPath(path); if (!ResourceUtils.isUrl(path)) { path = ResourceUtils.FILE_URL_PREFIX + path; } } locations.add(path); } } locations.addAll( asResolvedSet(ConfigFileApplicationListener.this.searchLocations, DEFAULT_SEARCH_LOCATIONS)); return locations; } //具体实现name private Set<String> getSearchNames() { if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) { return asResolvedSet(this.environment.getProperty(CONFIG_NAME_PROPERTY), null); } return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES); }
参考
http://blog.csdn.net/liaokailin/article/details/48878447
相关文章推荐
- SpringBoot学习之路:12.Spring Boot使用idea开发如何配置热加载
- Spring Boot学习——Spring Boot配置文件application
- springboot 修改启动时默认加载application.properties文件以及加载指定配置文件
- spring boot实战(第六篇)加载application资源文件源码分析
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
- spring boot 学习笔记(009)application.yml
- spring boot实战(第六篇)加载application资源文件源码分析
- What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?
- SpringBoot中加载application.yml中的常量值
- springboot 修改默认加载的配置文件不为application.properties
- spring boot 学习笔记(2)格式,加载XML文件
- 【系统学习SpringBoot】SpringBoot读取Property配置
- 解决eclipse创建spring boot项目加载不到application.properties配置文件的问题
- Spring boot 学习笔记(1)——hello world创建,属性application.properties、 yml配置
- SpringBoot学习(1)-使用maven加载本地jar包
- spring学习笔记(3)——配置文件applicationContext.xml的加载
- Spring-boot实例学习之Simple Command line application
- SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置
- springboot学习之启动类中的@SpringBootApplication注解