聽聽聽 灝嗛渶瑕佽繘琛屼簨鍔$殑澶氫釜鍘熷瓙DAO璁劇疆鍒癝ervice閲岋紝鍦⊿ervice灞傝繘琛屼簨鍔℃帶鍒跺拰浼氳瘽鎺у埗
聽聽聽 璁劇疆涓涓熀紜鐨刣ao錛屽嵆baseDAO 涓旂戶鎵夸箣HibernateDaoSupport錛岃屼笟鍔″璞″師瀛怐AO錛屾寔鏈変竴涓猙aseDAO鐨勫紩鐢紝閫氳繃setter娉ㄥ皠璁劇疆鍒頒笟鍔AO涓?/p>
聽聽聽 澶氫釜鍘熷瓙DAO錛岄氳繃setter娉ㄥ皠鍒癝ervice灞傘?/p>
2銆傚湪鏄劇ず灞傦紝涓瀵瑰鏄劇ず鏃訛紝鎻愮ずSession鍏抽棴鐨勯棶棰?/p>
聽聽聽 涓や釜瑙e喅鏂規硶錛?
聽聽聽 1錛夊湪one錛峵o錛峬any璁劇疆lazy錛漟alse錛屽嵆涓嶅歡榪熷姞杞藉瓙瀵硅薄錛屼絾榪欐牱鍙兘浼氬甫鏉ユц兘鐨勯棶棰橈紝鐗瑰埆鏄埗瀛愬叧緋繪ā鍨嬫槸鏍戠姸緇撴瀯鏃躲?br />聽聽聽
2錛変嬌鐢℉ibernate鎻愪緵鐨刼penSessiojnInView鎶鏈紝鍗沖湪瑙嗗浘灞傚紑鍚拰鍏抽棴Session錛屽湪service灞傝繘琛?
Transaction綆$悊錛岃繖涓緝涓虹畝鍗曪紝鍙渶瑕佽緗竴涓猣ilter鍗沖彲錛屽湪web.xml涓厤緗?/p>
<filter> 聽聽<filter-name>hibernateFilter</filter-name> 聽聽<filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class> 聽</filter> 聽<filter-mapping> 聽聽<filter-name>hibernateFilter</filter-name> 聽聽<url-pattern>/*</url-pattern> 聽</filter-mapping> |
闇瑕佹敞鎰忕殑鏄紝OpenSessionInViewFilter鍦ㄨ皟鐢ㄨ繃紼嬩腑錛岄渶瑕佸紩鐢⊿pring鐨凙pplicationContext錛岃岃 瀵硅薄瀹炰緥蹇呴』鏄繚瀛樺湪ServletContext涓殑錛屽洜姝ら渶瑕佸湪web.xml涓濮嬪寲Spring鐨勭幆澧冿紝澧炲姞閰嶇疆濡備笅錛?/p>
聽<context-param> 聽聽聽聽聽聽聽聽聽聽聽聽 <param-name>contextConfigLocation</param-name> 聽聽聽聽聽聽聽聽聽聽聽聽 <param-value>/WEB-INF/spring/applicationContext-hibernate.xml</param-value> 聽聽聽聽聽聽 聽</context-param> 聽聽聽聽 <servlet> |
鍏朵腑
<servlet-name>context</servlet-name> 聽聽聽聽聽聽聽聽聽聽聽聽 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 聽聽聽聽聽聽聽聽聽聽聽聽 <load-on-startup>1</load-on-startup> 聽聽聽 聽</servlet> |
琛ㄧず鍦ㄥ惎鍔ㄦ椂錛岃繘琛孲pring鐜鐨勫濮嬪寲錛屽洜姝や互鍚庡鏋滆寮曠敤Spring涓殑鏌愪釜bean瀹炰緥
濡備笅浠g爜鍗沖彲
org.springframework.context.ApplicationContext ctx = (org.springframework.context.ApplicationContext)application.getAttribute(org.springframework.web.context.WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); |
鐒跺悗璋冪敤ctx.getBean榪斿洖bean瀹炰緥
Spring涓嶅彧鏄疉OP鍜孖OC錛屾洿鎻愪緵浜嗗ぇ閲忕殑鍙噸鐢ㄦ妧鏈紝璀request涓殑閲嶈緙栫爜錛孲pring鎻愪緵浜嗙浉搴旂殑Filter錛?strong>org.springframework.web.filter.CharacterEncodingFilter 閫氳繃鐩存帴閰嶇疆錛屽嵆鍙В鍐砏eb搴旂敤涓璓OST鐨勪腑鏂囦貢鐮侀棶棰橈紝JMS鐨?strong>org.springframework.jms 鍖吢?JavaMail鐨?strong>org.springframework.mail鍖?/strong>絳夌瓑錛屾洿澶氱殑錛岃鍙傜湅Spring鐨剅eference錛?/p>
Spring鑳芥敼鍠勭幇鏈変唬鐮佺粨鏋勶紝澶ч噺鐨勫噺灝慼ard code錛屽姞蹇紑鍙戣繘搴︼紱
涓鐩存湁涓縐嶆劅瑙夛細緋葷粺鐨勬瀯寤烘槸綾諱技鐨勬惌縐湪