论坛首页 Java企业应用论坛

webwork+spring最简单的集成

浏览 11787 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-12-31  
xwork-option项目中,新增了一种更简单的实现WebWork和Spring集成的方案。它只要一个拦截器ActionAutowiringInterceptor就可以搞定!并且是自动为Action组装它所需的Spring容器中的Bean,也就是不用在每个Action中逐个配置所需的Bean。
配置步骤:
1、在Web.xml文件中配置Spring Application Context:
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2、在xwork.xml文件中定义ActionAutowiringInterceptor,并配置到相应的Action中:
<interceptor name="autowire" class="com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor">
    <param name="autowireStrategy">
@org.springframework.beans.factory.config.AutowireCapableBeanFactory@AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE
    </param>
  </interceptor>

可以自己设置Bean组装的策略,上面的定义是根据类型组装。如果不设置,默认情况是根据Bean的名称组装。
详细参考:http://wiki.opensymphony.com/display/WW/WebWork+2+Spring+Integration
ActionAutowiringInterceptor主要代码如下:
protected void before(ActionInvocation invocation); throws Exception {
    if (!initialized); {
      ApplicationContext applicationContext = (ApplicationContext);ActionContext.getContext();.getApplication();.get(
          WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);;

      if (applicationContext == null); {
        log.warn("ApplicationContext could not be found.  Action classes will not be autowired.");;
      } else {
        setApplicationContext(applicationContext);;
        factory = new SpringObjectFactory();;
        factory.setApplicationContext(getApplicationContext(););;
        if (autowireStrategy != null); {
          factory.setAutowireStrategy(autowireStrategy.intValue(););;
        }
      }
      initialized = true;
    }
    
    if (factory == null);
      return;

    Action bean = invocation.getAction();;
    factory.autoWireBean(bean);;
    
    ActionContext.getContext();.put(APPLICATION_CONTEXT, context);;
  }
   发表时间:2004-12-31  
我很笨,想起以前的笨拙的external
埃!
这个是最好的,都怪当初只认为外国人wiki上提供的方法是最好的 ,自己懒得动脑子,真是只会抄袭的家伙哦!
0 请登录后投票
   发表时间:2005-01-07  
小声的说:
谁能共享一个com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor这个么?我在公司不能上java.net
0 请登录后投票
   发表时间:2005-01-07  
http://forum.iteye.com/viewtopic.php?p=54959#54959

有个包下载
0 请登录后投票
   发表时间:2005-01-07  
已经下载了,嗯,好用,谢谢
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics