一、构建Spring Boot示例应用
1、配置POM
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
...
2、编写应用
@SpringBootApplication
@NacosPropertySource(dataId = "learning", autoRefreshed = true)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、启动应用
██████╗ ███████╗██████╗ ███████╗███████╗██╗ ██╗███████╗
██╔══██╗██╔════╝██╔══██╗██╔════╝██╔════╝██║ ██║██╔════╝
██████╔╝█████╗ ██████╔╝███████╗█████╗ ██║ ██║███████╗
██╔═══╝ ██╔══╝ ██╔══██╗╚════██║██╔══╝ ██║ ██║╚════██║
██║ ███████╗██║ ██║███████║███████╗╚██████╔╝███████║
╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝ ╚═════╝ ╚══════╝
:: Perseus :: (v0.0.1)
2020-03-28 19:56:03.196 INFO 56682 --- [ main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.name=learning, dubbo.application.qos-enable=false, dubbo.config.multiple=true, dubbo.registry.address=nacos://mars.logithing.com:8848}
2020-03-28 19:56:03.270 INFO 56682 --- [ main] com.falcon.Application : Starting Application on 192.168.1.101 with PID 56682 (/Users/ryan/Documents/Bitbucket/learning/target/classes started by ryan in /Users/ryan/Documents/Bitbucket/mars)
2020-03-28 19:56:03.270 INFO 56682 --- [ main] com.falcon.Application : No active profile set, falling back to default profiles: default
2020-03-28 19:56:03.299 INFO 56682 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@68f4865: startup date [Sat Mar 28 19:56:03 CST 2020]; root of context hierarchy
2020-03-28 19:56:05.980 INFO 56682 --- [ main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : org.apache.dubbo.config.ApplicationConfig#0, class : org.apache.dubbo.config.ApplicationConfig] has been registered.
2020-03-28 19:56:05.980 INFO 56682 --- [ main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : org.apache.dubbo.config.ApplicationConfig#0] has been registered.
2020-03-28 19:56:05.981 INFO 56682 --- [ main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : org.apache.dubbo.config.RegistryConfig#0, class : org.apache.dubbo.config.RegistryConfig] has been registered.
2020-03-28 19:56:05.981 INFO 56682 --- [ main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : org.apache.dubbo.config.RegistryConfig#0] has been registered.
2020-03-28 19:56:05.981 INFO 56682 --- [ main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The dubbo config bean definition [name : org.apache.dubbo.config.ProtocolConfig#0, class : org.apache.dubbo.config.ProtocolConfig] has been registered.
2020-03-28 19:56:05.981 INFO 56682 --- [ main] .a.d.c.s.c.a.DubboConfigBindingRegistrar : The BeanPostProcessor bean definition [org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor] for dubbo config bean [name : org.apache.dubbo.config.ProtocolConfig#0] has been registered.
2020-03-28 19:56:06.035 INFO 56682 --- [ main] b.f.a.ServiceAnnotationBeanPostProcessor : [DUBBO] BeanNameGenerator bean can't be found in BeanFactory with name [org.springframework.context.annotation.internalConfigurationBeanNameGenerator], dubbo version: 2.7.3, current host: 192.168.1.101
2020-03-28 19:56:06.036 INFO 56682 --- [ main] b.f.a.ServiceAnnotationBeanPostProcessor : [DUBBO] BeanNameGenerator will be a instance of org.springframework.context.annotation.AnnotationBeanNameGenerator , it maybe a potential problem on bean name generation., dubbo version: 2.7.3, current host: 192.168.1.101
2020-03-28 19:56:06.044 INFO 56682 --- [ main] b.f.a.ServiceAnnotationBeanPostProcessor : [DUBBO] The BeanDefinition[Root bean: class [org.apache.dubbo.config.spring.ServiceBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] of ServiceBean has been registered with name : ServiceBean:com.falcon.service.HelloService:1.0.0:learning_service, dubbo version: 2.7.3, current host: 192.168.1.101
2020-03-28 19:56:06.044 INFO 56682 --- [ main] b.f.a.ServiceAnnotationBeanPostProcessor : [DUBBO] 1 annotated Dubbo's @Service Components { [Bean definition with name 'helloServiceImpl': Generic bean: class [com.falcon.service.impl.HelloServiceImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/Users/ryan/Documents/Bitbucket/learning/target/classes/com/falcon/service/impl/HelloServiceImpl.class]] } were scanned under package[com.falcon.service], dubbo version: 2.7.3, current host: 192.168.1.101
2020-03-28 19:56:06.045 WARN 56682 --- [ main] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance @Configuration bean definition 'org.apache.dubbo.spring.boot.autoconfigure.DubboRelaxedBinding2AutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2020-03-28 19:56:06.045 WARN 56682 --- [ main] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance @Configuration bean definition 'org.apache.dubbo.spring.boot.autoconfigure.DubboAutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2020-03-28 19:56:06.228 INFO 56682 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [org.apache.dubbo.spring.boot.autoconfigure.BinderDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-28 19:56:06.233 INFO 56682 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [org.apache.dubbo.spring.boot.autoconfigure.BinderDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-28 19:56:06.234 INFO 56682 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'relaxedDubboConfigBinder' of type [org.apache.dubbo.spring.boot.autoconfigure.BinderDubboConfigBinder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-28 19:56:06.432 INFO 56682 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8078 (http)
二、SpringApplication.run内部流程
1、实例化SpringApplication,调用run方法
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
2、SpringApplication构造过程
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));// 把sources设置到SpringApplication的sources属性中
this.webApplicationType = deduceWebApplicationType();// 判断是否是web程序,并设置到webEnvironment属性中
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class)); // 从spring.factories文件中找出key为ApplicationContextInitializer的类并实例化后设置到SpringApplication的initializers属性中
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));// 从spring.factories文件中找出key为ApplicationListener的类并实例化后设置到SpringApplication的listeners属性中
this.mainApplicationClass = deduceMainApplicationClass();
}
说明:
*deduceWebApplicationType:判断Web环境(Servlet/Reactive)
*setInitializers:加载ApplicationContextInitializer实例列表,根据类型、配置文件( META-INF/spring.factories)加载相应类,示例结果:
initializers = {ArrayList@952} size = 6
0 = {DubboApplicationContextInitializer@1057}
1 = {ContextIdApplicationContextInitializer@1058}
2 = {ConfigurationWarningsApplicationContextInitializer@1059}
3 = {ServerPortInfoApplicationContextInitializer@1060}
4 = {SharedMetadataReaderFactoryContextInitializer@1061}
5 = {AutoConfigurationReportLoggingInitializer@1062}
spring.factories中加载类的机制
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));加载spring.factories,以type为key读取相应的值
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,classLoader, args, names);//实例化读取到的值
AnnotationAwareOrderComparator.sort(instances);//将实例排序并返回
return instances;
}
*setListeners:加载ApplicationListener实例列表
listeners = {ArrayList@1055} size = 10
0 = {ConfigFileApplicationListener@1067}
1 = {AnsiOutputApplicationListener@1068}
2 = {LoggingApplicationListener@1069}
3 = {ClasspathLoggingApplicationListener@1070}
4 = {BackgroundPreinitializer@1071}
5 = {DelegatingApplicationListener@1072}
6 = {ParentContextCloserApplicationListener@1073}
7 = {ClearCachesApplicationListener@1074}
8 = {FileEncodingApplicationListener@1075}
9 = {LiquibaseServiceLocatorApplicationListener@1076}
*deduceMainApplicationClass:推断程序入口(即添加了SpringBootApplication的****App.class)
网友评论