spring自动装配
- 激活自动装配:@EnableAutoConfiguration
- 实现自动装配:@XXXConfiguration
- 配置自动装配实现:META-INF/spring.factories
理解 SpringApplication
- SpringApplication的运行
- SpringApplication.run
- SpringApplicationBuilder.run
- 配置spring bean源
public class SpirngBootApplication {
public static void main(String[] args) {
Set<String> stringSet = new HashSet<>();
stringSet.add(SpringConfig.class.getName());
SpringApplication springApplication = new SpringApplication();
springApplication.setSources(stringSet);
ConfigurableApplicationContext context = springApplication.run(args);
System.out.println(context.getBean(SpringConfig.class));
}
@SpringBootApplication
public static class SpringConfig {
}
}
- 推断web应用类型
public enum WebApplicationType {
/**
* The application should not run as a web application and should not start an
* embedded web server.
*/
NONE,
/**
* The application should run as a servlet-based web application and should start an
* embedded servlet web server.
*/
SERVLET,
/**
* The application should run as a reactive web application and should start an
* embedded reactive web server.
*/
REACTIVE
}
通过线程堆栈来推断引导类
private Class<?> deduceMainApplicationClass() {
try {
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if ("main".equals(stackTraceElement.getMethodName())) {
return Class.forName(stackTraceElement.getClassName());
}
}
}