application.yml文件是如何被解析的

application.yaml配置

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: password

spring定义了org.springframework.boot.autoconfigure.web.ServerProperties类来解析,该类使用@ConfigurationProperties注解,代码片段

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
		implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {

	/**
	 * Server HTTP port.
	 */
	private Integer port;

	/**
	 * Network address to which the server should bind to.
	 */
	private InetAddress address;

	/**
	 * Context path of the application.
	 */
	private String contextPath;

	/**
	 * Display name of the application.
	 */
	private String displayName = "application";

@ConfigurationProperties对应的handler是ConfigurationPropertiesBindingPostProcessor,该类实现了BeanPostProcessor接口,可以在对象初始化之前,对事例中的属性进行设置。
代码片段

public class ConfigurationPropertiesBindingPostProcessor
		implements BeanPostProcessor, BeanFactoryAware, ResourceLoaderAware,
		EnvironmentAware, ApplicationContextAware, InitializingBean, DisposableBean,
		ApplicationListener<ContextRefreshedEvent>, PriorityOrdered {

private PropertySources loadPropertySources(String[] locations,
			boolean mergeDefaultSources) {
		try {
			PropertySourcesLoader loader = new PropertySourcesLoader();
			for (String location : locations) {
				Resource resource = this.resourceLoader
						.getResource(this.environment.resolvePlaceholders(location));
				String[] profiles = this.environment.getActiveProfiles();
				for (int i = profiles.length; i-- > 0;) {
					String profile = profiles[i];
					loader.load(resource, profile);
				}
				loader.load(resource);
			}
	}	
}	

你可能感兴趣的:(java)