`

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

 
阅读更多
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);
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics