龙威 2008-12-14 16:57
浏览 450
已采纳

如果得到ctx.getBean("myBean")??

本人不才,请教个问题

项目中用的ibatis2.3+spring2.5+struts1.2

在action中可以用getWebApplicationContext()来获取bean对象
public class XXXAction extends DispatchActionSupport {
UserService userService = (UserService) getWebApplicationContext().getBean("userService");
String jsonStr = userService.showList(parameterObj);
}

我现在写了个servlet listener,listener又调用了一个timetask类

public class UserListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent event) {
    timer.schedule(new UserTask(event.getServletContext()), new Date(),MIN * 60 * 1000);
}

}

public class UserTask extends TimerTask {
public void run() {
//在这里如果得到ctx.getBean("myBean")??
}
}

看了api http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContext.html ,尝试了几个applicationcontext类,都报异常而失败,郁闷

  • 写回答

1条回答 默认 最新

  • samungtered 2008-12-14 17:15
    关注

    首先 写个工具类SpringContextUtil
    [code="java"]
    @SuppressWarnings("unchecked")
    public class SpringContextUtil {
    private static ApplicationContext context;

    public static void setApplicationContext(ApplicationContext acx) {
        context = acx;
    }
    
    public static ApplicationContext getApplicationContext() {
        return context;
    }
    
    public static Object getBean(String name) throws BeansException {
        return context.getBean(name);
    }
    
    
    public static Object getBean(String name, Class requiredType) throws BeansException {
        return context.getBean(name, requiredType);
    }
    
    public static boolean containsBean(String name) {
        return context.containsBean(name);
    }
    
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return context.isSingleton(name);
    }
    
    public static Class getType(String name) throws NoSuchBeanDefinitionException {
        return context.getType(name);
    }
    
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return context.getAliases(name);
    }
    

    }
    [/code]

    然后再写个你要的servlet listener
    [code="java"]
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;

    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    /**

    • ContextLoaderListener子类, 把ApplicationContext赋给SpringContextUtil的静态变量Context.
    • */
      public class SpringLoaderListener extends ContextLoaderListener {

      @Override
      public void contextInitialized(ServletContextEvent event) {

      super.contextInitialized(event);
      ServletContext context = event.getServletContext();
      ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
      //设置appliactionContext
      SpringContextUtil.setApplicationContext(ctx);
      
      //你真正要执行的业务
       //timer是哪里来的呢?
       timer.schedule(new UserTask(event.getServletContext()), new Date(),MIN * 60 * 1000); 
      

      }

    }
    [/code]

    后面就是如何实现你的UserTask
    [code="java"]
    public class UserTask extends TimerTask {
    public void run() {
    //在这里可以通过工具类得到
    SpringContextUtil.getBean("myBean");
    }
    }

    [/code]

    这样就可以了... 只需要在web.xml配置下监听就OK了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog