分享
 
 
 

使用Spring减少代码量

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

http://www.skyinn.org/wiki/Wiki.jsp?page=Java_blogentry_270704_1

在使用Spring之前,DAO的配置、初始化、获取、释放等都需要自己写,

将近11个类或接口,量比较大,关系复杂,尚未包括配置文件处理、ThreadLocal、Filter等等

原来的DAOFactory,这还将DAO的配置信息由别的模块处理了的

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_walker

004??*?=============================================

005??*?The?Skyinn?Software?License?v1.0

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工厂类。

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??????*?取DAO工厂,Singleton模式以确保系统中只有一个DAOFactory实例。

054??????*

055??????*?@return?DAO工厂实例

056??????*/

057?????public?static?synchronized?DAOFactory?getInstance?()?{

058?????????if?(null?==?instance)?{

059?????????????instance?=?new?DAOFactory();

060?????????}

061?????????return?instance;

062?????}

063?

064?????/**

065??????*?从DAO池中取对应KEY的DAO实例。

066??????*?若该实例未存在,则从DAO配置中取该DAO的Mapping,并从中取该DAO的实现类的类名,

067??????*?初始化之并将其置入池中缓存。

068??????*

069??????*?@param?key

070??????*?@return

071??????*?@throws?DAOException

072??????*/

073?????public?synchronized?DAO?findDAOByKey?(final?String?key)

074?????????throws?DAOException?{

075?????????//TODO:DAO实例是在初始化时全部创建一边还是在每次被调用时才创建???

076?????????//get?dao?instance?from?dao?pool

077?????????DAO?daoInstance?=?(DAO)?daos.get?(key);

078?

079?????????//get?dao?mapping

080?????????final?DAOMapping?daoMapping?=?daoConfig.findDAOMapping?(key);

081?

082?????????//if?null?or?different?type?bewteen?the?current?dao?and?it's?mapping

083?????????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?instance

095?????????????????Class?clazz?=?Class.forName?(daoImplClass);

096?????????????????daoInstance?=?(DAO)?clazz.newInstance?();

097?

098?????????????????//set?current?type

099?????????????????daoInstance.setCurrentType?(daoMapping.getCurrentType?());

100?

101?????????????????//add?to?dao?pool

102?????????????????daos.setFast?(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?(IllegalAccessException?e)?{

116?????????????????log.error?("IllegalAccessException:"?+?e.getMessage());

117?????????????????throw?new?DAOException(e);

118?????????????}

119?????????}

120?????????return?daoInstance;

121?????}

122?}

相应的配置文件: 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???

而现在在使用了spring之后,只定义了三个类或接口:

DAO DAO接口 DAOException 异常 HibernateDAO Hibernate dao 基类,

而且相应的代码不到100行: 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_walker

05??*?=====================================================================*/

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

15??*?

16??*?

17??*?View?Source

18??*?

19??*?

20??*?@author?$Author:?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(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?????PersistentObject?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.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管理、事务等等都交由spring去处理,确实省事不少。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有