package com.job;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ContextUtil extends ContextLoaderListener
{
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(ContextUtil.class);
private static ApplicationContext context;
@Override
public void contextInitialized(ServletContextEvent event)
{
logger.debug("ContextUtil contextInitialized method running...");
super.contextInitialized(event);
ServletContext context = event.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
ContextUtil.setContext(ctx);
logger.debug("ContextUtil context initialized ok.");
}
public static ApplicationContext getContext()
{
return context;
}
public static void setContext(ApplicationContext context)
{
ContextUtil.context = context;
}
}
如何在manager中获得service对象
BaseService JobService = (BaseService) ContextUtil.getContext().getBean("jobService");