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 * ========== =========================================================================================================================================================================================================006 * ===== ===================================================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.
p> 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 ===================================================== 025 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 daoconfig = 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 corresponding 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 DAO implementation class is initialized, and it is cached in the pool.
068 * 069 * @param key070 * @ return071 * @throws DAOException072 * / 073 public synchronized DAO findDAOByKey (final String key) 074 throws DAOException {075 // TODO: DAO instance is created during initialization of all or each side is called Create when it is? ? ? 076 // get dao instance from dao pool077 DAO daoInstance = (DAO) daos.get (key); 078 079 // get dao mapping080 final DAOMapping daoMapping = daoConfig.findDAOMapping (key); 081 082 // if null or different type bewteen the current dao and it's mapping083 if ((null == daoInstance) || 084 daoInstance.validateType (daoMapping.getCurrentType ())!) {085 try {086 final String daoImplClass = 087 daoMapping.findDAOImplClass (daoMapping.getCurrentType ()); 088 089 if (StringUtil.isNullOrEmpty (daoImplClass)) {090 throw new DAOException ( "Not found DAO implement class of: [" 091 daoMapping "]"); 092} 093 094 // new instance095 class clazz = Class.forName (DAOIMPLCLASS); 096 DAOINSTANCE = (DAO) CLAZZ.NEWINSTANCE (); 097 098 // set current type099 DAOINSTANCE.SETCURRENTTYPE (DAOMApping.getCurrentType ()); 100 101 // Add to Dao Pool102 Daos.set Fast (FALSE); 103 DAOS.PUT (KEY, DAOINSTANCE); 104 daos.setfast (TRUE); 105 106 IF (log. ISDebugenabled ()) {107 log.debug ("a DAO Instance Created: [" Key "]");
108} 109} catch (ClassNotFoundException e) {110 log.error ( "ClassNotFoundException:" e.getMessage ()); 111 throw new DAOException (e); 112} catch (InstantiationException e) {113 log.error ( "InstantiationException : " E.GetMessage ()); 114 throw new daoException (e); 115} catch (IllegaCcessException E) {116 log.error (" IllegaCcessExce: " E.getMessage ()); 117 throw new daoException (e) 118} 119} 120 Return DAOINSTANCE; 121} 122} The corresponding profile:
01 02
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 / * ================================================================================================================================================================================ ====================== 02 * $ ID: Hibernatedao.java, V 1.1 2004/07/26 14:38: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.java P> 15 * 16 *
17 * view source i> 18 * p> 19 * 20 * @Author $ authoror : l_walker $ 21 * @version $ Reversion $ $ Date: 2004/07/26 14:38:22 $ 22 * / 23 public class HibernateDAO extends HibernateDaoSupport implements DAO {24 private static final String KEY = "HibernateDAO"; 25 26 public PersistentObject createObject (PersistentObject obj) {27 return (persistentObject) this.GethibernateTemplate (). Save (obj); 28} 29 30 public void updateObject (PERS IstentObject 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 PersistentObject obj = 39 (PersistentObject) this.getHibernateTemplate ( ). 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.getHibernateTemplate () 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 effort.