Struts的acion中如何取得spring管理的bean

1.

http://hi.baidu.com/chif/blog/item/2ab7da82934c1596f603a690.html

struts源码学习
2008年08月19日 星期二 16:51

       目前项目组采用的技术框架是SSI(struts1.3.8+spring2.5+ibatis2.3),偶对于spring和ibatis其实了解不多,为了了解这三者如何结合的,遂解读了一下代码。
       其中,关于struts和spring的结合使用部分,现有的体系里有这么一个BaseAction类,它是派生自org.apache.struts.actions.DispatchAction,其中override了setServlet方法,setServlet方法是在Action类中定义,改写方法如下:
public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        if (actionServlet != null) {
               ServletContext servletContext = actionServlet.getServletContext();
               WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
              this.facade = (Facade) wac.getBean("facade");
       }
}
        问题来了,setServlet这个方法有时什么时候被调用,又是在什么地方被调用的呢?为了搞清楚这个问题,我们有必要剖析一下struts1.3.8的源码,从http://struts.apache.org/download.cgi上可以当下来。

        首先要从ActionServlet的init方法看起,需要关注的有这么几行代码:

            // Initialize modules as needed
            ModuleConfig moduleConfig = initModuleConfig("", config);

            initModuleMessageResources(moduleConfig);
            initModulePlugIns(moduleConfig);
            initModuleFormBeans(moduleConfig);
            initModuleForwards(moduleConfig);
            initModuleExceptionConfigs(moduleConfig);
            initModuleActions(moduleConfig);

        ModuleConfig这个类其实就是负责解析装配struts config配置文件的类,在initModuleConfig方法中调用了ModuleConfigFactory这个工厂类来创建一个ModuleConfig实例,继续往下看,根据方法名你也能猜到initModuleActions是用来干吗的,在这个方法中通过调用ModuleConfigImp的findActionConfigs方法加载和初始化Action类的,检查一番,发现除了加载配置文件中的Action类之外似乎额外的动作不多,显然我想要找的东西不在这里。
        接下来要看的自然是doGet和doPost方法了,方法内部均是调用另外的一个process方法,在这个方法里,我们看到了如下一段代码:
        ModuleConfig config = getModuleConfig(request);

        RequestProcessor processor = getProcessorForModule(config);

        if (processor == null) {
            processor = getRequestProcessor(config);
        }

        processor.process(request, response);

        看来关键就在于RequestProcessor的process方法了,看到
        // Create or acquire the Action instance to process this request
        Action action = processActionCreate(request, response, mapping);

        你应该心中有数,离胜利不远了,果然,在processActionCreate方法里
        if (instance.getServlet() == null) {
            instance.setServlet(this.servlet);
        }

        终于找到了!就是在这里将ActionServlet对象塞入了Action中。

        其实对于技术框架源码的剖析并不是很难,关键在于把握主线,对于一些旁枝末节可以稍后关注。虽然细节上还有待进一步深入探索,但一开始理清脉络才是最重要的,宏观上没有把握,微观上的行为很可能就是盲目的,其实做任何事都是这样的啊。

 

2.Struts Action 的setServlet() 方法是自动运行的吗?

这个方法会在action初始化的时候调用,这样在action初始化的时候就把spring管理的bean付给了action(作为action的全局变量),但是要注意的是在action中只有无状态的对象才能作为action的全局变量哦.

private PetStoreFacade petStore;   
public void setServlet(ActionServlet actionServlet) {   
        super.setServlet(actionServlet);   
        if (actionServlet != null) {   
            ServletContext servletContext = actionServlet.getServletContext();   
            WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);   
            this.petStore = (PetStoreFacade) wac.getBean("petStore");   
        }   
           
      System.out.println("俺已经初始化了么?");     
    }  


启动tomcat时候,没有输出该语句. 
我进入首页后,点击 Enter the Store,控制台输出了该语句,此时执行的DoNothingAction 
其代码如下: 


代码
public class DoNothingAction extends BaseAction {   
  
  /* Public Methods */  
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {   
    return mapping.findForward("success");   
  }   
}  


所以看来确实是在Action初始化的时候执行的该方法. 
不过每次执行一个BaseAction子类,setServlet()方法就要执行一次. 
参考资料:http://www.javaeye.com/topic/40646
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值