您的位置:首页 > 移动开发

Spring的ApplicationContext加载在没有继承的地方加载持久化对象

2013-03-12 10:04 351 查看
public class ServiceLocator
{

private static ApplicationContext applicationContext; //Spring应用上下文环境
//下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法

public static ApplicationContext getContext()
{
return applicationContext;
}

public static Object getBean(String name)
{
return applicationContext.getBean(name);
}

public static void setApplicationContext(ApplicationContext applicationContext){
ServiceLocator.applicationContext = applicationContext;
}

}

 

 

 

public class StartupServlet extends HttpServlet {

/**
* 描述
*/

private static final long serialVersionUID = 6871531526585981445L;

public void init() throws ServletException {
//将applicationContext放入ServiceLocator
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(this.getServletContext());
ServiceLocator.setApplicationContext(ctx);
}
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: