在厨房,车顶,草地上使用Spring
来源:互联网 发布:网络当红女主播 编辑:程序博客网 时间:2024/05/16 01:46
原文出处及讨论:http://www.blogjava.net/calvin/archive/2005/12/30/26015.html
作者:江南白衣
Spring再强大,也要面对降临的问题--因为Spring不是Weblogic、Tomcat般的顶层容器,Servlet和EJB对象不由它创建,所以它必须要降临到Weblogic、Tomcat所在的位面。
初学者一般不用管那么多,照着Spring+hibernate+Struts之类的Sample就做了,但慢慢的,也许就要开始在jsp+javabean体系,土制框架,singleton类等环境下使用Spring了。
《Professional Java Development with the Spring Framework》第3章有"Managing the Containe"一节讲这个问题。一般可以分为直接召唤系与IoC fashion两类。
1.直接召唤系--Singleton的Application Context
最简单的,就像在UnitTest里那样,直接构造Application Context:
ApplicationContext ctx = new ClasspathXmlApplicationContext("ApplicationContext.xml");
在Web环境里,会使用ContextLoader构造ApplicationContext后,压进Servlet Context。
由ContextLoaderListener或ContextLoaderServlet,在Web应用启动时完成。
然后在Jsp/Servelet中,可以通过Servlet Context取得ApplicationContext:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);
但像singleton类或者EJB中,就没有Servlet Context可用了。
如果全部像UnitTest那样直接构造,速度就会很不堪。自然的,就想到把ApplicationContext做成单例。
Spring提供了ContextSingletonBeanFactoryLocator这样的物体。
先搞一个beanRefFactory.xml,里面写上所有的applcationContext-*.xml文件名,并把Context命名为"default-context":
<beans>
<bean id="default-context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list> <value>applicationContext.xml</value></list>
</constructor-arg>
</bean>
</beans>
<bean id="default-context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-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
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);
}
{
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的种种方式,写出自己的简单整合代码来。
只有不确定的调用某些Singleton类,不适合过早ioc的情况,可以使用直接召唤系。
- 在厨房,车顶,草地上使用Spring
- 在厨房,车顶,草地上使用Spring
- 在厨房,车顶,草地上使用Spring
- 青蛙和蚊子一只小青蛙在一片草地上休息
- 在websphere上使用spring-ws
- 在厨房中开“处方”
- FOJ 2150 在二维草地上点火烧完所有草最少时间 BFS+图论+容斥
- 厨房
- 上得机房,下得厨房
- 周五是上天堂,还是下厨房
- Java -- 在Eclipse上使用Spring
- Java -- 在Eclipse上使用Spring
- 在eclipse上使用cvs导入spring源码
- 使用IDEA在Spring Boot上集成JSP
- Spring Cloud 的 Hystrix 在 Feign上使用 Hystrix功能
- Spring学习(一)在IntelliJ IDEA上使用Maven创建Spring项目
- 11个漂亮的使用草地的网站设计
- Android厨房的使用环境搭建以及使用
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- POJO应用框架:Spring与EJB3.0的比较
- tomcat服务器配置ms sql数据库的数据源
- J2EE应用中与Oracle数据库的连接
- Spring对log4j的增强
- 在厨房,车顶,草地上使用Spring
- Linux下的Java配置
- 【转】OFFICE咖啡文化!
- 我不想说我是鸡[超级搞笑FLASH]
- 计算Java日期--学习怎样创建和使用日期
- 在view关闭时执行某些操作
- 用Java转化汉字为拼音全拼
- j2se 事件
- 我们为什么要结婚?