在Spring 3.1中如何构造构造器-autowire HttpServletResponse? [英] How do I constructor-autowire HttpServletResponse in Spring 3.1?

查看:116
本文介绍了在Spring 3.1中如何构造构造器-autowire HttpServletResponse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个请求范围的bean,我需要访问HttpServletResponse和HttpServletRequest对象。

I have a request scoped bean, and I need to have access to the HttpServletResponse and HttpServletRequest objects.

我需要在构造函数中访问这些对象,因此属性

I need access to these objects in the constructor, so property autowiring is not an option.

我做了以下操作:

@Component
@Scope("request")
public class MyClass{

    @Autowired(required=true)
    public MyClass(HttpServletRequest request, HttpServletResponse response) {

        // do stuff I need to do in the constructor

    }
}

这给我以下错误:


未找到默认构造函数;嵌套的异常是java.lang.NoSuchMethodException:com.foo.bar.MyClass。()

No default constructor found; nested exception is java.lang.NoSuchMethodException: com.foo.bar.MyClass.()

根据此错误消息,它是寻找不存在参数的默认构造函数。我自动构造了构造函数,并特别将 required设置为true,因此注入器应选择该构造函数。由于Bean是请求范围,因此应该可以,但是不能。

According to this error message, it is looking for a paramless default constructor, which does not exist. I autowired the constructor and specifically set "required" to true, so the injector should choose this constructor. Since the bean is request scope this should work, but it does not.

有什么想法吗?

推荐答案

据我了解,您将无法使用标准方法自动连接HttpServletResponse对象。检查WebApplicationContextUtils类的registerWebApplicationScopes方法。可解决的依赖项仅是HttpSession.class和HttpSession.class。

As far as I understand , you will not be able to autowire the HttpServletResponse object using the standard approach . Check the registerWebApplicationScopes method of the WebApplicationContextUtils class . The resolvable dependencies are only the HttpSession.class and HttpSession.class .

检查自定义解决方案此处

这篇关于在Spring 3.1中如何构造构造器-autowire HttpServletResponse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆