spring=画蛇添足

本文提出了一种简单的对象构建和获取方式——ContextHolder模式,该模式通过静态方法简化了对象的获取过程,避免了使用XML配置和依赖IoC框架带来的复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

决定把在javaeye上最后一点积分用光,大家投隐藏吧,投新手吧,不过我还是要说,盲从--中国程序员的悲哀。

尝试是好事,盲从就不好了。

现在我开始尝试使用一种简单的方式来构建对象,获取对象。可称之为ContextHolder模式。

 

java 代码
  1. /**  
  2.  * 2007-6-5 下午02:27:03  
  3.  */  
  4.   
  5. /**  
  6.  * @author <a href="mailto:guileen@gmail.cn">桂健雄</a>  
  7.  * @since 2007-6-5  
  8.  */  
  9. public abstract class ContextHolder {   
  10.     static{   
  11.         //init the context ,sessionFactory   
  12.         AnnotationConfiguration cfg = new AnnotationConfiguration();   
  13.         Properties prop = new Properties();   
  14.         InputStream is = null;   
  15.         try{   
  16.             is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");   
  17.             prop.load(is);   
  18.         } catch (IOException e) {   
  19.             Logger.getLogger(Properties.class).error("jdbc.properties 读取失败");   
  20.         }finally{   
  21.             if(is != null){   
  22.                 try {   
  23.                     is.close();   
  24.                 } catch (IOException e) {   
  25.                 }   
  26.             }   
  27.         }   
  28.         cfg.addProperties(prop);   
  29.         initAnnotatedClasses(cfg);   
  30.         buildDaos();   
  31.         sessionFactory = cfg.buildSessionFactory();   
  32.         SessionHolder.setSessionFactory(getSessionFactory());   
  33.     }   
  34.     private static SessionFactory sessionFactory;   
  35.     private static MyDao1 dao1;   
  36.     private static MyDao2 dao2;   
  37.     /**  
  38.      * @param cfg  
  39.      */  
  40.     private static void initAnnotatedClasses(AnnotationConfiguration cfg) {   
  41.         cfg.addAnnotatedClass(MyEntity1.class);   
  42.         cfg.addAnnotatedClass(MyEntity2.class);   
  43.         cfg.addAnnotatedClass(MyEntity3.class);   
  44.     }   
  45.        
  46.     private static void buildDaos(){   
  47.         dao1 = new MyDao1Impl();   
  48.         dao2 = new MyDao2Impl();   
  49.     }   
  50.        
  51.     public static SessionFactory getSessionFactory(){   
  52.         return sessionFactory;   
  53.     }   
  54.        
  55.     public static MyDao1 getMyDao1(){   
  56.         return dao1;   
  57.     }   
  58. }   
<script>render_code();</script>

 

IOC的目标是,建立统一的对象装配器,对象提取器。但是,使用xml的配置实在是没有必要,取对象出来还要用getBean方法,要填对象名,要作类型转换,对了,还要先设法获取ApplicationContext对象。真是无聊之极。

当然,想要使用Spring中的Bean,也不一定非要获取ApplicationContext,可以把调用者也配置到Context中,定义好set方法,在配置文件中,把调用者的property ref 到 想用到bean上。

不过,如此一来,就更加是画蛇添足了。

aop和事务管理,何必一定完全交给IoC框架呢,自己编码啊。使用框架,以为自己发现了捷径,走过去,就发现,这条小路走的不那么爽,布满荆棘。

而使用这种ContextHolder模式,一切是多么的简单明了,直接static的方法调用,ContextHolder.getMyDao1(),就ok了,而且少了一个2M的spring.jar,看着舒服,少了xml,看着舒服,少了一些无聊的getters and setters,舒服。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值