如果得到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类,都报异常而失败,郁闷

首先 写个工具类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了