获取ServletContext

时间:2015-05-28 03:41:19

标签: spring

我想知道在Spring MVC web中调用bean。我试着调用这个类,但我不知道如何传递ServletContext。这是代码。

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext (sc);
Servicio servicioB= (Servicio) ctx.getBean("servB",Servicio.class);

如何获取ServletContext?。

1 个答案:

答案 0 :(得分:0)

使用以下步骤 -

  1. 自动装配ServletContext对象

    @Autowired ServletContext context;

  2. 获取控制器类中的ServletContext对象

    private ServletContext context;

    public void setServletContext(ServletContext servletContext) { this.context = servletContext; }

  3. 注意:您必须在控制器类中具有实现ServletContextAware接口。

相关问题