简体   繁体   English

请求范围的Bean未创建

[英]Request scoped bean is not creating

I don't know what I am missing, checked all the links which tells about request-scoped bean creation but I am missing something that's why request-scoped bean is not creating. 我不知道我缺少什么,检查了所有有关创建请求范围的Bean的链接,但是我丢失了一些内容,这就是为什么不创建请求范围的Bean的原因。

Here is request scoped bean class: 这是请求范围的Bean类:

package com.spring.beans;

public class RequestScopedBean 
{
    public RequestScopedBean()
    {
        System.out.println("RequestScopedBean constructor");
    }   
    @Autowired
    HttpServletRequest request;

    public void getSessionId()
    {
        if(request != null)
        {
            System.out.println(request.getSession().getId());
        }
    }
}

Web.xml Web.xml

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>com.spring.systemaudit.OESLog4jListener</listener-class>
    </listener>
    <listener>
     <listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor</listener-class>
    </listener>  
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    ...
</web-app>

and root-context.xml 和root-context.xml

...
<bean id="requestScopedBean" class="com.spring.beans.RequestScopedBean" scope="request">

</bean>
...

If I make scope="singleton" then bean is created because I can see sysout on console while starting server then why not with scope="request" . 如果我使scope="singleton"那么将创建bean,因为在启动服务器时可以在控制台上看到sysout ,然后为什么不使用scope="request" What I am missing? 我缺少什么?

I guess it's successfully created... but on request. 我想它已经成功创建了……但是应要求提供。 The bean must be created on call a controller where the bean is used. 必须在调用使用该bean的控制器时创建该bean。 Eg when you type localhost:8080/someRequestMapping your controller is triggered and if the Controller has a reference to your bean it is created. 例如,当您键入localhost:8080/someRequestMapping将触发控制器,并且如果控制器具有对您bean的引用,则会创建它。

By default the reference is a Proxy which creates Request scoped beans on demand. 默认情况下,引用是一个代理,可根据需要创建请求范围的Bean。

On startup no request exists so the bean is not created (only proxy is created) 启动时不存在任何请求,因此不会创建Bean(仅创建代理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM