本文持续更新中。
springboot版本 2.1.0
让我们带着以下问题:
- 一个springboot项目的banner是什么时候打印的?
- 数据库连接池这些对数据库进行设置的操作在哪里进行的?
- 一个项目启动后是怎样接收接口访问的?
开始我们的探秘之路?
一、探秘之路
- 首先,从springboot 的main方法着手
@SpringBootApplication
@ImportResource({
"classpath:applicationContext-all.xml"})
@EnableAspectJAutoProxy (exposeProxy = true)
@EnableTransactionManagement
@Component
@EnableOAuthServer
public class Application
{
public static void main( String[] args )
{
if(args != null && args.length>0) {
for(String arg:args) {
if(arg.startsWith("setuFilePath:")) {
String setuFilePath = arg.substring(13);
SetuSystemUtil.setuFilePath = setuFilePath;
System.out.println("使用外部配置文件:"+setuFilePath);
}
}
}
SetuSystemUtil.applicationName="founder_gateway";
SetuSystemUtil.sessionBeanClazz=SetuSession.class;
SpringApplication.run(Application.class, args);
}
}
为了指定默认启动类和默认设置,我们调用SpringApplication.run(Application.class, args)
启动项目。
- ?,现在,进入
org.springframework.boot.SpringApplication#run(java.lang.Class<?>, java.lang.String…)
方法。
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings.
* @param primarySource the primary source to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?> primarySource,
String... args) {
return run(new Class<?>[] {
primarySource }, args);
}
该方法可以运行一个SpringApplication
。run方法接收的第一个参数是一个Class<?>类型的数组。
- 继续。进入
org.springframework.boot.SpringApplication#run(java.lang.Class<?>[], java.lang.String[])
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified sources using default settings and user supplied arguments.
* @param primarySources the primary sources to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}