r-arg> <list> <value>applicationContext.xml</value></list> </constructor-arg> </bean> </beans> 然后让loactor去找它,但代码有点长:
BeanFactoryReference bfr = DefaultLocatorFactory.getInstance().useBeanFactory("default-context"); BeanFactory factory = bfr.getFactory(); MyService myService = factory.getBean("myService"); bfr.release(); // now use myService 上面的代码实在是太灵活,太麻烦了。
还不如自己实现一个简单的Singleton,扩展ContextLoaderListener类,在Web系统启动时压入Singleton。
新的ContextLoaderListener类重载如下,ContextUtil中包含一个静态的ApplicationContext变量:
public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event);
ServletContext context = event.getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context); ContextUtil.setContext(ctx); } 用家可直接取用:
ApplicationContext context = ContextUtil.getContext(); 2.IoC fashion
如果所有地方都使用直接召唤系,那就反而是在打Rod的耳光了。因为他一直都反对代码与框架深耦合的。
所以,更好的方法是写一些glue code、base class来完成Spring的降临,而不让应用代码察觉Spring Application Context的存在。
不过,因为各个框架的结构不同,Rod也没办法讲出一个通用的整合方法,所以建议大家尽量学习已整合的各种框架,如Spring MVC、Struts的种种方式,写出自己的简单整合代码来。
只有不确定的调用某些S上一页 [1] [2] [3] 下一页
|