小编Ben*_*den的帖子

如何在 @Configuration 或 @SpringBootApplication 类中访问 ServletContext

我正在尝试更新旧的 Spring 应用程序。具体来说,我试图从旧的 xml 定义形式中提取所有 bean 并将它们拉入 @SpringBootApplication 格式(同时显着减少定义的 bean 总数,因为其中许多不需要豆子)。我当前的问题是我无法弄清楚如何使 ServletContext 对需要它的 bean 可用。

我当前的代码如下所示:

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
        // …
Run Code Online (Sandbox Code Playgroud)

java spring autowired postconstruct spring-boot

4
推荐指数
1
解决办法
5787
查看次数

标签 统计

autowired ×1

java ×1

postconstruct ×1

spring ×1

spring-boot ×1