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>
然后让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的种种方式,写出自己的简单整合代码来。
只有不确定的调用某些singleton类,不适合过早ioc的情况,可以使用直接召唤系。
闽公网安备 35060202000074号