在web应用中通过以下方式装载spring bean context:
如果已在其它地方装载,这一步可以省去
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/conf/appContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在tapestry4下引入tapestry-spring.jar,即可在页面中通过spring:访问spring的bean。
当时在servlet中如何访问这个spring context获得bean呢?使用global吗?
在网上找了资料也没有找到。只是自己试验出了一种方式,通过ServletContext访问,暂时先用着:
ServletContext sc = req.getSession().getServletContext();
WebApplicationContext wc = WebApplicationContextUtils .getWebApplicationContext(sc);
调用wc.getBean(beanID)就可以获得对应的bean了
本文介绍了一种在Web应用中通过ServletContext获取Spring Bean的方法。利用ContextLoaderListener初始化Spring上下文,并通过WebApplicationContextUtils工具类获取Bean实例。
263

被折叠的 条评论
为什么被折叠?



