我正在尝试更新旧的 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)