1.springbootå 载类çè¿ç¨ï¼
2.springå¯å¨åç(springå·¥ç¨å¯å¨)
springbootå 载类çè¿ç¨ï¼
springbootåºç¨å¯å¨åçäºæ©å±urlclassloaderå®ç°åµå¥jarå è½½
1ãæ以ï¼springboot以org.springframework.boot.loader.JarLauncher为å¯å¨ç±»ï¼åèªå®ä¹äºLaunchedURLClassLoaderç¨æ¥å è½½BOOT-INFä¸çclassæ件以åBOOT-INF/libä¸çåµå¥jarå ã
2ãå 为SpringBootå®ç°äºJarå çåµå¥ï¼ä¸ä¸ªJarå å®ææ´ä¸ªç¨åºçè¿è¡ãå¼å ¥èªå®ä¹ç±»å è½½å¨å°±æ¯ä¸ºäºè§£å³è¿äºä¸ç¬¦åjarè§æ ¼çç±»æ æ³å è½½çé®é¢ã
3ãSpringBootApplicationä½ç¨ï¼æ 注å¨æ个类ä¸è¯´æè¿ä¸ªç±»æ¯SpringBootç主é 置类ï¼SpringBootè¿è¡è¿ä¸ªç±»çmainæ¹æ³æ¥å¯å¨SpringBootåºç¨ã
Springbootå¯å¨è¿ç¨
1ãé ç½®beançæºï¼å°±æ¯beançæ¥æºï¼å°±æ¯æ³¨è§£äºSpringBootApplicationçé£ä¸ªç±»ãæ¨æï¼æ¨æåºç¨ç±»åï¼æwebfluxï¼webservletï¼noneæ¨æ主类ï¼è¿ä¸ªæ¯éè¿çº¿ç¨å æ å®ç°ï¼æé ä¸ä¸ªè¿è¡æ¶å¼å¸¸ï¼æ¾å¼å¸¸å æ éé¢æ¾mianæå¨çé£ä¸ªç±»ã
2ãSpringBootçå¯å¨ä¸»è¦æ¯éè¿å®ä¾åSpringApplicationæ¥å¯å¨çã
3ãå¯å¨ï¼æ¯ä¸ªSpringBootç¨åºé½æä¸ä¸ªä¸»å ¥å£ï¼ä¹å°±æ¯mainæ¹æ³ï¼mainéé¢è°ç¨SpringApplication.run()å¯å¨æ´ä¸ªspring-bootç¨åºï¼è¯¥æ¹æ³æå¨ç±»éè¦ä½¿ç¨@SpringBootApplication注解ã
4ãå¨äºè§£SpringBootçå¯å¨æµç¨çæ¶åï¼æ们å çä¸ä¸ä¸ä¸ªSpringBootåºç¨æ¯å¦ä½å¯å¨çï¼å¦ä¸æ¯ä¸ä¸ªç®åçSpringBootç¨åºï¼é常çç®æ´ï¼ä»æ¯å¦ä½åå°çå¢ï¼æ们æ¥ä¸æ¥å°±å°ä¸æ¥æ¥å解ã
5ãé¦å éåæ§è¡ææéè¿SpringFactoriesLoaderï¼å¨å½åclasspathä¸çMETA-INF/spring.factoriesä¸æ¥æ¾ææå¯ç¨çSpringApplicationRunListeners并å®ä¾åãè°ç¨å®ä»¬çstarting()æ¹æ³ï¼éç¥è¿äºçå¬å¨SpringBootåºç¨å¯å¨ã
6ãSpringBootå¨å¯å¨æ¶ï¼éè¿ConfigurationClassPostProcessor.postProcessBeanFactoryå®æ对ä¾èµjarå ä¸XxAutopConfigurationç±»ç注åï¼èªç¶DubboAutoConfigurationä¹ä¼è¢«æ³¨åå°å®¹å¨å é¨ã
springBootåºç¨ä¸é ç½®æ件çå è½½è¿ç¨ç¼åä½ çconrollerï¼ä¹ä¸å®è¦å @RefreshScope注解ï¼ä¸å çè¯ï¼åµåµï¼å¯¹ä¸èµ·ï¼æ æ³è¿è¡ã
è³æ¤ï¼æ´ä¸ªenvironmentçé ç½®å è½½è¿ç¨å°±å®æäºã
ååªä¼æ ¹æ®classloaderçclasspathå表ï¼éå第ä¸ä¸ªåºç°çæ件ãå 为springbootå è½½é ç½®æ件æ¶æåºå±æ¯ä½¿ç¨çä¸é¢çæ¹æ³ï¼è¿ä¸¤ä¸ªæ¹æ³åªä¼è·åclassloaderç±»çucpå±æ§éé¢ç¬¬ä¸ä¸ªå¹é å°çå¼ã
Springbootåå§åæµç¨è§£æé¦å è¿éæéè¦çå¿ ç¶æ¯æ³¨è§£@SpringBootApplicationSpringBootApplication注解ç±å 个注解å¤åç»æï¼å ¶ä¸æ主è¦çå°±æ¯@SpringBootConfigurationã@EnableAutoConfigurationå@ComponentScanè¿ä¸ä¸ªã
application.propertiesé设置spring.jpa.hibernate.ddl-auto=createï¼springbootä¼éè¿å®ä½ç±»å建表ï¼åæ§è¡import.sqlã
springbootå¯å¨æµç¨å为两é¨åä¸é¨åæ¯åå¤é¶æ®µï¼ä¸é¨åæ¯è¿è¡é¶æ®µåå¤é¶æ®µä¸»è¦æè¿ä¹å æ¥ï¼é ç½®beançæºï¼å°±æ¯beançæ¥æºï¼å°±æ¯æ³¨è§£äºSpringBootApplicationçé£ä¸ªç±»ã
å¦ä¹ springbootéè¦äºè§£å ¶å¯å¨è¿ç¨ï¼å¯¹åççç解å¯ä»¥é¿å åçå¨CRUDå±é¢ï¼å¯¹åºå±æå¾å¤æ·±å ¥çç解å¯ä»¥å©åæ们æé¿ï¼è§£å³æ·±å±æ¬¡é®é¢ãSpringApplicationæé å½æ°runå½æ°prepareContextèµæºå è½½ï¼å¹¶ä¸åå§åã
å¨äºè§£SpringBootçå¯å¨æµç¨çæ¶åï¼æ们å çä¸ä¸ä¸ä¸ªSpringBootåºç¨æ¯å¦ä½å¯å¨çï¼å¦ä¸æ¯ä¸ä¸ªç®åçSpringBootç¨åºï¼é常çç®æ´ï¼ä»æ¯å¦ä½åå°çå¢ï¼æ们æ¥ä¸æ¥å°±å°ä¸æ¥æ¥å解ã
åå§åinitializeæ¹æ³æ§è¡å®ä¹åï¼ä¼è°ç¨runæ¹æ³ï¼å¼å§å¯å¨SpringBootãé¦å éåæ§è¡ææéè¿SpringFactoriesLoaderï¼å¨å½åclasspathä¸çMETA-INF/spring.factoriesä¸æ¥æ¾ææå¯ç¨çSpringApplicationRunListeners并å®ä¾åã
SpringBootçèªå¨è£ é (ä¸)1ãSpringBootçèªå¨è£ é æ¯æï¼SpringBootä¼èªå¨å°ä¸äºé 置类çbean注åå°ioc容å¨ï¼æ们å¯ä»¥å¨éè¦çå°æ¹ä½¿ç¨@Autowiredæ@Resourceç注解æ¥ä½¿ç¨å®ã
2ãSPIæ件æå¾å¤çæ©å±ç¹(å¾å¤çé 置项key)ï¼ä¸å¨ä¸ä¸åè¿°ãå¯ä»¥å¨ideaä¸ç´æ¥æç´¢spring.factoriesï¼å¯ä»¥çå°æå¾å¤ã
3ãèªå·±å¨æåä¸ä¸ªå¯å¨å¨çç»åï¼è¿éæ¥éäºï¼åå æ¯åªå¯¼å ¥äºä¸ä¸ªbeanèSpringBootæ æ³å¯å¨æå¡å¨è§£å³åæ³ä¸ï¼å°@Importæ¹æ@ConponentScanè¿æ ·å°±ä¼æ«æå级å ååå ã
4ãSpringçèªå¨è£ é æ¯æéè¿Spring容å¨èªå¨å°å°ä¸ä¸ªbeanä¸å¦ä¸ä¸ªbeanå ³èèµ·æ¥ãSpringæä¾äºä¸ç§èªå¨è£ é æ¹å¼ï¼é»è®¤çbyNameèªå¨è£ é ï¼å½ä¸ä¸ªbeançå±æ§å称ä¸å ¶ä»beançidç¸åæ¶ï¼Springä¼èªå¨å°è¯¥beanå¼ç¨å°è¿äºååçbeanä¸å»ã
springå¯å¨åç(springå·¥ç¨å¯å¨)
SpringBootå¯å¨åçåæ
èªå¨é ç½®æ ¸å¿ç±»SpringFactoriesLoader
ä¸é¢å¨è¯´@EnableAutoConfigurationçæ¶åæ说META-INFä¸çspring.factoriesæ件ï¼é£ä¹è¿ä¸ªæ件æ¯æä¹è¢«springå è½½å°çå¢ï¼å ¶å®å°±æ¯SpringFactoriesLoaderç±»ã
SpringFactoriesLoaderæ¯ä¸ä¸ªä¾Springå é¨ä½¿ç¨çéç¨å·¥åè£ è½½å¨å®éï¼SpringFactoriesLoaderéæ两个æ¹æ³ï¼
å¨è¿ä¸ªSpringBootåºç¨å¯å¨è¿ç¨ä¸ï¼SpringFactoriesLoaderåäºä»¥ä¸å 件äºï¼
å è½½ææMETA-INF/spring.factoriesä¸çInitializer
å è½½ææMETA-INF/spring.factoriesä¸çListener
å è½½EnvironmentPostProcessorï¼å 许å¨Springåºç¨æ建ä¹åå®å¶ç¯å¢é ç½®ï¼
æ¥ä¸æ¥å è½½PropertiesåYAMLçPropertySourceLoaderï¼é对SpringBootç两ç§é ç½®æ件çå è½½å¨ï¼
åç§å¼å¸¸æ åµçFailureAnalyzerï¼å¼å¸¸è§£éå¨ï¼
å è½½SpringBootå é¨å®ç°çåç§AutoConfiguration
模æ¿å¼æTemplateAvailabilityProviderï¼å¦FreemarkerãThymeleafãJspãVelocityçï¼
æ»å¾æ¥è¯´ï¼SpringFactoriesLoaderå@EnableAutoConfigurationé åèµ·æ¥ï¼æ´ä½åè½å°±æ¯æ¥æ¾spring.factoriesæ件ï¼å è½½èªå¨é 置类ã
æ´ä½å¯å¨æµç¨
å¨æ们æ§è¡å ¥å£ç±»çmainæ¹æ³ä¹åï¼è¿è¡SpringApplication.runï¼åé¢newäºä¸ä¸ªSpringApplication对象ï¼ç¶åæ§è¡å®çrunæ¹æ³ã
åå§åSpringApplicationç±»
å建ä¸ä¸ªSpringApplication对象æ¶ï¼ä¼è°ç¨å®èªå·±çinitializeæ¹æ³
æ§è¡æ ¸å¿runæ¹æ³
åå§åinitializeæ¹æ³æ§è¡å®ä¹åï¼ä¼è°ç¨runæ¹æ³ï¼å¼å§å¯å¨SpringBootã
é¦å éåæ§è¡ææéè¿SpringFactoriesLoaderï¼å¨å½åclasspathä¸çMETA-INF/spring.factoriesä¸æ¥æ¾ææå¯ç¨çSpringApplicationRunListeners并å®ä¾åãè°ç¨å®ä»¬çstarting()æ¹æ³ï¼æ¶²è½éç¥è¿äºçå¬å¨SpringBootåºç¨å¯å¨ã
å建并é ç½®å½åSpringBootåºç¨å°è¦ä½¿ç¨çEnvironmentï¼å æ¬å½åææçPropertySource以åProfileã
éåè°ç¨ææçSpringApplicationRunListenersçenvironmentPrepared()çæ¹æ³ï¼éç¥è¿äºçå¬å¨SpringBootåºç¨çEnvironmentå·²ç»å®æåå§åã
æå°SpringBootåºç¨çbannerï¼SpringApplicationçshowBannerå±æ§ä¸ºtrueæ¶ï¼å¦æclasspathä¸åå¨banner.txtæ件ï¼åæå°å ¶å 容ï¼å¦åæå°é»è®¤bannerã
æ ¹æ®å¯å¨æ¶è®¾ç½®çapplicationContextClassåå¨initializeæ¹æ³è®¾ç½®çwebEnvironmentï¼å建对åºçapplicationContextã
å建å¼å¸¸è§£æå¨ï¼ç¨å¨å¯å¨ä¸åçå¼å¸¸çæ¶åè¿è¡å¼å¸¸å¤ç(å æ¬è®°å½æ¥å¿ãéæ¾èµæºç)ã
设置SpringBootçEnvironmentï¼æ³¨åSpringBeanå称çåºååå¨BeanNameGeneratorï¼å¹¶è®¾ç½®èµæºå è½½å¨ResourceLoaderï¼éè¿SpringFactoriesLoaderå è½½ApplicationContextInitializeråå§åå¨ï¼è°ç¨initializeæ¹æ³ï¼å¯¹å建çApplicationContextè¿ä¸æ¥åå§åã
è°ç¨ææçSpringApplicationRunListenersçcontextPreparedæ¹æ³ï¼éç¥é¹ç»å·è¿äºListenerå½åApplicationContextå·²ç»å建å®æ¯ã
ææ ¸å¿çä¸æ¥ï¼å°ä¹åéè¿@EnableAutoConfigurationè·åçææé 置以åå ¶ä»å½¢å¼çIoC容å¨é ç½®å è½½å°å·²ç»åå¤å®æ¯çApplicationContextã
è°ç¨ææçSpringApplicationRunListenerçcontextLoadedæ¹æ³ï¼å è½½åå¤å®æ¯çApplicationContextã
è°ç¨refreshContextï¼æ³¨åä¸ä¸ªå ³éSpring容å¨çé©åShutdownHookï¼å½ç¨åºå¨åæ¢çæ¶åéæ¾èµæºï¼å æ¬ï¼éæ¯Beanï¼å ³éSpringBeançå建工åçï¼
注ï¼é©åå¯ä»¥å¨ä»¥ä¸å ç§åºæ¯ä¸è¢«è°ç¨ï¼
1ï¼ç¨åºæ£å¸¸éåº
2ï¼ä½¿ç¨System.exit()
3ï¼ç»ç«¯ä½¿ç¨Ctrl+C触åçä¸æ
4ï¼ç³»ç»å ³é
5ï¼ä½¿ç¨Killpidå½ä»¤ææ»è¿ç¨
è·åå½åææApplicationRunneråCommandLineRunneræ¥å£çå®ç°ç±»ï¼æ§è¡å ¶runæ¹æ³
éåææçSpringApplicationRunListenerçfinished()æ¹æ³ï¼å®æSpringBootçå¯å¨ã
springå·¥ä½åç
Springçå·¥ä½åçæ¯è®©ä¸ä¸ªå¯¹è±¡çå建ä¸ç¨newå°±å¯ä»¥èªå¨çç产ï¼å¨è¿è¡æ¶ä¸xmlSpringçé ç½®æ件æ¥é«å²©å¨æçå建对象åè°ç¨å¯¹è±¡ï¼èä¸éè¦éè¿ä»£ç æ¥å ³èã
Springæ¯ä¸ä¸ªå¼æ¾æºä»£ç ç设计å±é¢æ¡æ¶ï¼ä»è§£å³çæ¯ä¸å¡é»è¾å±åå ¶ä»åå±çæ¾è¦åé®é¢ï¼å æ¤å®å°é¢åæ¥å£çç¼ç¨ææ³è´¯ç©¿æ´ä¸ªç³»ç»åºç¨ã
springç¹ç¹æ¯1.æ¹ä¾¿è§£è¦ï¼ç®åå¼åã2.AOPç¼ç¨çæ¯æã3.声æå¼äºå¡çæ¯æã4.æ¹ä¾¿ç¨åºçæµè¯ã5.æ¹ä¾¿éæåç§ä¼ç§æ¡æ¶ã6.éä½JavaEEAPIç使ç¨é¾åº¦ã7.Javaæºç æ¯ç»å ¸å¦ä¹ èä¾ã
Springæ¡æ¶æ¯ç±äºè½¯ä»¶å¼åçå¤ææ§èå建çãSpring使ç¨çæ¯åºæ¬çJavaBeanæ¥å®æ以ååªå¯è½ç±EJBå®æçäºæ ãç¶èï¼Springçç¨éä¸ä» ä» éäºæå¡å¨ç«¯çå¼åãä»ç®åæ§ãå¯æµè¯æ§åæ¾è¦åæ§è§åº¦èè¨ï¼ç»å¤§é¨åJavaåºç¨é½å¯ä»¥ä»Springä¸å¸¦æ¶åçãSpringæ¯ä¸ä¸ªè½»é级çæ§å¶å转(IoC)åé¢ååé¢(AOP)ç容å¨æ¡æ¶ã
Springéè¿ä¸ç§ç§°ä½æ§å¶å转ï¼IoCï¼çææ¯ä¿è¿äºæ¾è¦åãå½åºç¨äºIoCï¼ä¸ä¸ªå¯¹è±¡ä¾èµçå ¶å®å¯¹è±¡ä¼éè¿è¢«å¨çæ¹å¼ä¼ éè¿æ¥ï¼èä¸æ¯è¿ä¸ªå¯¹è±¡èªå·±å建æè æ¥æ¾ä¾èµå¯¹è±¡ãä½ å¯ä»¥è®¤ä¸ºIoCä¸JNDIç¸åââä¸æ¯å¯¹è±¡ä»å®¹å¨ä¸æ¥æ¾ä¾èµï¼èæ¯å®¹å¨å¨å¯¹è±¡åæè¡å¾¡å§åæ¶ä¸ç对象请æ±å°±ä¸»å¨å°ä¾èµä¼ éç»å®ã
springçåçæ¯ä»ä¹ï¼ä¸ãIoC(Inversionofcontrol):æ§å¶å转\x0d\1ãIoCï¼\x0d\æ¦å¿µï¼æ§å¶æç±å¯¹è±¡æ¬èº«è½¬å容å¨ï¼ç±å®¹å¨æ ¹æ®é ç½®è ¢è¡«æ件å»å建å®ä¾å¹¶å建å个å®ä¾ä¹é´çä¾èµå ³ç³»\x0d\æ ¸å¿ï¼beanå·¥åï¼å¨Springä¸ï¼beanå·¥åå建çå个å®ä¾ç§°ä½bean\x0d\äºãAOP(Aspect-OrientedProgramming):é¢åæ¹é¢ç¼ç¨ã\x0d\1ã代çç两ç§æ¹å¼ï¼\x0d\éæ代çï¼\x0d\é对æ¯ä¸ªå ·ä½ç±»åå«ç¼å代çç±»ã\x0d\é对ä¸ä¸ªæ¥å£ç¼åä¸ä¸ªä»£çç±»ã\x0d\å¨æ代çï¼\x0d\é对ä¸ä¸ªæ¹é¢ç¼åä¸ä¸ªInvocationHandlerï¼ç¶ååç¨JDKåå°å ä¸çProxy类为åç§æ¥å£å¨æçæç¸åºç代çç±»ã\x0d\2ãAOPç主è¦åçï¼å¨æ代çã\x0d\Springå·¥ä½åç\x0d\Springå·²ç»ç¨è¿ä¸æ®µæ¶é´äºï¼æè§Springæ¯ä¸ªå¾ä¸éçæ¡æ¶ãå é¨ææ ¸å¿çå°±æ¯IOCäºï¼\x0d\å¨ææ³¨å ¥ï¼è®©ä¸ä¸ªå¯¹è±¡çå建ä¸ç¨newäºï¼å¯ä»¥èªå¨çç产ï¼è¿å ¶å®å°±æ¯å©ç¨javaéçåå°ï¼åå°å ¶å®å°±æ¯å¨è¿è¡æ¶å¨æçå»å建ãè°ç¨å¯¹è±¡ï¼Springå°±æ¯å¨è¿è¡æ¶ï¼è·xmlSpringçé ç½®æ件æ¥å¨æçå建对象ï¼åè°ç¨å¯¹è±¡éçæ¹æ³çã\x0d\Springè¿æä¸ä¸ªæ ¸å¿å°±æ¯AOPè¿ä¸ªå°±æ¯é¢ååé¢ç¼ç¨ï¼å¯ä»¥ä¸ºæä¸ç±»å¯¹è±¡è¿è¡çç£åæ§å¶ï¼ä¹å°±æ¯å¨è°ç¨è¿ç±»å¯¹è±¡çå ·ä½æ¹æ³çååå»è°ç¨ä½ æå®ç模åï¼ä»èè¾¾å°å¯¹ä¸ä¸ªæ¨¡åæ©å çåè½ãè¿äºé½æ¯éè¿é 置类达å°è°åºçã\x0d\Springç®çå«æ¡£æ¡ï¼å°±æ¯è®©å¯¹è±¡ä¸å¯¹è±¡ï¼æ¨¡åä¸æ¨¡åï¼ä¹é´çå ³ç³»æ²¡æéè¿ä»£ç æ¥å ³èï¼é½æ¯éè¿é 置类说æ管ççï¼Springæ ¹æ®è¿äºé ç½®å é¨éè¿åå°å»å¨æçç»è£ 对象ï¼\x0d\è¦è®°ä½ï¼Springæ¯ä¸ä¸ªå®¹å¨ï¼å¡æ¯å¨å®¹å¨éç对象æä¼æSpringææä¾çè¿äºæå¡ååè½ã\x0d\Springéç¨çæç»å ¸çä¸ä¸ªè®¾è®¡æ¨¡å¼å°±æ¯ï¼æ¨¡æ¿æ¹æ³æ¨¡å¼ã
SpringBootåºç¨å¯å¨åç(äº)æ©å±URLClassLoaderå®ç°åµå¥jarå è½½å¨ä¸ç¯æç« ãSpringBootåºç¨å¯å¨åç(ä¸)å°å¯å¨èæ¬åµå ¥jarãä¸ä»ç»äºSpringBootå¦ä½å°å¯å¨èæ¬ä¸RunnableJaræ´å为ExecutableJarçåçï¼ä½¿å¾çæçjar/waræ件å¯ä»¥ç´æ¥å¯å¨
æ¬ç¯å°ä»ç»SpringBootå¦ä½æ©å±URLClassLoaderå®ç°åµå¥jarçç±»(èµæº)å è½½ï¼ä»¥å¯å¨æ们çåºåæ£æç¨ã
é¦å 岩ä¼ï¼ä»ä¸ä¸ªç®åç示ä¾å¼å§
build.gradle
WebApp.java
æ§è¡gradlebuildæ建jarå ï¼éé¢å å«åºç¨ç¨åºã第ä¸æ¹ä¾èµä»¥åSpringBootå¯å¨ç¨åºï¼å ¶ç®å½ç»æå¦ä¸
æ¥çMANIFEST.MFçå 容(MANIFEST.MFæ件çä½ç¨è¯·èªè¡GOOGLE)
å¯ä»¥çå°ï¼jarçå¯å¨ç±»ä¸ºorg.springframework.boot.loader.JarLauncherï¼è并ä¸æ¯æ们çcom.manerfan.SpringBoot.theory.WebAppï¼åºç¨ç¨åºå ¥å£ç±»è¢«æ 记为äºStart-Class
jarå¯å¨å¹¶ä¸æ¯éè¿åºç¨ç¨åºå ¥å£ç±»ï¼èæ¯éè¿JarLauncher代çå¯å¨ãå ¶å®SpringBootæ¥æ3ä¸ä¸åçLauncherï¼JarLauncherãWarLauncherãPropertiesLauncher
SpringBoot使ç¨Launcher代çå¯å¨ï¼å ¶æéè¦çä¸ç¹ä¾¿æ¯å¯ä»¥èªå®ä¹ClassLoaderï¼ä»¥å®ç°å¯¹jaræ件å ï¼jarinjarï¼æå ¶ä»è·¯å¾ä¸jarãclassæèµæºæ件çå è½½
å ³äºClassLoaderçæ´å¤ä»ç»å¯åèãæ·±å ¥ç解JVMä¹ClassLoaderã
SpringBootæ½è±¡äºArchiveçæ¦å¿µï¼ä¸ä¸ªArchiveå¯ä»¥æ¯jarï¼JarFileArchiveï¼ï¼å¯ä»¥æ¯ä¸ä¸ªæ件ç®å½ï¼ExplodedArchiveï¼ï¼å¯ä»¥æ½è±¡ä¸ºç»ä¸è®¿é®èµæºçé»è¾å±ã
ä¸ä¾ä¸ï¼spring-boot-theory-1.0.0.jaræ¢ä¸ºä¸ä¸ªJarFileArchiveï¼spring-boot-theory-1.0.0.jar!/BOOT-INF/libä¸çæ¯ä¸ä¸ªjarå ä¹æ¯ä¸ä¸ªJarFileArchive
å°spring-boot-theory-1.0.0.jar解åå°ç®å½spring-boot-theory-1.0.0ï¼åç®å½spring-boot-theory-1.0.0为ä¸ä¸ªExplodedArchive
æç §å®ä¹ï¼JarLauncherå¯ä»¥å è½½å é¨/BOOT-INF/libä¸çjarå/BOOT-INF/classesä¸çåºç¨class
å ¶å®JarLauncherå®ç°å¾ç®å
å ¶ä¸»å ¥å£æ°å»ºäºJarLauncher并è°ç¨ç¶ç±»Launcherä¸çlaunchæ¹æ³å¯å¨ç¨åº
åå建JarLauncheræ¶ï¼ç¶ç±»ExecutableArchiveLauncheræ¾å°èªå·±æå¨çjarï¼å¹¶å建archive
å¨Launcherçlaunchæ¹æ³ä¸ï¼éè¿ä»¥ä¸archiveçgetNestedArchivesæ¹æ³æ¾å°/BOOT-INF/libä¸ææjarå/BOOT-INF/classesç®å½æ对åºçarchiveï¼éè¿è¿äºarchivesçurlçæLaunchedURLClassLoaderï¼å¹¶å°å ¶è®¾ç½®ä¸ºçº¿ç¨å¥½æ©¡ä¸ä¸æç±»å è½½å¨ï¼å¯å¨åºç¨
è³æ¤ï¼ææ§è¡æ们åºç¨ç¨åºä¸»å ¥å£ç±»çmainæ¹æ³ï¼ææåºç¨ç¨åºç±»æ件åå¯éè¿/BOOT-INF/classeså è½½ï¼ææä¾èµç第ä¸æ¹jaråå¯éè¿/BOOT-INF/libå è½½
å¨åæLaunchedURLClassLoaderåï¼é¦å äºè§£ä¸ä¸URLStreamHandler
javaä¸å®ä¹äºURLçæ¦å¿µï¼å¹¶å®ç°å¤ç§URLåè®®ï¼è§URLï¼*http**file**ftp**jar*çï¼ç»å对åºçURLConnectionå¯ä»¥çµæ´»å°è·ååç§åè®®ä¸çèµæº
对äºjarï¼æ¯ä¸ªjaré½ä¼å¯¹åºä¸ä¸ªurlï¼å¦
jar:file:/data/spring-boot-theory/BOOT-INF/lib/spring-aop-5.0.4.RELEASE.jar!/
jarä¸çèµæºï¼ä¹ä¼å¯¹åºä¸ä¸ªurlï¼å¹¶ä»¥'!/'åå²ï¼å¦
jar:file:/data/spring-boot-theory/BOOT-INF/lib/spring-aop-5.0.4.RELEASE.jar!/org/springframework/aop/SpringProxy.class
对äºåå§çJarFileURLï¼åªæ¯æä¸ä¸ª'!/'ï¼SpringBootæ©å±äºæ¤åè®®ï¼ä½¿å ¶æ¯æå¤ä¸ª'!/'ï¼ä»¥å®ç°jarinjarçèµæºï¼å¦
jar:file:/data/spring-boot-theory.jar!/BOOT-INF/lib/spring-aop-5.0.4.RELEASE.jar!/org/springframework/aop/SpringProxy.class
èªå®ä¹URLçç±»æ ¼å¼ä¸º[pkgs].[protocol].Handlerï¼å¨è¿è¡Launcherçlaunchæ¹æ³æ¶è°ç¨äºJarFile.registerUrlProtocolHandler()以注åèªå®ä¹çHandler
å¨å¤çå¦ä¸URLæ¶ï¼ä¼å¾ªç¯å¤ç'!/'åé符ï¼ä»æä¸å±åºåï¼å æé spring-boot-theory.jarçJarFileï¼åæé spring-aop-5.0.4.RELEASE.jarçJarFileï¼æåæé æåSpringProxy.classç
JarURLConnectionï¼éè¿JarURLConnectionçgetInputStreamæ¹æ³è·åSpringProxy.classå 容
ä»ä¸ä¸ªURLï¼å°è¯»åå ¶ä¸çå 容ï¼æ´ä¸ªè¿ç¨ä¸º
URLClassLoaderå¯ä»¥éè¿åå§çjaråè®®ï¼å è½½jarä¸ä»classæ件
LaunchedURLClassLoaderéè¿æ©å±çjaråè®®ï¼ä»¥å®ç°jarinjarè¿ç§æ åµä¸çclassæ件å è½½
æ建warå å¾ç®å
æ建åºçwarå ï¼å ¶ç®å½æºæ为
MANIFEST.MFå 容为
æ¤æ¶ï¼å¯å¨ç±»å为äºorg.springframework.boot.loader.WarLauncherï¼æ¥çWarLauncherå®ç°ï¼å ¶å®ä¸JarLauncher并æ 太大差å«
å·®å«ä» å¨äºï¼JarLauncherå¨æ建LauncherURLClassLoaderæ¶ï¼ä¼æç´¢BOOT-INF/classesç®å½åBOOT-INF/libç®å½ä¸jarï¼WarLauncherå¨æ建LauncherURLClassLoaderæ¶ï¼åä¼æç´¢WEB-INFO/classesç®å½åWEB-INFO/libåWEB-INFO/lib-provided两个ç®å½ä¸çjar
å¦æ¤ä¾èµï¼æ建åºçwar便æ¯æ两ç§å¯å¨æ¹å¼
PropretiesLauncherçå®ç°ä¸JarLauncherWarLauncherçå®ç°æ为ç¸ä¼¼ï¼éè¿PropretiesLauncherå¯ä»¥å®ç°æ´ä¸ºè½»éçthinjarï¼å ¶å®ç°æ¹å¼å¯èªè¡æ¥é æºç
SpringBootè¿è¡åçSpringBootæ¯ä¸ä¸ªåºäºSpringå¼åï¼éæäºå¤§é第ä¸æ¹åºé ç½®çjavawebå¼åæ¡æ¶
pom.xml
ç¶ä¾èµ
å ¶ä¸å®ä¸»è¦æ¯ä¾èµä¸ä¸ªç¶é¡¹ç®ï¼ä¸»è¦æ¯ç®¡ç项ç®çèµæºè¿æ»¤åæ件ã以åæä»¬å¯¼å ¥ä¾èµé»è®¤æ¯ä¸éè¦åçæ¬çã
å¯å¨å¨spring-boot-starter
springboot-boot-starter-xxx:spring-bootçåºæ¯å¯å¨å¨é御
spring-boot-starter-web:帮æä»¬å¯¼å ¥äºweb模åæ£å¸¸è¿è¡æä¾èµçç»ä»¶ã
springBootå°ææçåè½åºæ¯é½æ½ååºæ¥ï¼åæä¸ä¸ªä¸ªçstarter(å¯å¨å¨)ï¼åªéè¦å¨é¡¹ç®ä¸å¼å ¥è¿äºstarterå³å¯ï¼ææç¸å ³çä¾èµé½ä¼è¢«å¼è¿æ¥ï¼æ们è¦ç¨ä»ä¹åè½å°±å¯¼å ¥ä»ä¹æ ·çåºæ¯å¯å¨å¨å³å¯ã
@SpringBootApplication
ä½ç¨ï¼æ 注å¨æ个类ä¸è¯´æè¿ä¸ªç±»æ¯SpringBootç主é 置类ï¼SpringBootè¿è¡è¿ä¸ªç±»çmainæ¹æ³æ¥å¯å¨SpringBootåºç¨ã
è¿å ¥è¿ä¸ªæ³¨è§£ï¼éé¢å å«äºå¾å¤å ¶ä»æ³¨è§£
@ComponentScanä½ç¨ï¼èªå¨æ«æ并å 载符åæ¡ä»¶çç»ä»¶æè beanï¼å°è¿ä¸ªbeanå®ä¹å è½½å°IOC容å¨ä¸ã
@SpringBootConfigurationä½ç¨ï¼SpringBootçé 置类ï¼æ 注å¨æ个类ä¸ï¼è¡¨ç¤ºè¿æ¯ä¸ä¸ªå§¿åSpringBootçé 置类ã
è¿å ¥@SpringBootConfiguration注解æ¥çï¼è¿éç@Configuration说æè¿æ¯ä¸ä¸ªé 置类ï¼é 置类对åºSpringçxmlé ç½®æ件ã
继ç»æ¥ç@SpringBootConfigurationå å«çå ¶ä»æ³¨è§£
@EnableAutoConfigurationï¼å¼å¯èªå¨é ç½®åè½
è¿å ¥@EnableAutoConfiguration注解æ¥ç
@AutoConfigurationPackageèªå¨é ç½®å
@importï¼Springåºå±æ³¨è§£@importï¼ç»å®¹å¨ä¸å¯¼å ¥ä¸ä¸ªç»ä»¶
@Importï¼{ AutoConfigurationImportSelector.class}ï¼ç»å®¹å¨å¯¼å ¥ç»ä»¶
AutoConfigurationImportSelectorï¼èªå¨é ç½®å¯¼å ¥éæ©å¨ãé£ä¹å®å¯¼å ¥åªäºç»ä»¶çéæ©å¨å¢
è¿ä¸ªç±»ä¸æè¿æ ·ä¸ä¸ªæ¹æ³ï¼getCandidateConfiguration,源码crm源码带手机版源码èå¨è¿ä¸ªæ¹æ³ä¸æè°ç¨äºSpringFactoriesLoaderç±»çéææ¹æ³loadFactoryNames()æ¹æ³
è¿å ¥loadSpringFactoriesæ¹æ³
æ ¹æ®å ¨å±æç´¢Spring.factoriesï¼æå¼åæ¯èªå¨é ç½®çæ件ã
é便æå¼ä¸ä¸ªå ¶ä¸çèªå¨é 置类çï¼å®ä»¬é½åå岩æ¯javaConfigé 置类ï¼é½æ³¨å ¥äºä¸äºBean
æ以ï¼èªå¨é ç½®çæ£å®ç°æ¯ä»classpathä¸æ寻ææçMETA-INF/spring.factoriesé ç½®æ件ï¼å¹¶å°å ¶ä¸å¯¹åºçorg.springframework.boot.autoconfigureå ä¸çé 置项éè¿åå°å®ä¾å为对åºæ 注äº@ConfigurationçjavaConfigå½¢å¼çIOC容å¨é 置类ï¼ç¶åå°è¿äºé½æ±æ»æ为ä¸ä¸ªå®ä¾å¹¶å è½½å°IOC容å¨ä¸ã
ç»è®ºï¼
1.SpringBootå¨å¯å¨çæ¶åä»ç±»è·¯å¾ä¸çMETA-INF/spring.factoriesä¸è·åEnableAutoConfigurationæå®çå¼
2.å°è¿äºå¼ä½ä¸ºèªå¨é ç½®ç±»å¯¼å ¥å®¹å¨ï¼èªå¨é 置类就çæï¼å¸®æ们è¿è¡èªå¨é 置工ä½ã
3.æ´ä¸ªJ2EEçæ´ä½è§£å³æ¹æ¡åèªå¨é ç½®é½å¨springboot-autoConfigureçjarå ä¸ã
4.å®ä¼ç»å®¹å¨ä¸å¯¼å ¥é常å¤çèªå¨é 置类ï¼xxxAutoConfigurationï¼,å°±æ¯ç»å®¹å¨ä¸å¯¼å ¥è¿ä¸ªåºæ¯éè¦çææç»ä»¶ï¼å¹¶é 置好è¿äºç»ä»¶ã
5.æäºèªå¨é 置类ï¼å å»äºæ们æå¨ç¼åé ç½®æ³¨å ¥åè½ç»ä»¶ççå·¥ä½ã
SpringApplication
è¿ä¸ªç±»ä¸»è¦åäºä»¥ä¸å件äº
1.æ¨æåºç¨çç±»åæ¯æ®éç项ç®è¿æ¯web项ç®
2.æ¥æ¾å¹¶å è½½ææå¯ç¨åå§åå¨ï¼è®¾ç½®å°initializerså±æ§ä¸
3.æ¾åºææçåºç¨ç¨åºçå¬å¨ï¼è®¾ç½®å°listenerså±æ§ä¸
4.æ¨æ并设置mainæ¹æ³çå®ä¹ç±»ï¼æ¾å°è¿è¡ç主类
SpringbootBatchçå¯å¨åç-ConfigurationSpringbootæ´åäºwebåbatchï¼ä½æ¯ä»ä»¬è¯å®ä¸æ¯åä¸æ¡è·¯ï¼å¨springbootä¸ï¼ä¼æ¨æå½åçè¿è¡ç¯å¢ãthis.webApplicationType=WebApplicationType.deduceFromClasspath();
ä»ä¸ç¨¿æ¨±æå¯ä»¥çåºï¼Springå°è¯ä»classpathéæ¾å°ç¹å¾ç±»ï¼æ¥å¤æå½åappæ¯ä»ä¹ç±»åãå½ç¶è¿ç§å¤ææ¯æå±éæ§çï¼æå¯è½æ¯transitive带ææ¸è¿æ¥ä¸ä¸ªå¸¦æservletç类被å½æäºWebApplicationType.SERVLETï¼å®é ä¸æ¯ä¸ªWebApplicationType.NONE;ãå¦æä¸æ³ä»¥webè¿è¡å°±æ¯æ³è¿è¡batchå¯ä»¥å¨application.properties强è¡æå®WebApplicationType
å ·ä½åçä½ç¨ç请çä¸é¢çstacktrace
å½ä¸ä¸ªbatchapplicationéè¦å¯å¨ï¼éè¦é ç½®JobRepositoryï¼Datasourceççï¼ææçå¼å§é½æ¥èªä¸ä¸ªannotation@EnableBatchProcessing
å½å å ¥@EnableBatchProcessingæ¶ï¼BatchConfigurationSelectorå¼å§å¯å¨ï¼æä¹å¯å¨ç大家å¯ä»¥é®æä¸åèä¸é¢çstacktraceã
import类主è¦æ¯ç±ConfigurationClassPostProcessoræ¥å®ç°çãå½BatchConfigurationSelector被è°ç¨çæ¶åï¼æ们å¯ä»¥çå°ä»æ两æ¡æ¯è·¯ã
é£ä¹è¿ä¸¤æ¡è·¯æå¥ä¸åå¢ã主è¦æ¯jobå®ä¹çæ¹å¼ä¸åã
modular=trueçæ åµä¸ï¼ä¸é¢æ¯ä¸ä¸ªä¾å
å¯ä»¥æå¤ä¸ªåAp