如何在@Configuration或@SpringBootApplication类内部访问ServletContext [英] How to get access to ServletContext inside of an @Configuration or @SpringBootApplication class

查看:703
本文介绍了如何在@Configuration或@SpringBootApplication类内部访问ServletContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新旧的Spring应用程序.具体来说,我正在尝试将所有bean从旧的xml定义的表单中拉出,并将它们拉成@SpringBootApplication格式(同时大幅减少了已定义的bean的总数,因为其中许多不需要豆子).我当前的问题是我无法弄清楚如何使ServletContext可用于需要它的bean.

I'm attempting to update an old Spring application. Specifically, I'm trying to pull all of the beans out of the old xml-defined form and pull them into a @SpringBootApplication format (while dramatically cutting down on the overall number of beans defined, because many of them did not need to be beans). My current issue is that I can't figure out how to make the ServletContext available to the beans that need it.

我当前的代码如下:

package thing;

import stuff

@SpringBootApplication
public class MyApp {

    private BeanThing beanThing = null;

    @Autowired
    private ServletContext servletContext; 

    public MyApp() {
        // Lots of stuff goes here.
        // no reference to servletContext, though
        // beanThing gets initialized, and mostly populated.
    }

    @Bean public BeanThing getBeanThing() { return beanThing; }

    @PostConstruct
    public void populateContext() {
        // all references to servletContext go here, including the
        // bit where we call the appropriate setters in beanThing
    }
}

我得到的错误:Field servletContext in thing.MyApp required a bean of type 'javax.servlet.ServletContext' that could not be found.

那么...我想念什么?我应该添加到路径中吗?我需要实现一些接口吗?我不能自己提供Bean,因为重点是我试图访问自己没有的servlet上下文信息(getContextPath()和getRealPath()字符串).

So... what am I missing? Is there something I'm supposed to be adding to the path? Is there some interface I need to implement? I can't provide the bean myself because the whole point is that I'm trying to access servlet context info (getContextPath() and getRealPath() strings) that I don't myself have.

推荐答案

请注意访问ServletContext的最佳实践:您不应该在主应用程序类中使用它,而是e. G.控制器.

Please be aware of the best practice for accessing the ServletContext: You shouldn't do it in your main application class, but e. g. a controller.

否则,请尝试以下操作:

Otherwise try the following:

实现ServletContextAware界面,Spring会为您注入它.

Implement the ServletContextAware interface and Spring will inject it for you.

删除@Autowired变量.

添加setServletContext方法.

@SpringBootApplication
public class MyApp implements ServletContextAware {

    private BeanThing beanThing = null;

    private ServletContext servletContext; 

    public MyApp() {
        // Lots of stuff goes here.
        // no reference to servletContext, though
        // beanThing gets initialized, and mostly populated.
    }

    @Bean public BeanThing getBeanThing() { return beanThing; }

    @PostConstruct
    public void populateContext() {
        // all references to servletContext go here, including the
        // bit where we call the appropriate setters in beanThing
    }

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


}

这篇关于如何在@Configuration或@SpringBootApplication类内部访问ServletContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆