http://www.skyinn.org/wiki/wiki.jsp?page=java_blogenTry_270704_1
Before using Spring, the DAO configuration, initialization, acquisition, release, etc. need to write itself, nearly 11 categories or interfaces, the amount is relatively large, the relationship is complex, and the configuration file processing, Threadlocal, Filter, etc.
The original DAOFActory, which also handles the DAO configuration information by other modules.
001? / *? ======================================================================================================================================================================================= 002 ?? *? $ ID:? DAOFACTORY.JAVA, V? 1.4? 2003/10/20? 14: 18: 44? L_walker? EXP? $ 003 ??? Created? ON? [2003-10-8? 22 : 48: 12]? BY? L_walker004 ?? *? ===================================== ======== 005 ?? *? The? Skyinn? Software? License? V1.0006 ?? *? ====================== ==================================007 ?? * / 008? Package? Org.skyinn.quasar.dao; 009? 010? Import? Org. apache.commons.collections.FastHashMap;? 011 import org.apache.commons.logging.Log;? 012 import org.apache.commons.logging.LogFactory;?? 013 import org.skyinn.quasar.config.ApplicationConfiguration??; 014? Import? Org.skyinn.quasar.util.StringUTIL; 015? 016? 017? / ** 018 ?? *? DAO factory class.
019 ?? * 020 ?? *? @ Author? $ Author:? L_walker? $ 021 ?? *? @ Version? $ Revision:? 1.4? $? $ DATE:? 2003/10/20? 14: 18: 44? $ 022 ?? * / 023? Public? Class? DAOFACTORY? {024 ????? // ~? Static? Fields / initializers? ==================== ==================================== 026 ????? ** daofactory? Singleton? Instance. * / 027 ????? Private? static? DAOFACTORY? Instance? =? new? daofactory (); 028? 029 ?????? // ~? instance? fields? ================== ====================================== 030? 031 ????? / **? Logging? * / 032 ????? private? Log? Log? =? Logfactory.getlog? (This.getclass? ()); 033? 034 ????? / ** Dao? Pool. * / 035? ???? protected? fasthashmap? daos? =? new? fasthashmap (); 036? 037 ????? / ** Dao? configuration. * / 038 ????? protected? DAOCONFIG? DAOCONFIG? =? null ; 039? 040 ?????? // ~? Constructors? =================================== ================================== 041? 042 ????? / ** 043 ?????? *? Default? Construtor.044? ????? * / 045 ????? private? DAOFACTORY? ()? {046 ????????? super (); 047 ????????? daoconfigure =? ApplicationConfiguration .GetInstance (). getDaoconfiguration (); 048 ?????} 049? 050 ?????? // ~? methods? ==================== =
===================================================== 051? 052??? ?? / ** 053 ?????? *? Take the DAO factory, Singleton mode to ensure that there is only one DAOFActory instance in the system. 054 ?????? * 055 ?????? *? @ RETURN? DAO factory instance 056 ?????? * / 057 ????? public? Static? Synchronized? DAOFACTORY? GetInstance? () ? {058 ????????? if? (Null? ==? Instance)? {059 ????????????? instance? =? New? DAOFACTORY (); 060? ?????????} 061 ????????? Return? Instance; 062 ?????} 063? 064 ????? ** 065 ?????? *? Take the DAO instance of the Key from the DAO pool. 066 ?????? *? If the instance does not exist, the DAO map is taken from the DAO configuration, and the class name of the implementation class of the DAO is taken, 067 ?????? * initializes Place it into the pool.
068 ?????? * 069 ?????? *? @ Param? Key070 ?????? *? @ RETURN071 ?????? *? @ THROWS? DAOException072 ?????? * / 073 ????? public? Synchronized? Dao? FindDaobyKey? (Final? String? Key) 074 ???????????????????????????????? // TODO : DAO instance is created while being created during initialization or is created each time it is called? ? ? 076 ?????????? // get? Dao? Instance? From? DAO? Pool077 ????????? dao? DAOINSTANCE? =? (DAO)? DAOS.GET? (Key); 078? 079 ????????? // get? Dao? Mapping080 ????????? final? Daomapp? Daomapping? =? Daoconfig.findaomapp? (Key); 081? 082 ??? ?????? // if? null? or? Different? type? beWteen? the? current? dao? and? it's? mapping083 ????????? f ((null? ==? DAOINSTANCE )? || 084 ?????????????????! DAOINSTANCE.VALIDATTYPE? (Daomapping.getcurrenttype? ()))? {085 ??????????????????????????? ?? Try? {086 ????????????????? final? String? daoImplclass? = 087 ??????????????????????????????????????????? ?? daomapping.finddaoImplclass? (daomapping.getcurrenttype? ()); 088? 089 ????????????????? f (DAOIMPLCLASS))? {090? ???????????????????????????????????????????????????????????????????????????????????????????????????????????. ": ["? 091 ?????? ???????????????????? daomapp? ? "]"); 092 ?????????????????} 093? 094 ???????????????????????????????????????????????????? Class? Clazz? =? Class.Forname? DaoImplclass; 096 ????????????????? DAOINSTANCE? =? (DAO)? CLAZZ.NEWINSTANCE? (); 097? 098 ???????????????????????? ?????? // set? current? type099 ???????????????? DAOINSTANCE.SETCURRENTTYPE? (daomapping.getcurrenttype? ()); 100? 101 ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????? daos.setFast (false);? 103 ????????????????? daos.put (key, daoInstance?);? 104 ?? ??????????????? daos.setfast? (TRUE); 105? 106 ????????????????? f (log.isdebugeload? ())? {107 ????????????? log.debug? ("A? Dao? Instance? Created: ["? ? KEY? ? "]");
108 ??????????????????} 109 ??????????????}? Catch? (ClassNotFoundException? E)? {110 ????? ???????????? log.error? ("ClassNotFoundExce:"? ? E.getMessage ()); 111 ?????????????????????????????????????????? ? New? daoException (e); 112 ?????????????}? catches? (InstantiationException? E)? {113 ????????????????????????????? ? log.error ( "InstantiationException:" e.getMessage ()??); 114 ????????????????? throw new DAOException (e); 115 ????? ???????????}? cat? (IllegaCcessException? e)? {116 ????????????????? log.error? ("IllegaCcessException:"? ? E.GetMessage ()); 117 ?????????????????????????????????????????????????????????????????????????? } 119 ?????????} 120 ????????? Return? DAOINSTANCE; 121 ?????} 122?} The corresponding profile:
01???
02???
03 ?????
"Aclservice"? ClassName = "org.skyinn.quasar.acl.aclservice" /> 04 ??? 05? 06???
07???
08 ?????
"Org.skyinn.quasar.acl.dao.userdao1"? CurrentType = "hibernate"> 09 ???????
"hibernate"? Implclass = "org.skyinn.quasar.acl.dao.hibernate.Userdaohibimpl" /> 10 ????? 11???
Now after using Spring, only three classes or interfaces are defined:
Dao Dao Interface DaoException Hibernatedao Hibernate DAO Base class,
And the corresponding code is less than 100 lines:
01? / *? ========================================================================================================================================================================== ====================================================================================================================================================================================================================================================== : 22? L_walker? EXP? $ 03 ?? *? 04 ?? *? Created:? [2004-7-25? 0: 03: 47]? BY? L_walker05 ?? *? ========= ============================================================================================================================================================================================================= ========== * / 06? Package? Org.skyinn.quasar.dao; 07? 08? Import? Java.util.list; 09 10? Import? Org.skyinn.quasar.common. PersistentObject; 11? Import? Org.springframework.orm.hibernate.support.hibernatedaosupport; 12? 13? / ** 14 ?? *? Hibernatedao.java15 ?? *? 16 ?? *? 17 ?? *?
View? Source18 ?? *? 19 ?? *? 20 ?? *? @ Author? $ Author:? L_walker? $ 21 ?? * @ version? $ Version $? $ DATE:? 2004/07/26? 14: 38: 22? $ 22 ?? * / 23? Public? Class? Hibernatedaosupports? Hibernatedaosupport? Imports? Dao? {24 ??? private? Static? Final? String? Key? =? "Hibernatedao"; 25? 26? ?? public? PersistentObject? creteObject (personistentObject? Obj)? {27 ????? Return? (persistentObject)? this.GethibernateTemplate (). save (obj); 28 ???} 29? 30 ??? public? Void? UpdateObject (PersistentObject? Obj)? {31 ????? this.GethibernateTemplate (). update (obj); 32 ???} 33? 34 ??? public? void? deleteObject (PersistentObject? Obj)? { 35 ????? this.GethibernateTemplate (). Delete (obj); 36 ???} 37 ??? public? Void? DELETEOBJECTBYID (Class? Clazz,? Long? Id)? {38 ????? personistentObjectObject ? OBJ? = 39 ??????? (persistentObject)? this.GethibernateTemplate (). Load (40 ????????? Clazz, 41 ????????? new? long ( ID))); 42 ????? if? (null?! =? Obj)? {43 ??????? this.GethibernateTemplate (). delete (obj); 44 ?????} 45? ??} 46? 47 ??? public? PersistentObject? RetriveObjectByid (Class? CLAZZ,? Long? Id)? {48 ????? Return? (PersistentObject)? This.Gethi BernateTemplate (). LOAD (49 ??????? Clazz, 50 ??????? new? long (id)); 51 ???} 52 ????? 53 ????? public List? Listall (class? Clazz) {54 ????????? Return? This.GethibernateTemplate (). Loadall (Clazz); 55 ?????} 56 ????? 57 ??? ?? 58?} SESSION management, transaction, etc. are handled by Spring, and there is a lot of trouble.