精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-03-18
Bruce, 很高兴能和你讨论,我们就不用客气了吧,呵呵
引用 I don't think 各层对注射造成依赖
感觉这样说起来很怪,呵呵,可是要命的是我也能看懂:-) 引用 I don't understand. what you said like 第二种做法会导致各层对注射造成依赖,而且注射的代码分散,不好管理。
我的意思是,第二种做法会在该层有WebApplicationContextUtils.getWebApplicationContext之类的代码,这些代码是对Spring等具体技术有依赖的代码。如果每一层取得实例的时候都作相关的工作,一方面是功能类似,另一方面是依赖于同一种技术的代码应尽量放在一处,基于这两个考虑,和Spring相关的代码,应该尽量放在一处。并且其他层要能访问并取得实例的话,该处的访问应该是全局的。 不管使用那种IoC的容器,都会有一个Start Point。我的想法是应该尽量让这个Start Point只有一个,如果有多个的话,也应尽量在一处,否则一旦有改变,就无法确定要修改那几处了。我所说的容易管理,也是这个意思。 至于你说的Service Locator和Factory,和这里的意思有所不同(我强调的是一旦发生修改,只需要改一处。)。但如果这些代码能够集中,当然是更好了。 不知我的解释是否完全? |
|
返回顶楼 | |
发表时间:2005-03-18
We could have a superclass for every detailed action class. Let's name this superclass as SuperAction. We also could introduce strategy pattern to resolve your worry. Now we have Spring Ioc container. Let's say there will be Summer Ioc container in the future. Look at following codes.
public interface IocContainer { public Object getMyBean(String beanName);; } public class springIocContainer implemented IocContainer{ private static ApplicationContext ctx = null; public Object getMyBean(String beanName);{ if (ctx == null); { ctx = webApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext(););; } return ctx.getBean(beanName);; } } public class superAction{ IocContainer myIocContainer; public void setIocContainer(IocContainer myIocContainer);{ this.myIocContainer= myIocContainer; } public Object getMyBean(String beanName);{ return myIocContainer.getMyBean(beanName);; } } Probably, we need Factory pattern or serviceLocator pattern to point like IocContainer springIocContiainer = new springIocContainer (); in somewhere. Hehe, maybe we need another Ioc container to inject above one line code. ![]() |
|
返回顶楼 | |
发表时间:2005-03-18
bruce 写道 We could have a superclass for every detailed action class. Let's name this superclass as SuperAction. We also could introduce strategy pattern to resolve your worry. Now we have Spring Ioc container. Let's say there will be Summer Ioc container in the future. Look at following codes.
public interface IocContainer { public Object getMyBean(String beanName);; } public class springIocContainer implemented IocContainer{ private static ApplicationContext ctx = null; public Object getMyBean(String beanName);{ if (ctx == null); { ctx = webApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext(););; } return ctx.getBean(beanName);; } } public class superAction{ IocContainer myIocContainer; public void setIocContainer(IocContainer myIocContainer);{ this.myIocContainer= myIocContainer; } public Object getMyBean(String beanName);{ return myIocContainer.getMyBean(beanName);; } } Probably, we need Factory pattern or serviceLocator pattern to point like IocContainer springIocContiainer = new springIocContainer (); in somewhere. Hehe, maybe we need another Ioc container to inject above one line code. ![]() 使用Super Action的方法是可行的。 引用 IocContainer springIocContiainer = new springIocContainer ();
依赖还是出现了 打个比方说,我有一个JAAS login module, 要调用Security的Authentication的实现类,从那里获得实例?无外乎两种办法,使用一个类似于Application的类作全局变量(如果像你的例子里使用一个接口,那依赖没完没了了),或自己来new 一个实例(还是产生依赖了)。当然你用的是自己定义的接口,觉得不会有问题,其他的人用就会不舒服了。 还是觉得使用全局变量是唯一的方法。 这一点想做好,还是不容易。你觉得呢? |
|
返回顶楼 | |
发表时间:2005-03-18
No free lunch, we always have to do balance. Good luck. Over
|
|
返回顶楼 | |