




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、基于Struts+Spring+JDBC架構(gòu)的課程設(shè)計(jì)實(shí)訓(xùn)教學(xué)例如工程?網(wǎng)上銀行賬戶管理系統(tǒng)?利用反射技術(shù)創(chuàng)立通用的工廠類1.1.1 利用反射技術(shù)創(chuàng)立通用的工廠類1、在工程中添加一個(gè)GenerialFactory類1包名稱為,并且繼承類2編程該類package com.px1987.webbank.factory;import java.util.Observable;1987.webbank.config.ClassNameConfig;import com.px1987.webbank.exception.WebBankException;public class GenerialFact
2、ory extends Observable public GenerialFactory() super(); public Object newSomeOneInstance(String someOneClassNameInXML) throws WebBankException Object someOneInstance = null; Class oneClass = null; try oneClass = Class.forName(someOneClassNameInXML); catch (ClassNotFoundException e) int logImpleKind
3、=Integer.parseInt(ClassNameConfig.getProperty("logImpleKind");this.addObserver(LogInfoFactory.newLogInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObservers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankException("不能正確地獲得" + someOneClassNameInXML + "類"); try someOneI
4、nstance = oneClass.newInstance(); catch (InstantiationException e) int logImpleKind=Integer.parseInt(ClassNameConfig.getProperty("logImpleKind");this.addObserver(LogInfoFactory.newLogInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObservers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankE
5、xception("不能正確地創(chuàng)立" + someOneClassNameInXML + "類的對(duì)象實(shí)例"); catch (IllegalAccessException e) int logImpleKind=Integer.parseInt(ClassNameConfig.getProperty("logImpleKind");this.addObserver(LogInfoFactory.newLogInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObserv
6、ers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankException("不能正確地創(chuàng)立" + someOneClassNameInXML +"類的對(duì)象實(shí)例"); return someOneInstance; 2、修改TestConnectDBBean類中的有關(guān)代碼package com.px1987.webbank.daotest;import java.sql.Connection;import org.junit.After;import org.junit.Assert;import org.junit.Befor
7、e;import org.junit.Test;import com.px1987.webbank.config.ClassNameConfig;import er.ConnectDBInterface;import com.px1987.webbank.exception.WebBankException;import com.px1987.webbank.factory.GenerialFactory;public class TestConnectDBBean ConnectDBInterface connectDBBean=null;
8、String connectDBBeanClassName=null;Beforepublic void setUp() throws Exception connectDBBeanClassName=ClassNameConfig.getProperty("connectDBBean.className");/connectDBBean=new ConnectDBFactory().newConnectDBBean(connectDBBeanClassName);connectDBBean = (ConnectDBInterface)new GenerialFactory
9、().newSomeOneInstance(connectDBBeanClassName);Afterpublic void tearDown() throws Exception connectDBBean=null;Testpublic void testCloseDBCon()throws WebBankException connectDBBean.closeDBCon();Connection returnDBCon=connectDBBean.getConnection();Assert.assertNull(returnDBCon); /斷言Testpublic void tes
10、tGetConnection() throws WebBankExceptionConnection returnDBCon=connectDBBean.getConnection();Assert.assertNotNull(returnDBCon); /斷言3、執(zhí)行上面的測試用例1.1.2 在UserManageDAOJDBCImple類中應(yīng)用GenerialFactory類1、修改UserManageDAOJDBCImple類中的getConnection()方法public Connection getConnection() throws WebBankException/conne
11、ctDBBean=new ConnectDBFactory().newConnectDBBean(connectDBBeanClassName);connectDBBean = (ConnectDBInterface)new GenerialFactory().newSomeOneInstance(connectDBBeanClassName);con=connectDBBean.getConnection();return con;2、修改TestUserManageDAOJDBCImple類Beforepublic void setUp() throws Exception userMan
12、ageDAOJDBCImpleClassName=ClassNameConfig.getProperty("userManageDAOJDBCImple.className");/oneUserManageDAOJDBCImple=new UserManageDAOFactory().newUserManageDAOBean(userManageDAOJDBCImpleClassName);oneUserManageDAOJDBCImple = (UserManageDAOInterface)new GenerialFactory().newSomeOneInstance(
13、userManageDAOJDBCImpleClassName);3、執(zhí)行該測試用例1.1.3 在AdminUserManageDAOJDBCImple類中中應(yīng)用GenerialFactory類1、修改AdminUserManageDAOJDBCImple類public Connection getConnection() throws WebBankException /connectDBBean=new ConnectDBFactory().newConnectDBBean(connectDBBeanClassName);connectDBBean = (ConnectDBInterfac
14、e)new GenerialFactory().newSomeOneInstance(connectDBBeanClassName);con=connectDBBean.getConnection();return con;2、修改TestAdminUserManageDAOJDBCImple類Beforepublic void setUp() throws Exception adminManageDAOJDBCImpleClassName=ClassNameConfig.getProperty("adminManageDAOJDBCImple.className");/
15、adminUserManageDAOJDBCImple=new AdminUserManageDAOFactory().newAdminManageDAOBean(adminManageDAOJDBCImpleClassName);adminUserManageDAOJDBCImple = (AdminUserManageDAOInterface)new GenerialFactory().newSomeOneInstance(adminManageDAOJDBCImpleClassName);3、執(zhí)行TestAdminUserManageDAOJDBCImple用例類1.1.4 在Accou
16、ntManageDAOJDBCImple類中中應(yīng)用GenerialFactory類1、修改AccountManageDAOJDBCImple類public Connection getConnection() throws WebBankException /connectDBBean=new ConnectDBFactory().newConnectDBBean(connectDBBeanClassName);connectDBBean = (ConnectDBInterface)new GenerialFactory().newSomeOneInstance(connectDBBeanCl
17、assName);con=connectDBBean.getConnection();return con;2、修改TestAccountManageDAOJDBCImple類Beforepublic void setUp() throws Exception accountManageDAOJDBCImpleClassName=ClassNameConfig.getProperty("accountManageDAOJDBCImple.className");/oneAccountManageDAOJDBCImple=new AccountManageDAOFactory
18、().newUserManageDAOBean(accountManageDAOJDBCImpleClassName);oneAccountManageDAOJDBCImple = (AccountManageDAOInterface)new GenerialFactory().newSomeOneInstance(accountManageDAOJDBCImpleClassName);3、執(zhí)行TestAccountManageDAOJDBCImple用例類1.1.5 應(yīng)用泛型技術(shù)完善前面的通用工廠類實(shí)現(xiàn)1、添加一個(gè)GenericTypeFactory1包名稱為2編程該類package com
19、.px1987.webbank.factory;import java.util.Observable;import com.px1987.webbank.config.ClassNameConfig;987.webbank.exception.WebBankException;public class GenericTypeFactory<GenericType> extends Observable public GenericTypeFactory() public GenericType newSomeOneInstance(String someOneClassNameI
20、nXML) throws WebBankException GenericType someOneInstance = null; Class oneClass = null; try oneClass = Class.forName(someOneClassNameInXML); catch (ClassNotFoundException e) int logImpleKind=Integer.parseInt(ClassNameConfig.getProperty("logImpleKind");this.addObserver(LogInfoFactory.newLo
21、gInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObservers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankException("不能正確地獲得" + someOneClassNameInXML + "類"); try someOneInstance = (GenericType)oneClass.newInstance(); catch (InstantiationException e) int logImpleKind=Integer.pars
22、eInt(ClassNameConfig.getProperty("logImpleKind");this.addObserver(LogInfoFactory.newLogInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObservers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankException("不能正確地創(chuàng)立" + someOneClassNameInXML + "類的對(duì)象實(shí)例"); catch (IllegalAccess
23、Exception e) int logImpleKind=Integer.parseInt(ClassNameConfig.getProperty("logImpleKind");this.addObserver(LogInfoFactory.newLogInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObservers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankException("不能正確地創(chuàng)立" + someOneClassNameInXML +
24、"類的對(duì)象實(shí)例"); return someOneInstance; 2、在各個(gè)測試用例中應(yīng)用它oneUserManageDAOImple=new GenericTypeFactory<UserManageDAOInterface>().newSomeOneInstance(userDAOBeanClassName);注意:在應(yīng)用該泛型工廠類時(shí),“預(yù)先指定所需要?jiǎng)?chuàng)立的目標(biāo)產(chǎn)品類的類型而不是應(yīng)用常規(guī)的工廠類時(shí),對(duì)創(chuàng)立后的目標(biāo)對(duì)象的類型進(jìn)行“事后轉(zhuǎn)換。3、執(zhí)行測試用例的結(jié)果1.1.6 利用泛型方法對(duì)前面的GenericTypeFactory類進(jìn)一步完善1、利用泛型方
25、法設(shè)計(jì)一個(gè)GenericTypeMethodsFactory類package com.px1987.webbank.factory;import java.util.Observable;import com.px1987.webbank.config.ClassNameConfig;import com.px1987.webbank.exception.WebBankException;public class GenericTypeMethodsFactory extends Observable public GenericTypeMethodsFactory() public <
26、GenericType>GenericType newSomeOneInstance(String someOneClassNameInXML) throws WebBankException GenericType someOneInstance = null; Class oneClass = null; try oneClass = Class.forName(someOneClassNameInXML); catch (ClassNotFoundException e) int logImpleKind=Integer.parseInt(ClassNameConfig.getPr
27、operty("logImpleKind");this.addObserver(LogInfoFactory.newLogInstance(logImpleKind); this.setChanged(); /注意要設(shè)置變化點(diǎn) notifyObservers(e); /當(dāng)出現(xiàn)異常時(shí)將通知各個(gè)觀察者 throw new WebBankException("不能正確地獲得" + someOneClassNameInXML + "類"); try someOneInstance = (GenericType)oneClass.newInstance(); catch (InstantiationException e
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 美容合同到期終止協(xié)議書
- 裝修公司拒簽合同協(xié)議書
- 專利知識(shí)產(chǎn)權(quán)合同范本
- 采購藥品保密協(xié)議書范本
- 美團(tuán)買菜分揀員合同范本
- 資產(chǎn)買賣合同協(xié)議書范本
- 認(rèn)購協(xié)議書作廢聲明模板
- 采摘八角雇傭合同協(xié)議書
- 中小學(xué)生感恩課件
- 國開學(xué)習(xí)網(wǎng)《園林生態(tài)學(xué)》形成性考核冊(cè)任務(wù)1234答案
- 2023年四川省宜賓市全科醫(yī)學(xué)專業(yè)實(shí)踐技能測試卷(含答案)
- 電梯井道腳手架施工方案
- 興平市生活垃圾焚燒發(fā)電項(xiàng)目環(huán)評(píng)報(bào)告
- 初中數(shù)學(xué)浙教版九年級(jí)上冊(cè)第4章 相似三角形4.3 相似三角形 全國公開課一等獎(jiǎng)
- 主令電器(課用)課件
- DLT 5066-2010 水電站水力機(jī)械輔助設(shè)備系統(tǒng)設(shè)計(jì)技術(shù)規(guī)定
- 湘少版英語六年級(jí)下冊(cè)全冊(cè)教案
- 測繪生產(chǎn)困難類別細(xì)則及工日定額
- 湖南省長郡中學(xué)“澄池”杯數(shù)學(xué)競賽初賽試題(掃描版含答案)
- 消防系統(tǒng)施工總進(jìn)度計(jì)劃
- 2022年廣東省中山市紀(jì)念中學(xué)三鑫雙語學(xué)校小升初數(shù)學(xué)試卷
評(píng)論
0/150
提交評(píng)論