spring - 如何在 Spring 3.1 中构造函数 Autowiring HttpServletResponse?

标签 spring dependency-injection constructor-injection

我有一个请求范围的 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/

相关文章:

android - 使用 Hilt 将存储库注入(inject) Android 中的服务

asp.net-mvc - 使用Unity动态注入(inject)构造函数参数

java - 找不到依赖项 'com.amazonaws'

java - Spring Boot 2 - 使用 Mockito 测试 @Cacheable 不带参数的方法不起作用

java - Spring中的数据库连接管理

java - 在 websphere 共享库中使用 CDI

c# - 使用 Unity 在 ASP.NET MVC 中注入(inject)基本 Controller 构造函数

php - 从php构造函数获取构造参数依赖

c# - 默认依赖注入(inject)?

java - 通过 Hibernate(不是 JDBC)从 blob 中检索图像