我有一个请求范围的 bean,并且我需要访问 HttpServletResponse 和 HttpServletRequest 对象。
我需要在构造函数中访问这些对象,因此属性 Autowiring 不是一个选项。
我做了以下事情:
@Component
@Scope("request")
public class MyClass{
@Autowired(required=true)
public MyClass(HttpServletRequest request, HttpServletResponse response) {
// do stuff I need to do in the constructor
}
}
这给了我以下错误:
No default constructor found; nested exception is java.lang.NoSuchMethodException: com.foo.bar.MyClass.()
根据此错误消息,它正在寻找一个不存在的无参数默认构造函数。我 Autowiring 了构造函数并专门将“required”设置为 true,因此注入(inject)器应该选择此构造函数。由于 bean 是请求范围,这应该可以工作,但事实并非如此。
有什么想法吗?
最佳答案
据我了解,您将无法使用标准方法 Autowiring HttpServletResponse 对象。检查 WebApplicationContextUtils 类的 registerWebApplicationScopes 方法。可解析的依赖项只有 HttpSession.class 和 HttpSession.class 。
检查自定义解决方案 here .
关于spring - 如何在 Spring 3.1 中构造函数 Autowiring HttpServletResponse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8887831/