SpringBoot和Bean的初始化過程

1、SpringBoot的入口

SpringApplication.run(Application.class, args);

2、SpringBootWeb項目

最少需要導入jar包

  • org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE

這個jar包默認依賴以下jar

  • org.springframework.boot:spring-boot-starter-json:2.0.5.RELEASE
  • org.springframework.boot:spring-boot-starter:2.0.6.RELEASE
  • org.springframework.boot:spring-boot-starter-tomcat:2.0.5.RELEASE
  • org.hibernate.validator:hibernate-validator:6.0.12.Final
  • org.springframework:spring-webmvc:5.0.9.RELEASE

實現自動配置的類:WebMvcAutoConfiguration

 

3、當使用@Autowired注入bean的時候,調用棧,調用線程main

	  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(AutowiredAnnotationBeanPostProcessor.java:231)
	  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1013)
	  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
	  - locked <0x312e> (a java.lang.Object)
	  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	  at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$134.178917238.getObject(Unknown Source:-1)
	  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	  - locked <0x172a> (a java.util.concurrent.ConcurrentHashMap)
	  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	  at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
	  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
	  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
	  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
	  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
	  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
	  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1336)
	  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
	  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	  at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$134.178917238.getObject(Unknown Source:-1)
	  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
	  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548)
	  - locked <0x3139> (a java.lang.Object)
	  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
	  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
	  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
	  at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
	  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
	  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)

可以看到棧頂是AutowiredAnnotationBeanPostProcessor 的postProcessMergedBeanDefinition方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章