1.å¦ä½è¯ä»· React Native
2.springå¯å¨åç(springå·¥ç¨å¯å¨)
å¦ä½è¯ä»· React Native
React nativeå åå©ç¨äºFacebookçç°æè½®åï¼æ¯ä¸ä¸ªå¾ä¼ç§çéæä½åï¼å¹¶ä¸æç¸ä¿¡è¿ä¸ªå¢é对å端çäºè§£å¾æ·±å»ï¼å¦åä¸å¯è½è®©Native codeãéå± äºçº¿ãã
对åºå°å端å¼åï¼æ´ä¸ªç³»ç»ç»ææ¯è¿æ ·ï¼
JSX vs HTML
CSS-layout vs css
ECMAScript 6 vs ECMAScript 5
React native View vs DOM
æ éç¼è¯ï¼æå¨ç¬¬ä¸æ¬¡ç¼è¯äºipaè£ å¥½ä»¥åï¼å°±åä¹æ²¡æ´æ°è¿appï¼åªè¦æ´æ°äºç«¯çjs代ç ï¼reloadä¸ä¸ï¼æ´ä¸ªçé¢å°±å ¨åäºã
å¤æ°å¸å±ä»£ç é½æ¯JSXï¼ææNativeç»ä»¶é½æ¯æ ç¾åçï¼è¿å¯¹äºå端ç¨åºåæ¥è¯´ï¼éä½äºä¸å°å¦ä¹ ææ¬ï¼ä¹å¤§å¤§åå°äºä»£ç éãä¸ä¿¡ä½ å¯ä»¥ççJSXç¼è¯åç代ç ã
å¤ç¨Reactç³»ç»ï¼ä¹åå°äºä¸å®å¦ä¹ åå¼åææ¬ï¼æ´éè¦çæ¯å©ç¨äºReactéé¢çåå±ådiffæºå¶ãjså±ä¼ ç»Nativeå±çæ¯ä¸ä¸ªdiffåçjsonï¼ç¶åç±Nativeå°è¿ä¸ªæ°æ®æ å°æçæ£çå¸å±è§å¾ã
css-layoutä¹æ¯ç¹çä¹ç¬ï¼å端å¯ä»¥ç»§ç»ç¨çæçç±»cssæ¹å¼æ¥ç¼åå¸å±ï¼éè¿è¿ä¸ªå·¥å ·è½¬æ¢æconstrainå¸å±ã
ç³»
ç»åªæjs-objcçååè°ç¨ï¼å°±æ¯æåçUIç»ä»¶çæ¹æ³éè¿javascritcoreæè webviewï¼ä½çæ¬iOSï¼æ å°å°jsä¸æ¥ï¼æ´ä¸ªè°ç¨
è¿ç¨æ¯å¼æ¥çï¼è¿æ ·ç设计令React nativeå¯ä»¥è®©jsè¿è¡å¨æ¡é¢chromeä¸ï¼éè¿websocketè¿æ¥Native
codeåæ¡é¢chromeï¼æ大å°æ¹ä¾¿äºè°è¯ãå¯¹å ¶ä¸çæºå¶Bangçä¸ç¯æç« åå¾å¾è¯¦ç»ï¼æå°±ä¸æ¾äººçæ §äºï¼React Nativeéä¿¡æºå¶è¯¦è§£ « bangâs blog ãä½è¿æ ·è®¾è®¡ä¹ä¼å¸¦æ¥ä¸äºé®é¢ï¼åé¢è¯´ã
ç¹ææä½ä¹è¢«æ½è±¡æäºä¸ç»ç»ä»¶ï¼TouchableXXXï¼ï¼è¿ç§æ½è±¡æ¹å¼æ¯æå¨ä¹åå类似工ä½ä¸æ²¡ææ³å°çãfacebookè¿ååºNative为ä»ä¹åwebãææãä¸åçåå ï¼å®æ¶çç¹æåé¦ååæ¶è½åãReact Native è¿å¥ç¸åºæºå¶è®¾è®¡å¾å¾å®åï¼è½åNative codeé£æ ·æ§å¶æ´ä¸ªç¹ææä½çææè¿ç¨ã
Debug
ç¸å½æ¹ä¾¿ï¼ä¿®æ¹äºjs以åï¼éè¿å 建çnodejs
watcherç¼è¯æbundleï¼å¨æ¨¡æå¨éé¢æcmd+rå°±å¯ä»¥çå°ææãèä¸æcmd+dï¼å¯ä»¥æå¼ä¸ä¸ªchromeçªå£ï¼ææçjsé½ç§»å°äº
chromeéé¢è¿è¡ï¼æ以ä»ä¹æç¹åæ¥æè°ç¨æ ï¼é½ä¸å¨è¯ä¸ã
ä¸é¢çæ¢æ¯ç¹ç¹ä¹æ¯ä¼ç¹ï¼ä¸é¢è¯´è¯´ç¼ºç¹ï¼æè åºè¯¥è¯´ï¼ãä»ç¶éççé®é¢ãï¼å¨æçæ¥ï¼è¿ä¸ªæ¹æ¡å·²ç»è¶ è¶äºHybirdæ¹æ¡ã
ç³»
ç»ä»ç¶ï¼ä¸å¾ä¸ï¼ä¾èµåçç»ä»¶æ´é²åºæ¥çç»ä»¶åæ¹æ³ã举两个ä¾åï¼ScrollViewè¿ä¸ªç»ä»¶ï¼å¨Nativeå±æ¯æ大éäºä»¶
çï¼scrollViewWillBeginDraggingï¼
scrollViewWillEndDraggingï¼scrollViewDidEndDraggingççï¼è¿äºäºä»¶å¨ç°æççæ¬é½æ²¡ææ´é²ï¼åºæ¬ä¸
åä¸äºç»ä»¶èå¨ææãå¦å¤ï¼è¿ä¸ªçæ¬ä¸æ大éç»ä»¶æ¯iOS
onlyçï¼ActivityIndicatorIOSãDatePickerIOSãNavigatorIOSãPickerIOSã
SliderIOSãSwitchIOSãTabBarIOSãAlertIOSãAppStateIOSãLinkingIOSã
PushNotificationIOSãStatusBarIOSãVibrationIOSï¼åè¿æ¥çï¼å©ä½çé½æ¯ä¸äºæ½è±¡ç¨åº¦æ强çåºæ¬ç»ä»¶ãè¿
æ ·ï¼ç¨æ·å¿ é¡»å¨ä¸åçå¹³å°ä¸å两å¥ä»£ç ï¼èä¸ææè½åä»ç¶å¼ºçä¾èµ React native å¼å人åæ´é²çæ¥å£ã
ç±äºæå¤å±æ¯
Reactï¼å次å¦ä¹ ææ¬é«ï¼ä¸åå¾å¸¸çHybirdæ¹æ¡ï¼åªè¦å¤å¦å 个JS
APIå°±å¯ä»¥å¼å§å¹²æ´»äºãå½ç¶ï¼Reactç确让åç»å¼ååå¾ç®åäºä¸äºï¼è¿ä¹ä¸å¥å¤æ¥çï¼åºäºiOSï¼ãæ®ç¼ºä¸å ¨çï¼css-layoutï¼å¨
Reactçå è£ ä¸ï¼çç¡®æ¾å¾ä¸é£ä¹é¢ç®å¯æäºã
å¦å¤ï¼React Nativeä»ç¶å¾ä¸å®åãææ¡£è¿ä¸å ¨ï¼æåºæ¬ä¸æ¯ççä»ç示ä¾ä»£ç å®æçdemoï¼éæå°å·²æappçææ¡£ä¹æ¯ä»å¤©æåºæ¥ãæç §å®æ¹ç说æ³ï¼Androidçæ¬è¦å°åå¹´åæåå¸ï¼Blog | React ï¼å±æ¶æ´ä¸ªç³»ç»è®¾è®¡å¯è½è¿ä¼æå¾å¤§çååã
PSï¼å¨ä½¿ç¨Tabbarçæ¶åï¼ææåçåç°ä»ä»¬å± ç¶ç¨äºiconfontæ¹æ¡ï¼æç°å¨æ头ç项ç®ä¸ä¹æåæ ·çå®ç°ï¼ä¸è¿APIæä¹è®¾è®¡ä¸ç´å¾å¤´ç¼ãç»æï¼æåç°ä»æ¯è¿ä¹åçï¼
<TabBarItemIOS
name="blueTab"
icon={ _ix_DEPRECATED('favorites')}
....>
å¨ _ix_DEPRECATED çå®ä¹å¤ï¼æä¸å¥æ³¨éï¼ // TODO(nicklockwood): How can this 桔子分发源码fit our require system?
以ä¸ã
ä¸é¢æ¯ä¸å¨åï¼å¨React nativeè¿æ²¡å¼æºçæ¶åï¼éè¿å解ipaçä¸äºåæè¿ç¨ï¼æå ´è¶£çå¯ä»¥ççã
------------------------ç®åç²æ´çåå²çº¿--------------------
èæ¯åè°ç æ段
React
Nativeè¿æ²¡å¼æºï¼æè¿åç»éå å¼ãåç¼è¯ãäºFacebook Groupï¼è¿ä¸ªåºç¨æ¯ç¨React
Nativeå®ç°çï¼çipa代ç ï¼åºæ¥å ç¾ä¸ªJSæ件ï¼æ ¼å¼åä¸ä¸ï¼è±äºå 天æ¶é´è¯»äºä¸ä¸æºç ï¼å¯¹React
Nativeçå é¨æ ¸å¿æºå¶ç®æ¯æäºä¸ä¸ªåºæ¬äºè§£ã
React Nativeçæ ¸å¿å®ç°ï¼
å ç®å说å ç¹ï¼è¯¦ç»ççå头æ´æ°ã
1. React Nativeéé¢æ²¡æwebviewï¼è¿è´§ä¸æ¯Hybrid appï¼éé¢æ§è¡JSæ¯ç¨ç
JavascriptCoreã
2. å说React Nativeçæ ¸å¿ï¼iOS Native codeæä¾äºåæ¥ä¸ªæåºæ¬æ ¸å¿çç±»ï¼RCTDeviceEventEmitterã
RCTRenderingPerfçï¼ãæç»ä»¶ï¼RCTViewãRCTTextFieldãRCTTextViewã
RCTModalFullscreenViewçï¼ï¼ç¶åç±React NativeçJSé¨åï¼ç»æäºåæ¥ä¸ªåºæ¬ç»ä»¶ï¼PopoverãListviewçï¼ï¼äº¤ç±ä¸å±çä¸å¡æ¹æ¥ä½¿ç¨ï¼THGroupViewï¼ã
3. å°±å¦ä»ä»¬å¨å®£ä¼ æ¶æ说ï¼ä»ä»¬å®ç°äºä¸å¥ç±»ä¼¼cssçåéï¼ç¨æ¥è§£å³æ ·å¼é®é¢ï¼ç¸å½å¤æå强大ï¼é è¿ä¸ªæè½å°Nativeçæ ¸å¿ç»ä»¶ç»æJSå±çåºæ¬ç»ä»¶åç»æä¸å¡ç«¯çä¸å¡ç»ä»¶ï¼åºè¯¥æ¯éç¨facebook/css-layout · GitHubçCè¯è¨çæ¬å®ç°çï¼å¨pptä¸æ们çå°äºç±»ä¼¼flex-direction: columnä¸ç±»ç代ç ï¼è¿ä¸ªæ£æ¯css-layoutæ¯æçè¯æ³ï¼ã
4. å¨React Nativeä¸ï¼åJSçå·¥ç¨å¸è§£å³çæ¯ãå°åºæ¬ç»ä»¶æ¼è£ æå¯ç¨çReactç»ä»¶ãçé®é¢ï¼åNative Codeçå·¥ç¨å¸è§£å³çæ¯ãæä¾æ ¸å¿ç»ä»¶ï¼æä¾è¶³å¤çæ©å±æ§ãçµæ´»æ§åæ§è½ãçé®é¢ã
React Nativeç设计èèï¼
ReactJS对React Nativeæçç´æ¥çå½±åï¼æ没å¨ç产ç¯å¢ä¸ç¨è¿Reactï¼åªçè¿ä»£ç &ç¨è¿Angularï¼å¦ææ误请æåºï¼
ReactJSéé¢æè¿æ ·ç设计ï¼
1. ReactJS ç大工åå ¥å£createElementè¿åçä¸æ¯æ个å®ä½DOM对象ï¼èåªæ¯ä¸ä¸ªæ°ç»
2. éè¿æºç ä¸ ui/browser/ ç®å½ä¸ç代ç ï¼å°è¿ä¸ªæ°ç»è½¬æ¢æDOM
3. åºå±ç渲ææ ¸å¿æ¯å¯ä»¥æ´æ¢ç
å¦å¤ï¼Facebookèªå·±æJSXï¼css-layoutçå¼æºé¡¹ç®ï¼åºäºè¿äºï¼å¦æè¦åä¸ä¸ªç¨ JSæ¥å¼åNative appçä¸è¥¿ï¼å¾èªç¶å°±æ³å°äºä¸å¥æææççææ³ï¼
1. å° ui/browser éé¢ç代ç æ¿æ¢æä¸å¥ Native çæ¡¥æ¥JSï¼å®é ä¸ï¼iOSçæ¯éè¿
injectGenericComponentClassæ¹æ³ï¼å°æ ¸å¿ç»ä»¶çæ¹æ³æ³¨å ¥å°JSéé¢ ï¼ï¼å°±ç´æ¥å¤ç¨ReactçMVVMï¼èªå¨å°æ°æ®æ å°å°Nativeäº
2.
Native
codeéé¢å®ç°ä¸ç»æ ¸å¿APIï¼ä¸ç»æä¾æ ¸å¿ç»ä»¶çAPIï¼createãupdateãdeleteï¼ï¼ä¸ç»äºä»¶æ¹æ³ï¼ReactJSéé¢ç
EventEmitter ï¼ï¼ä¸ç»å¯¹cssè¿è¡è§£æï¼css-layoutï¼ä»¥åè¿åStyleçComputedStyleï¼React
Nativeéé¢å«meatureStyleï¼ã
è¿æ ·ï¼ç¨ä¸äºReactJSæ¬èº«çæææ ¸å¿åè½å设计æè·¯ï¼Nativeçå¼åä¹è¶³å¤ç®åã
é£ï¼React Nativeæ¯ä»ä¹ï¼
å ¶å®è¿ä¸è¥¿ä»Nativeå¼åæ¥è¯´ï¼ç¸å½äºéæ°åæäºä¸ä¸ªæµè§å¨æ¸²æå¼æ并ä¸å¥ä¸ä¸ªReactç壳ï¼ä»Webå¼åè§åº¦æ¥è¯´ï¼å°±æ¯æåæ¥Reactçå端æ¢æäºNative codeæ¥å®ç°ï¼å°±è·Flipboardæè¿æçReact Canvas ä¸æ ·ï¼ Flipboard · GitHubreact-canvas
React Nativeçä¼å¿åå£å¿ï¼ï¼
ä¼å¿ç¸å¯¹Hybird appæè Webappï¼
1. ä¸ç¨Webviewï¼å½»åºæè±äºWebview让人ä¸ç½ç交äºåæ§è½é®é¢
2. æè¾å¼ºçæ©å±æ§ï¼è¿æ¯å 为Native端æä¾çæ¯åºæ¬æ§ä»¶ï¼JSå¯ä»¥èªç±ç»å使ç¨
3. å¯ä»¥ç´æ¥ä½¿ç¨Nativeåççãçé¼ãå¨ç»ï¼å¨FB Groupè¿ä¸ªappéé¢ï¼é¢æ¿æ»åºå¸¦ä¸ç¹æå»å¼¹å¨ï¼é¢æ¿åºäºæ个ç¹å±å¼è¿ç§å¨ç»éå¤å¯è§ï¼è¿ç§å¨ç»ç¨Native codeæ¥åå°èä¸ç¢ï¼ä½æ¯ç¨Webæ¥åå°±é¾ä¸å é¾ï¼ã
ä¼å¿ç¸å¯¹äºNative appï¼
1. å¯ä»¥éè¿æ´æ°è¿ç«¯JSï¼ç´æ¥æ´æ°appï¼ä¸è¿è¿å¿«æ为å家大åNative appçæ é äºâ¦
å£å¿ï¼
1. æ©å±æ§ä»ç¶è¿è¿ä¸å¦webï¼ä¹è¿è¿ä¸å¦ç´æ¥åNative codeï¼è¿ä¸ªä¸ç¨åºè¯è§£éäºå§ï¼
2.
ä»Nativeå°Webï¼è¦åå¾å¤æ¦å¿µè½¬æ¢ï¼å¿å¿ é æåæ¹é½è¦å¦¥åãæ¯å¦webè¦ç¨ä¸å¥CSSçéå²çï¼Nativeéè¿css-layoutæ¿å°æç»æ ·
å¼å转æ¢ænativeåçç表达æ¹å¼ï¼æ¯å¦iOSçConstraint\origin\Centerçå±æ§ï¼ï¼åæ¯å¦å¨ç»ãå¦å¤ï¼è¥Androidå
iOSé½è¦åç¸åçå°è£ ï¼æ¦å¿µè½¬æ¢å°±æ´å¤æäºã
æ´æ°1ï¼æ·»å äºReact对React Nativeçå½±åã
æ´æ°2ï¼åºæ¬ç¡®å®å ¶ä½¿ç¨äº css-layoutï¼æ·»å äºå¯¹React Nativeçæ»ç»
æ´æ°3: React nativeå·²ç»å¼æºäºï¼ React Nativeï¼åªæiOSçãæåäºå 个demoï¼ç®åçäºçobjc代ç 并åå¼æºåçæ们çä¸äºç»è®ºï¼è§åæï¼äº¤åéªè¯ãç®åå°ä»å端工ç¨å¸åç³»ç»æ´ä½è§åº¦è¯´ä¸ä¸React nativeçç¹ç¹åä¼å£å§ã
æ´æ°4: è¡¥å äºå æ¡ä¼å¿åä¸å端å¼åçå¯¹ç §
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,èå¨è¿ä¸ªæ¹æ³ä¸æè°ç¨äº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