如题所述
1ãBeançç®ä»
å¨Springä¸ï¼é£äºç»æåºç¨ç¨åºç主ä½(backbone)åç±Spring IoC容å¨æ管çç对象ï¼è¢«ç§°ä¹ä¸ºbeanã ç®åå°è®²ï¼beanå°±æ¯ç±Spring容å¨åå§åãè£ é å管çç对象ï¼é¤æ¤ä¹å¤ï¼beanå°±ä¸åºç¨ç¨åºä¸çå ¶ä»å¯¹è±¡æ²¡æä»ä¹åºå«äºã èbeanå®ä¹ä»¥åbeanç¸äºé´çä¾èµå ³ç³»å°éè¿é ç½®å æ°æ®æ¥æè¿°ã
2ãBeançä½ç¨å
å建ä¸ä¸ªbeanå®ä¹ï¼å ¶å®è´¨æ¯ç¨è¯¥beanå®ä¹å¯¹åºçç±»æ¥å建çæ£å®ä¾çâé æ¹(recipe)âãæbeanå®ä¹çæä¸ä¸ªé æ¹å¾ææä¹ï¼å®ä¸classå¾ç±»ä¼¼ï¼åªæ ¹æ®ä¸å¼ âå¤æ¹âå°±å¯ä»¥å建å¤ä¸ªå®ä¾ã
ä¸ä» å¯ä»¥æ§å¶æ³¨å ¥å°å¯¹è±¡ä¸çåç§ä¾èµåé ç½®å¼ï¼è¿å¯ä»¥æ§å¶è¯¥å¯¹è±¡çä½ç¨åãè¿æ ·å¯ä»¥çµæ´»éæ©æ建对象çä½ç¨åï¼èä¸å¿ å¨Java Class级å®ä¹ä½ç¨åãSpring Frameworkæ¯æäºç§ä½ç¨åï¼å ¶ä¸æä¸ç§åªè½ç¨å¨åºäºwebçSpring ApplicationContextï¼ã
1ï¼singleton
å½ä¸ä¸ªbeançä½ç¨å为singleton, é£ä¹Spring IoC容å¨ä¸åªä¼åå¨ä¸ä¸ªå ±äº«çbeanå®ä¾ï¼å¹¶ä¸ææ对beanç请æ±ï¼åªè¦idä¸è¯¥beanå®ä¹ç¸å¹é ï¼ååªä¼è¿åbeançåä¸å®ä¾ã
注æï¼Singletonä½ç¨åæ¯Springä¸ç缺çä½ç¨åãè¦å¨XMLä¸å°beanå®ä¹æsingletonï¼å¯ä»¥è¿æ ·é ç½®ï¼
1
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton">
2ï¼prototype
ä¸ä¸ªbeanå®ä¹å¯¹åºå¤ä¸ªå¯¹è±¡å®ä¾ãPrototypeä½ç¨åçbeanä¼å¯¼è´å¨æ¯æ¬¡å¯¹è¯¥bean请æ±ï¼å°å ¶æ³¨å ¥å°å¦ä¸ä¸ªbeanä¸ï¼æè 以ç¨åºçæ¹å¼è°ç¨å®¹å¨çgetBean()æ¹æ³ï¼æ¶é½ä¼å建ä¸ä¸ªæ°çbeanå®ä¾ãæ ¹æ®ç»éªï¼å¯¹æç¶æçbeanåºè¯¥ä½¿ç¨prototypeä½ç¨åï¼è对æ ç¶æçbeanååºè¯¥ä½¿ç¨singletonä½ç¨åã
3ï¼request
å¨ä¸æ¬¡HTTP请æ±ä¸ï¼ä¸ä¸ªbeanå®ä¹å¯¹åºä¸ä¸ªå®ä¾ï¼å³æ¯æ¬¡HTTP请æ±å°ä¼æåèªçbeanå®ä¾ï¼ å®ä»¬ä¾æ®æ个beanå®ä¹å建èæã该ä½ç¨åä» å¨åºäºwebçSpring ApplicationContextæ å½¢ä¸ææã
èèä¸é¢beanå®ä¹ï¼
1
<bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
é对æ¯æ¬¡HTTP请æ±ï¼Spring容å¨ä¼æ ¹æ®loginAction beanå®ä¹å建ä¸ä¸ªå ¨æ°çLoginAction beanå®ä¾ï¼ ä¸è¯¥loginAction beanå®ä¾ä» å¨å½åHTTP requestå ææï¼å æ¤å¯ä»¥æ ¹æ®éè¦æ¾å¿çæ´æ¹æ建å®ä¾çå é¨ç¶æï¼ èå ¶ä»è¯·æ±ä¸æ ¹æ®loginAction beanå®ä¹å建çå®ä¾ï¼å°ä¸ä¼çå°è¿äºç¹å®äºæ个请æ±çç¶æååã å½å¤ç请æ±ç»æï¼requestä½ç¨åçbeanå®ä¾å°è¢«éæ¯ã
4ï¼session
å¨ä¸ä¸ªHTTP Sessionä¸ï¼ä¸ä¸ªbeanå®ä¹å¯¹åºä¸ä¸ªå®ä¾ã该ä½ç¨åä» å¨åºäºwebçSpring ApplicationContextæ å½¢ä¸ææã
èèä¸é¢beanå®ä¹ï¼
1
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
é对æ个HTTP Sessionï¼Spring容å¨ä¼æ ¹æ®userPreferences beanå®ä¹å建ä¸ä¸ªå ¨æ°çuserPreferences beanå®ä¾ï¼ ä¸è¯¥userPreferences beanä» å¨å½åHTTP Sessionå ææã ä¸requestä½ç¨åä¸æ ·ï¼å¯ä»¥æ ¹æ®éè¦æ¾å¿çæ´æ¹æå建å®ä¾çå é¨ç¶æï¼èå«çHTTP Sessionä¸æ ¹æ®userPreferenceså建çå®ä¾ï¼ å°ä¸ä¼çå°è¿äºç¹å®äºæ个HTTP Sessionçç¶æååã å½HTTP Sessionæç»è¢«åºå¼çæ¶åï¼å¨è¯¥HTTP Sessionä½ç¨åå çbeanä¹ä¼è¢«åºå¼æã
5ï¼global session
å¨ä¸ä¸ªå ¨å±çHTTP Sessionä¸ï¼ä¸ä¸ªbeanå®ä¹å¯¹åºä¸ä¸ªå®ä¾ãå ¸åæ åµä¸ï¼ä» å¨ä½¿ç¨portlet contextçæ¶åææã该ä½ç¨åä» å¨åºäºwebçSpring ApplicationContextæ å½¢ä¸ææã
èèä¸é¢beanå®ä¹ï¼
1
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global sessionä½ç¨å类似äºæ åçHTTP Sessionä½ç¨åï¼ä¸è¿ä» ä» å¨åºäºportletçwebåºç¨ä¸æææä¹ãPortletè§èå®ä¹äºå ¨å±Sessionçæ¦å¿µï¼å®è¢«æææææ个portlet webåºç¨çåç§ä¸åçportletæå ±äº«ãå¨global sessionä½ç¨åä¸å®ä¹çbean被éå®äºå ¨å±portlet Sessionççå½å¨æèå´å ã
å¨Springä¸ï¼é£äºç»æåºç¨ç¨åºç主ä½(backbone)åç±Spring IoC容å¨æ管çç对象ï¼è¢«ç§°ä¹ä¸ºbeanã ç®åå°è®²ï¼beanå°±æ¯ç±Spring容å¨åå§åãè£ é å管çç对象ï¼é¤æ¤ä¹å¤ï¼beanå°±ä¸åºç¨ç¨åºä¸çå ¶ä»å¯¹è±¡æ²¡æä»ä¹åºå«äºã èbeanå®ä¹ä»¥åbeanç¸äºé´çä¾èµå ³ç³»å°éè¿é ç½®å æ°æ®æ¥æè¿°ã
2ãBeançä½ç¨å
å建ä¸ä¸ªbeanå®ä¹ï¼å ¶å®è´¨æ¯ç¨è¯¥beanå®ä¹å¯¹åºçç±»æ¥å建çæ£å®ä¾çâé æ¹(recipe)âãæbeanå®ä¹çæä¸ä¸ªé æ¹å¾ææä¹ï¼å®ä¸classå¾ç±»ä¼¼ï¼åªæ ¹æ®ä¸å¼ âå¤æ¹âå°±å¯ä»¥å建å¤ä¸ªå®ä¾ã
ä¸ä» å¯ä»¥æ§å¶æ³¨å ¥å°å¯¹è±¡ä¸çåç§ä¾èµåé ç½®å¼ï¼è¿å¯ä»¥æ§å¶è¯¥å¯¹è±¡çä½ç¨åãè¿æ ·å¯ä»¥çµæ´»éæ©æ建对象çä½ç¨åï¼èä¸å¿ å¨Java Class级å®ä¹ä½ç¨åãSpring Frameworkæ¯æäºç§ä½ç¨åï¼å ¶ä¸æä¸ç§åªè½ç¨å¨åºäºwebçSpring ApplicationContextï¼ã
1ï¼singleton
å½ä¸ä¸ªbeançä½ç¨å为singleton, é£ä¹Spring IoC容å¨ä¸åªä¼åå¨ä¸ä¸ªå ±äº«çbeanå®ä¾ï¼å¹¶ä¸ææ对beanç请æ±ï¼åªè¦idä¸è¯¥beanå®ä¹ç¸å¹é ï¼ååªä¼è¿åbeançåä¸å®ä¾ã
注æï¼Singletonä½ç¨åæ¯Springä¸ç缺çä½ç¨åãè¦å¨XMLä¸å°beanå®ä¹æsingletonï¼å¯ä»¥è¿æ ·é ç½®ï¼
1
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton">
2ï¼prototype
ä¸ä¸ªbeanå®ä¹å¯¹åºå¤ä¸ªå¯¹è±¡å®ä¾ãPrototypeä½ç¨åçbeanä¼å¯¼è´å¨æ¯æ¬¡å¯¹è¯¥bean请æ±ï¼å°å ¶æ³¨å ¥å°å¦ä¸ä¸ªbeanä¸ï¼æè 以ç¨åºçæ¹å¼è°ç¨å®¹å¨çgetBean()æ¹æ³ï¼æ¶é½ä¼å建ä¸ä¸ªæ°çbeanå®ä¾ãæ ¹æ®ç»éªï¼å¯¹æç¶æçbeanåºè¯¥ä½¿ç¨prototypeä½ç¨åï¼è对æ ç¶æçbeanååºè¯¥ä½¿ç¨singletonä½ç¨åã
3ï¼request
å¨ä¸æ¬¡HTTP请æ±ä¸ï¼ä¸ä¸ªbeanå®ä¹å¯¹åºä¸ä¸ªå®ä¾ï¼å³æ¯æ¬¡HTTP请æ±å°ä¼æåèªçbeanå®ä¾ï¼ å®ä»¬ä¾æ®æ个beanå®ä¹å建èæã该ä½ç¨åä» å¨åºäºwebçSpring ApplicationContextæ å½¢ä¸ææã
èèä¸é¢beanå®ä¹ï¼
1
<bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
é对æ¯æ¬¡HTTP请æ±ï¼Spring容å¨ä¼æ ¹æ®loginAction beanå®ä¹å建ä¸ä¸ªå ¨æ°çLoginAction beanå®ä¾ï¼ ä¸è¯¥loginAction beanå®ä¾ä» å¨å½åHTTP requestå ææï¼å æ¤å¯ä»¥æ ¹æ®éè¦æ¾å¿çæ´æ¹æ建å®ä¾çå é¨ç¶æï¼ èå ¶ä»è¯·æ±ä¸æ ¹æ®loginAction beanå®ä¹å建çå®ä¾ï¼å°ä¸ä¼çå°è¿äºç¹å®äºæ个请æ±çç¶æååã å½å¤ç请æ±ç»æï¼requestä½ç¨åçbeanå®ä¾å°è¢«éæ¯ã
4ï¼session
å¨ä¸ä¸ªHTTP Sessionä¸ï¼ä¸ä¸ªbeanå®ä¹å¯¹åºä¸ä¸ªå®ä¾ã该ä½ç¨åä» å¨åºäºwebçSpring ApplicationContextæ å½¢ä¸ææã
èèä¸é¢beanå®ä¹ï¼
1
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
é对æ个HTTP Sessionï¼Spring容å¨ä¼æ ¹æ®userPreferences beanå®ä¹å建ä¸ä¸ªå ¨æ°çuserPreferences beanå®ä¾ï¼ ä¸è¯¥userPreferences beanä» å¨å½åHTTP Sessionå ææã ä¸requestä½ç¨åä¸æ ·ï¼å¯ä»¥æ ¹æ®éè¦æ¾å¿çæ´æ¹æå建å®ä¾çå é¨ç¶æï¼èå«çHTTP Sessionä¸æ ¹æ®userPreferenceså建çå®ä¾ï¼ å°ä¸ä¼çå°è¿äºç¹å®äºæ个HTTP Sessionçç¶æååã å½HTTP Sessionæç»è¢«åºå¼çæ¶åï¼å¨è¯¥HTTP Sessionä½ç¨åå çbeanä¹ä¼è¢«åºå¼æã
5ï¼global session
å¨ä¸ä¸ªå ¨å±çHTTP Sessionä¸ï¼ä¸ä¸ªbeanå®ä¹å¯¹åºä¸ä¸ªå®ä¾ãå ¸åæ åµä¸ï¼ä» å¨ä½¿ç¨portlet contextçæ¶åææã该ä½ç¨åä» å¨åºäºwebçSpring ApplicationContextæ å½¢ä¸ææã
èèä¸é¢beanå®ä¹ï¼
1
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global sessionä½ç¨å类似äºæ åçHTTP Sessionä½ç¨åï¼ä¸è¿ä» ä» å¨åºäºportletçwebåºç¨ä¸æææä¹ãPortletè§èå®ä¹äºå ¨å±Sessionçæ¦å¿µï¼å®è¢«æææææ个portlet webåºç¨çåç§ä¸åçportletæå ±äº«ãå¨global sessionä½ç¨åä¸å®ä¹çbean被éå®äºå ¨å±portlet Sessionççå½å¨æèå´å ã
温馨提示:答案为网友推荐,仅供参考