现在的位置: 首页 > 综合 > 正文

Spirng Use In Other Times (补充)

2013年09月03日 ⁄ 综合 ⁄ 共 3055字 ⁄ 字号 评论关闭

大家都知道web应用在启动时,Spring的listener在启动时会找到Spring的配置文件,然后spring 的bean factory

生产bean,并压入servletConext作用域内,所以在只要能取得servletConext的环境中就很容易取到这些bean,

Spring也提供了WebApplicationContextUtils 工具类。

以下引自:http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/
#############################################################
当 Web 应用集成 Spring 容器后,代表 Spring 容器的 WebApplicationContext 对象将以

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键存放在 ServletContext 属性列表中。您

当然可以直接通过以下语句获取 WebApplicationContext:

WebApplicationContext wac = (WebApplicationContext)servletContext.
getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 

但通过位于 org.springframework.web.context.support 包中的 WebApplicationContextUtils 工具类获取

WebApplicationContext 更方便:
WebApplicationContext wac =WebApplicationContextUtils.
getWebApplicationContext(servletContext);
#############################################################

 

所以在jsp, servlet, filter, listener中都能很方便地取到servletConext,当然也可以很方便地取得实例化好的

bean。但是在javabean里就比较麻烦了。

当然可以参见下文:
以下引自:http://wiki.springside.org.cn/display/springside/Spirng+Use+In+Other+Times
#############################################################
Spirng Use In Other Times
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两类。

#############################################################

在javabean里直接使用 ApplicationContext ctx = new ClasspathXmlApplicationContext

("ApplicationContext.xml");无疑是最简单的。其它的方法稍显麻烦

在我们的一个应用中,使用到了定时任务,当然也是用spring封装的,直接使用javabean作为任务。在定时任务中

我们想操作数据库,而操作数据库的service都配置在spring中,这样形成了一个死循环。如果使用上面的方法,每

次任务执行时,又要把所有的bean重新“生产”一次,效率肯定不高。
现实中,我们是这样解决的
1、分配置文件
  spring-dao.xml
 daos services
  spring-conext.xml(把spring-dao.xml import进来)
 dataSource,事务,等
  spring-task.xml
 执行任务的javabean,jobDetails triggers, SchedulerFactoryBean

  1.  public interface AutoRunService {
  2.   void doSomething();
  3.  }

2、web.xml的configLocation中写上上面两个文件

 

  1.  <context-param>
  2.   <param-name>contextConfigLocation</param-name>
  3.   <param-value>classpath:spring-context.xml,classpath:spring-task.xml</param-value>
  4.  </context-param>
  5.  <listener>
  6.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7.  </listener>

 

3、如果采用直接招唤的方法
  直接在javabean中

  1. ApplicationContext ctx = new ClasspathXmlApplicationContext("spring-conext.xml");

  下在就可以操作数据库了。

4、不用第三种方法,我们注意到执行任务的javabean也是在容器中“生产”了,如果知道它所在的容器就好了。
正好发现有个ApplicationContextAware接口,实现此接口的类必须要实现 setApplicationContext(ApplicationContext applicationContext)方法,传入applicationContext,这正是我想要的,当然这个javabean必须也是在这个容器中生产的。于是有:

 

  1. public class AutoRunServiceDefaultImpl implements AutoRunService, ApplicationContextAware {
  2.  XxxService xxxService;
  3.  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  4.   xxxService = (XxxService) applicationContext.getBean("xxxService");
  5.  }
  6.  public void doSomething() {
  7.   //xxxService.xxx();
  8.  }

虽然,引入了Spring的API,破坏了此JavaBean的“纯洁”,但是还是值得的,比直接召唤的性能上要好很多。

 

 

 

 

抱歉!评论已关闭.