銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/tst' title='tst'>tst
琛ュ厖 ack sql 瀹瑰櫒 property nfa str rod鎺у埗鍙嶈浆Inverse Of Control鐨勬紨鍙橈細
鍦ㄤ箣鍓嶇殑鍘熺敓Javaweb椤圭洰鐨勯棶棰橈細
鎴戜滑涓夊眰鏋舵瀯姣忎竴灞備箣闂寸殑鑱旂郴鏄繖鏍风殑锛?/p>
鐢盙radeDao鎺ュ彛鎸囧悜GradeDaoImpl
鍐嶇敱GradeService鎸囧悜GradeServiceImpl
浣嗘槸鎴戜滑鍙戠幇锛屽鏋滆繖鏍峰啓姝诲湪姣忎竴灞傜殑纭紪鐮佷腑鐨勮瘽锛岄偅涔堟帴鍙g殑瀛樺湪灏卞け鍘讳簡鎰忎箟
瀵硅薄鐨勫垱寤烘槸鐢辫繖涓笂灞傜殑鍐呴儴鎵€鍐冲畾
鎴戜滑鏄笇鏈涙瘡涓€灞傜殑鑱旂郴鏈€灏忓寲锛屼絾鏄張涓嶈兘瀹屽叏闅旂粷鍏崇郴锛岀粍浠跺氨鍍忕粍瑁呯殑濂界殑闆朵欢
鐩镐簰涔嬮棿鐨勪緷璧栭檷浣庡埌鏈€灏忥紝姣斿杩欎釜daoImpl鎴戦渶瑕佹浛鎹紝灏辨牴鎹疄鐜扮殑dao鎺ュ彛灏卞彲浠ユ湁
mysql瀹炵幇锛孫racle瀹炵幇锛孭ostgreSQL瀹炵幇绛夌瓑
瀵硅薄鐨勫垱寤哄彲浠ョЩ浜ゅ埌澶栭儴锛屾垜浠湪鍐呴儴璁剧疆setter娉ㄥ叆鍗冲彲锛屾垨鑰呬娇鐢ㄥ甫鍙傛瀯閫犲櫒
閫氳繃setter鎴栬€呭甫鍙傛瀯閫犲櫒锛屾垜浠彲浠ヤ粠鏈眰鐨勫唴閮ㄥ垱寤哄璞¤繖涓帶鍒舵潈鍔涳紝绉讳氦鍒颁簡澶栭潰
铏界劧杩欒繕鏄‖缂栫爜涓?/p>
鎺ヤ笅鏉ユ垜浠氨鍒涘缓涓€涓伐鍘傜被锛岀敱宸ュ巶瀹炵幇瀵硅薄鐨勫垱寤猴紝鑰岃繖涓涓猴紝闈炲父鍍忕敓浜т骇鍝?/p>
涓€涓釜瀵硅薄鍗虫垜浠殑涓氬姟鐨勪骇鍝?/p>
瀵硅薄鐨勬敞鍏ヤ氦鐢卞伐鍘傜敓浜х粰浜?/p>
浣嗘槸鎴戜滑鏄笉鏄彂鐜帮紝宸ュ巶鐨勪骇鍝佽繕鏄浐瀹氱殑锛熺劧鍚庡啀鍍廕dbc涓€鏍凤紝
閫氳繃璇诲彇閰嶇疆鏂囦欢鐨勫舰寮忚В闄よ€﹀悎
棣栧厛鏄厤缃枃浠讹細bean.properties
渚嬪杩欓噷鎴戝氨閰嶇疆鍙﹀涓€绉嶅疄鐜?/p>
AaaDao = ioc.dao.AaaDaoImpl2
AaaService = ioc.service.AaaServiceImpl2
杩欓噷鍙槸閿€煎绠€鍗曠殑瀛楃涓?/p>
鐒跺悗鏀归€犳垜浠殑BeanFactory
閫氳繃鍔犺浇鐨勬椂鍊欒鍙栭厤缃枃浠剁洿鎺ョ敓鎴愬璞℃斁杩涜繖涓猰ap瀹瑰櫒涓?/p>
杩欎釜瀹瑰櫒涓嶅氨鏄痓ean鐨勫鍣ㄤ簡鍚楋紝閿搴旂殑瀹炵幇绫诲悕锛屽€煎嵆瀵硅薄
put鏀捐繘鍘荤殑key搴旇鎹alue杩涘幓銆傘€傘€傛垜鏈夌偣鍒嗕笉娓?/span>
浣嗚繖涓嶆槸閲嶇偣锛屾垜浠笅闈㈣幏鍙栧璞$殑鏂规硶灏辨槸浠庡鍣ㄩ噷闈㈣幏鍙?/p>
杩欏氨闇€瑕乲ey浣滀负鍙傛暟锛?/p>
鍐嶅姞涓婃硾鍨嬶紝杩欐牱鎴戜滑灏变笉闇€瑕佽繘琛屽己杞簡
浣嗘槸鏈変釜闂灏辨槸dao涔熸槸鎺ュ彛鍟婏紝鎺ュ彛鎸囧悜浜嗕箣鍚庢病鍔炴硶鍦╯etter浜嗭紝浣嗘槸鍙互涓嶈缃帴鍙e鐞?/p>
className搴旇鎹㈡垚鍙玝eanName
鐜板湪鎴戜滑閫氳繃閰嶇疆鏂囦欢鐨勬柟寮忔潵鑾峰彇瀵硅薄锛屾暣涓繃绋嬪畬鍏ㄧ湅涓嶅埌涓€涓猲ew瀛椼€?/p>
鎬荤粨锛氬啓鐨勫ソ鐑傦紝杩囩▼鏀逛簡濂藉嚑鍥?/p>
鐬庢壇鐨勪唬鐮侊細
Bean宸ュ巶
package ioc; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * @author DaiZhiZhou * @file OA-Project * @create 2020-07-24 22:57 */ public class BeanFactory { private static Map<String,Object> beanContainer = new HashMap<>(); static { try { // InputStream inputStream = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); 銆€銆€銆€銆€銆€銆€銆€// 鍥犱负鎵句笉鍒伴厤缃枃浠跺氨妫€鏌ヤ笅璺緞鐨勮鍙?span style="color: #000000;"> String file = BeanFactory.class.getClassLoader().getResource("bean.properties").getFile(); System.out.println(file); InputStream inputStream = new FileInputStream(new File(file)); Properties properties = new Properties(); properties.load(inputStream); Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = properties.getProperty(key); Class<?> classByValue = Class.forName(value); Object valueInstance = classByValue.newInstance(); beanContainer.put(key, valueInstance); } } catch (Exception e) { e.printStackTrace(); } } public static <Bean>Bean getBean(String className, Class<Bean> beanClass) { Object o = beanContainer.get(className); return (Bean)o; } }
properties:
AaaDao = ioc.dao.AaaDaoImpl2
AaaService = ioc.service.AaaServiceImpl2
娴嬭瘯绫伙細鍋囪鏄鍥剧殑Servlet
package ioc.servlet; import ioc.BeanFactory; import ioc.dao.AaaDao; import ioc.dao.AaaDaoImpl; import ioc.service.AaaService; import ioc.service.AaaServiceImpl; import ioc.service.AaaServiceImpl2; import org.junit.Test; /** * @author DaiZhiZhou * @file OA-Project * @create 2020-07-24 22:48 */ public class AaaServletFake { private AaaService aaaService; public void setAaaService(AaaService aaaService) { this.aaaService = aaaService; } @Test public void toAaaListPage() { setAaaService(new AaaServiceImpl(new AaaDaoImpl())); aaaService.getAaaList(); } @Test public void toAaaListPageByFactoryProduct() { //this.setAaaService(BeanFactory.getAaaService()); aaaService.getAaaList(); } @Test public void toAaaListPageByFactoryProductPropertyFileInject() { AaaDao aaaDao = BeanFactory.getBean("AaaDao", AaaDao.class); AaaServiceImpl2 aaaServiceImpl2 = BeanFactory.getBean("AaaService", AaaServiceImpl2.class); aaaServiceImpl2.setAaaDao(aaaDao); this.setAaaService(aaaServiceImpl2); this.aaaService.getAaaList(); } }
鍏充簬娉ㄥ叆闂鐨勫啀琛ュ厖锛?/h2>
鍥犱负瀛樺湪杩欑宸ュ巶鏂瑰紡鐨勬敞鍏ワ紝鎵€浠pring灏变篃鎻愪緵浜嗗搴旂殑杩欑鏂瑰紡锛?/p>
闈欐€佸伐鍘傝幏鍙?nbsp;鍜?nbsp;宸ュ巶瀹炰緥鏂规硶鑾峰彇锛?/h3>
璇︾粏瑙侊細
http://123.57.58.29/blog/1591625710261
闈欐€佸伐鍘?/p>
<!-- 姝ょ鏂瑰紡鏄? 浣跨敤 StaticFactory 绫讳腑鐨勯潤鎬佹柟娉?createAccountService 鍒涘缓瀵硅薄锛屽苟瀛樺叆 spring 瀹瑰櫒 id 灞炴€э細鎸囧畾 bean 鐨?id锛岀敤浜庝粠瀹瑰櫒涓幏鍙? class 灞炴€э細鎸囧畾闈欐€佸伐鍘傜殑鍏ㄩ檺瀹氱被鍚? factory-method 灞炴€э細鎸囧畾鐢熶骇瀵硅薄鐨勯潤鎬佹柟娉? --> <bean id="accountService" class="com.tc51.factory.StaticFactory" factory-method="createAccountService"></bean>
瀹炰緥宸ュ巶
銆€銆€<!--鍒涘缓宸ュ巶瀵硅薄--> <!--鍏堥€氳繃瀹炰緥宸ュ巶鍒涘缓瀵硅薄--> <bean id="bean3Factory" class="com.tc51.factory.Bean3Factory"></bean> <bean id="accountService" factory-bean="bean3Factory" factory-method="createAccountService"></bean>
以上是关于銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1的主要内容,如果未能解决你的问题,请参考以下文章
闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢
/etc/profile銆?etc/bashrc銆亊/.bash_profile銆亊/.bashrc 鐨勫尯鍒紙杞級
绗旇瘯-4399銆?020鏍℃嫑銆慦eb鍚庣寮€鍙戝伐绋嬪笀绗旇瘯棰橈紙鎴戝張琛屼簡锛熺劧鍚庝竴浠介潰璇曢€氱煡閮芥病鏈夛紝鎴戞槸鐪熺殑鑿滃晩銆傘€傘€傘€傦級