卡住了奇怪的概率。我将ServletContext添加到我的类(定义为@Service),并且它始终返回null。试过@Autowired和没有它。Spring ServletContext返回null
此外,我在应用程序启动时没有收到任何错误。 只有当我打电话 '的ServletContext.getRealPath( “/ WEB-INF /”)' null值
这就是我想要使用它的类:
@Service
public class MyFactory implements ServletContextAware {
@Autowired
ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
private someMethod(){
servletContext.getRealPath("/WEB-INF/"); //this return null
}
}
PS servletContext
本身null,不是.getRealPath("/WEB-INF/")
方法
你当然让Spring为你实例化类,而不是自己调用'new MyFactory()',对吗? ''someMethod()'在* autowiring完成之后被调用,对吧? – Andreas
是的,都是对的。 – user1935987