Spring-无法实例化请求范围中的组件对象

Spring-无法实例化请求范围中的组件对象,spring,spring-ioc,Spring,Spring Ioc,我正在尝试实例化一个组件对象,我在applicationContext.xml中为其声明了项。实例化我的目标组件类的流程如下 CalculateControl->CalculateService->CalculateComponent 在哪里 CalculatorController-scope=request,用@Controller注释并包含在webapplicationContext.xml中的组件扫描中 CalculatorService-Scope=singleton,用@servic

我正在尝试实例化一个组件对象,我在applicationContext.xml中为其声明了
项。实例化我的目标组件类的流程如下

CalculateControl->CalculateService->CalculateComponent

在哪里

CalculatorController-scope=request,用@Controller注释并包含在webapplicationContext.xml中的组件扫描中

CalculatorService-Scope=singleton,用@service注释并包含在applicationContext.xml的组件扫描中

CalculateComponent-scope=请求,无注释,从webApplicationNext.xml和applicationContext.xml中的组件扫描中排除。在webApplicationContext.xml中定义了bean条目,范围=请求。还包括
定义中的

我在web.xml中包含了以下条目

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:spring/applicationContext.xml
            /WEB-INF/mvc-dispatcher-servlet.xml
            ....Other resource xmls
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- To retrieve session related information -->
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<listener>
    <listener-class>
            org.springframework.web.context.request.RequestContextListener 
    </listener-class>
</listener> 


<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

上下文配置位置
类路径:spring/applicationContext.xml
/WEB-INF/mvc-dispatcher-servlet.xml
..其他资源XML
org.springframework.web.context.ContextLoaderListener
org.springframework.security.web.session.HttpSessionEventPublisher
org.springframework.web.context.request.RequestContextListener
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/
请注意,CalculateComponent有一个3参数的ref对象构造函数,这三个构造函数在webApplicationContext.xml中都有
条目,其作用域为单例,并且没有注释

当发送创建CalculateComponent对象的请求时,spring容器抛出以下错误

“未找到线程绑定请求:您是指请求吗?” 实际web请求之外的属性,或处理请求 在最初接收线程之外?如果您实际上是 在web请求中操作,但仍然收到此消息,您的 代码可能正在外部运行 DispatcherServlet/DispatcherPortlet:在这种情况下,使用 RequestContextListener或RequestContextFilter公开当前 请求。”

请告知

更新:

当我从contextConfigLocation中删除/WEB-INF/mvc-dispatcher-servlet.xml并启动服务器时,我得到了一个自动连接失败的错误——“找不到依赖项为“”的CalculateComponent类型的合格bean”,即使在我将作用域从request更改为singleton之后也是如此

然后我在CalculateService中注释了CalculateComponent的自动连接,现在我可以看到CalculateComponent启动了两次(正如我提到的@Serge Ballesta)。因此我得出结论,CalculateService是在加载DispatcherServlet之前通过ContextLoaderListener(applicationContext.xml中的bean条目)启动的(即mvc DispatcherServlet中没有提到任何bean)

我在contextConfigLocation中再次添加了/WEB-INF/mvc-dispatcher-servlet.xml,但这次是作为第一个条目(即位于applicationContext.xml之上)。现在CalculateComponent又加载了两次,自动连线是在单例作用域中完成的。在这个设置中,我将CalculateComponent的作用域更改回request,但再次出现“我没有找到线程绑定的请求”错误

所以问题是,


ContextLoaderListener试图在DispatcherServlet(CalculateComponent)加载/可用之前初始化其资源

有一个请求范围的控制器很奇怪。。。好吧,框架希望它是一个单体。试着把它放在单例范围内

我认为
/WEB-INF/mvc dispatcher servlet.xml
中的所有bean都实例化了两次:

  • 根应用程序上下文中的第一个在全局上下文ConfigLocation中声明
  • 接下来,在名为
    mvc dispatcher的servlet的servlet应用程序上下文中

您应该重命名该文件,使其不作为servlet应用程序上下文加载,或者将其从根上下文中删除,回答我自己的问题。正如我在问题中提到的,这是我获取请求范围的组件bean的流程

CalculateControl->CalculateService->CalculateComponent

但是CalculateController是通过异步请求调用的我们无法从异步请求线程访问web范围的bean。


参考:

我从所有控制器中删除了@scope值,并从context参数中删除了条目/WEB-INF/mvc-dispatcher-servlet.xml。现在,当我启动服务器时,我得到一个错误“没有为依赖项找到CalculateComponent类型的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项批注:{@org.springframework.beans.factory.annotation.Autowired(必需=true),@org.springframework.beans.factory.annotation.Qualifier(value=“BeandefNameforCalculateComponenet“@Gopal:这现在是一个更标准的错误…当您使用代理在单例中连接请求范围的bean时,
CalculateComponent
必须作为接口连接,因为默认情况下Spring使用JDK代理。您可以强制使用javassist类代理(
proxy\u target\u class=true
)但千万不要两者混用。谢谢你的回复。添加到请求范围的bean不足以代理类。我还在pom.xml中添加了cglib。@Gopal:是的,它实际上是cglib。你正确地修复了我的评论:-)如果您可以从我的问题中看到,我正在使用aop范围的代理来处理请求范围的bean,但是我仍然得到了错误。请给出建议。