总结

spring.factories的作用是 当外界使用该jar时不用再写配置 会自动加入其中已经配置好的配置

用法及配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Initializers
org.springframework.context.ApplicationContextInitializer=\
com.jhj.springboot.autoconfig.MyApplicationContextInitializer

## Application Listeners
org.springframework.context.ApplicationListener=\
com.jhj.springboot.autoconfig.MyApplicationListener

## Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
com.jhj.springboot.autoconfig.MyAutoConfigurationImportListener

## Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
com.jhj.springboot.autoconfig.MyConfigurationCondition

## Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jhj.springboot.autoconfig.MyConfiguration

## Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.jhj.springboot.autoconfig.MyFailureAnalyzer

## Template availability providers
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\
com.jhj.springboot.autoconfig.MyTemplateAvailabilityProvider

#后置环境变量处理器
org.springframework.boot.env.EnvironmentPostProcessor=cn.telecom.starter.cloud.LoadProperties

ApplicationContextInitializer 上下文初始化

1
2
org.springframework.context.ApplicationContextInitializer=\
com.jhj.springboot.autoconfig.MyApplicationContextInitializer
1
2
3
4
5
6
7
8
9
10
11
public class MyApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {


@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

System.out.println("MyApplicationContextInitializer.initialize() " + applicationContext);
}

}

ApplicationListener 配置应用程序监听器

1
2
org.springframework.context.ApplicationListener=\
com.jhj.springboot.autoconfig.MyApplicationListener
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {


@Override
public void onApplicationEvent(ApplicationEvent event) {

System.out.println("MyApplicationListener.onApplicationEvent() " + event);
if(event instanceof ApplicationStartedEvent) {

throw new RuntimeException("我故意抛出的错误,仅仅为了触发自定义 MyFailureAnalyzer");
}
}

}

AutoConfigurationImportListener 配置自动配置导入监听器

1
2
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
com.jhj.springboot.autoconfig.MyAutoConfigurationImportListener
1
2
3
4
5
6
7
8
9
10
public class MyAutoConfigurationImportListener implements AutoConfigurationImportListener {


@Override
public void onAutoConfigurationImportEvent(AutoConfigurationImportEvent event) {

System.out.println("MyAutoConfigurationImportListener.onAutoConfigurationImportEvent() " + event);
}

}

AutoConfigurationImportFilter 配置自动配置导入过滤器

1
2
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
com.jhj.springboot.autoconfig.MyConfigurationCondition
1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyConfigurationCondition implements AutoConfigurationImportFilter {


@Override
public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {

System.out.println("MyConfigurationCondition.match() autoConfigurationClasses=" +
Arrays.toString(autoConfigurationClasses) +
", autoConfigurationMetadata=" + autoConfigurationMetadata);
return new boolean[0];
}

}

EnableAutoConfiguration 配置自动配置

1
2
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jhj.springboot.autoconfig.MyConfiguration
1
2
3
4
5
6
7
8
9
10
@Configuration
public class MyConfiguration {


public MyConfiguration() {

System.out.println("MyConfiguration()");
}

}

FailureAnalyzer 配置自定的错误分析类

1
2
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.jhj.springboot.autoconfig.MyFailureAnalyzer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 自定义自己的错误分析器 FailureAnalyzer
* @author Administrator 2021/4/1 13:14
* @version 1.0
*/
public class MyFailureAnalyzer implements FailureAnalyzer {


@Override
public FailureAnalysis analyze(Throwable failure) {

System.out.println("MyFailureAnalyzer.analyze() failure=" + failure);
return new FailureAnalysis("MyFailureAnalyzer execute", "test spring.factories", failure);
}

}

TemplateAvailabilityProvider 配置模板的可用性提供者

1
2
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\
com.jhj.springboot.autoconfig.MyTemplateAvailabilityProvider
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 验证指定的模板是否支持
* @author Administrator 2021/4/1 13:22
* @version 1.0
*/
public class MyTemplateAvailabilityProvider implements TemplateAvailabilityProvider {


@Override
public boolean isTemplateAvailable(String view, Environment environment,
ClassLoader classLoader, ResourceLoader resourceLoader) {

System.out.println("MyTemplateAvailabilityProvider.isTemplateAvailable() view=" +
view + ", environment=" + environment + ", classLoader=" + classLoader +
"resourceLoader=" + resourceLoader);
return false;
}

}

EnvironmentPostProcessor 环境后置处理器

1
org.springframework.boot.env.EnvironmentPostProcessor=cn.telecom.starter.cloud.LoadProperties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class LoadProperties implements EnvironmentPostProcessor {


/**
* 文件名
*/
private static final String FILE_NAME = "test-starter-cloud.properties";

/**
* 版本锁
*/
private static int version = 0;


@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

// 通过版本号控制解决启动服务时加载两次的问题
if (version++ == 0) {

System.out.println("动态加载自定义配置:" + getClass().getName());
MutablePropertySources sources = environment.getPropertySources();
// 加载指定的配置文件(效率较高,但需要指定文件)
loadPropertySource(sources, FILE_NAME);
}
}

/**
* 加载配置文件资源
*
* @param sources
* @param file
*/
private void loadPropertySource(MutablePropertySources sources, String file) {

try {

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream(file);

if (is != null) {

Properties properties = new Properties();
properties.load(is);
PropertiesPropertySource propertySource = new PropertiesPropertySource("dynamic", properties);
sources.addLast(propertySource);
}
} catch (Exception e) {

e.printStackTrace();
}
}

}

作者声明

1
如有问题,欢迎指正!