前言
用过的springboot的小伙伴都知道springboot不需要再像springmvc引入那么多的配置文件,只需要加入application.properties或者application.yml即可,比如在上一篇文章讲到数据库的配置,只需要在文件引入如下的配置即可:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zplxjj?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=@ZPLxjj12345
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
下面简单介绍下springboot在启动的时候是在何时读取的properties和yml文件的内容的
实现一个简单的自定义监听器
第一步:定义一个event,继承ApplicationEvent
public class CustomerApplicationEvent extends ApplicationEvent {
public CustomerApplicationEvent(Object source) {
super(source);
System.out.println("CustomerApplicationEvent constructor...");
}
}
第二步:定义一个listener
@Component
public class CustomerApplicationListener implements ApplicationListener<CustomerApplicationEvent> {
@Override
public void onApplicationEvent(CustomerApplicationEvent customerApplicationEvent) {
System.out.println("customerApplicationEvent:"+customerApplicationEvent.getClass().getName());
}
}
第三步:注册监听器
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
// 注册 CustomerApplicationListener 事件监听器
context.addApplicationListener(new CustomerApplicationListener());
// 发布 CustomerApplicationEvent 事件
context.publishEvent(new CustomerApplicationEvent(new Object()));
}
}
启动项目后,会发现控制台输出了:
CustomerApplicationEvent constructor...
customerApplicationEvent:com.stone.zplxjj.event.CustomerApplicationEvent
springboot自带的事件
- ApplicationStartingEvent:应用启动事件,在调用 SpringApplication.run() 方法之前,可以从中获取到 SpringApplication 对象,进行一些启动前设置。
- ApplicationEnvironmentPreparedEvent:Environment准备完成事件,此时可以从中获取到 Environment 对象并对其中的配置项进行查看或者修改。
- ApplicationPreparedEvent:ApplicationContext准备完成事件,接下来 Spring 就能够向容器中加载 Bean 了 。
- ApplicationReadyEvent:应用准备完成事件,预示着应用可以接收和处理请求了。
- ApplicationFailedEvent:应用启动失败事件,可以从中捕获到启动失败的异常信息进行相应处理,例如:添加虚拟机对应的钩子进行资源的回收与释放。
读取配置代码入口:ApplicationEnvironmentPreparedEvent和ConfigFileApplicationListener
加载配置文件需要用到ConfigFileApplicationListener,其代码如下:
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent(
(ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}
进入方法:onApplicationEnvironmentPreparedEvent
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());
}
}
进入postProcessor.postProcessEnvironment:
//类:ConfigFileApplicationListener
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
addPropertySources(environment, application.getResourceLoader());
}
进入addPropertySources
protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
//将随机方法放入到PropertySources中
RandomValuePropertySource.addToEnvironment(environment);
//load加载
new Loader(environment, resourceLoader).load();
}
进入load方法:
public void load() {
this.profiles = new LinkedList<>();
this.processedProfiles = new LinkedList<>();
this.activatedProfiles = false;
this.loaded = new LinkedHashMap<>();
initializeProfiles();
while (!this.profiles.isEmpty()) {
Profile profile = this.profiles.poll();
if (profile != null && !profile.isDefaultProfile()) {
addProfileToEnvironment(profile.getName());
}
load(profile, this::getPositiveProfileFilter,
addToLoaded(MutablePropertySources::addLast, false));
this.processedProfiles.add(profile);
}
resetEnvironmentProfiles(this.processedProfiles);
load(null, this::getNegativeProfileFilter,
addToLoaded(MutablePropertySources::addFirst, true));
addLoadedPropertySources();
}
进入字方法load
private void load(Profile profile, DocumentFilterFactory filterFactory,
DocumentConsumer consumer) {
getSearchLocations().forEach((location) -> {
boolean isFolder = location.endsWith("/");
Set<String> names = isFolder ? getSearchNames() : NO_SEARCH_NAMES;
names.forEach(
(name) -> load(location, name, profile, filterFactory, consumer));
});
}
- getSearchLocations():首先看CONFIG_LOCATION_PROPERTY,是否存在配置,无则走默认配置路径DEFAULT_SEARCH_LOCATIONS
/**
* The "config location" property name.
*/
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
- getSearchNames():首先看CONFIG_NAME_PROPERTY(spring.config.name)配置,否则走DEFAULT_NAMES(application)
**spring.config.name说明:**假如你不喜欢“application.properties”这个默认文件名,你可以重新设定:spring.config.name属性直接指定属性文件名称,spring.config.location属性指定明确路径,但是要注意不能写在application.properties文件里,这样会不起作用,可以写在java -jar xxx.jar --spring.config.name=custom.properties,还可以通过环境变量等方式,yml文件也可以这样
真正加载配置文件的方法:
private void load(String location, String name, Profile profile,
DocumentFilterFactory filterFactory, DocumentConsumer consumer) {
if (!StringUtils.hasText(name)) {
for (PropertySourceLoader loader : this.propertySourceLoaders) {
if (canLoadFileExtension(loader, location)) {
load(loader, location, profile,
filterFactory.getDocumentFilter(profile), consumer);
return;
}
}
}
Set<String> processed = new HashSet<>();
for (PropertySourceLoader loader : this.propertySourceLoaders) {
for (String fileExtension : loader.getFileExtensions()) {
if (processed.add(fileExtension)) {
loadForFileExtension(loader, location + name, "." + fileExtension,
profile, filterFactory, consumer);
}
}
}
}
loader.getFileExtensions():获取所有支持的文件后缀,loader初始化如下:
Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(
PropertySourceLoader.class, getClass().getClassLoader());
}
通过加载jar:spring-boot-2.1.4.RELEASE.jar:META-INF/spring.factories文件下对应内容:
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
从这里我们可以看到,通过PropertiesPropertySourceLoader和YamlPropertySourceLoader
加载配置文件,具体源码没有细看了,有兴趣自行阅读吧
加载完配置文件,调用方法:addLoadedPropertySources()
结语
至此,springboot加载properties和yml的入口就分析到这里了,细节上肯定不能面面俱到,但是入口知道了,后面就好分析了