Very weird memory overflow. I hope there will be a wave of boss education.

Project framework spring boot 2.0
I wrote that request and response are automatically injected into the parent class BaseController

< hr >
public  class BaseController{

    @Autowired
    public HttpServletRequest request;

    @Autowired
    public HttpServletResponse response;

}
< hr >

IndexController inherits BaseController

< hr >

public class IndexController extends BaseController {

@RequestMapping("/index")
public String index(Model model) {
    return "index";
}

@RequestMapping("login")
public String login() throws ServletException, IOException {
    request.getRequestDispatcher("/admin/index").forward(request, response);
    return null;
}
< hr >

index page, without any data manipulation, is a static page

< html >

< head >

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">
<title>500</title>

< / head >

< body style= "background:-sharpFFFFFF; color: red; font-size: 40em; text-align: center" >

TEST

< / body >
< / html >

< hr >

the following error occurred when accessing http://localhost:9090/admin/login
, which caused a memory overflow
java.lang.StackOverflowError: null

due to circular references.
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:312)
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:65)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:328)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:323)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
< hr >

1. Notice that my login is forwarded to the home page using request (note that request and response are the BaseController of the parent class)

  1. Note that the problem is not caused by address loop jumps

3. Use the method to inject request and response and there will be no problem
@ RequestMapping ("login")

public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/admin/index").forward(request, response);
    return null;
}
< hr >

is there a big boss to explain?

Feb.23,2022

try the interface first,


:@RequestMapping("/index")<br>public String index(Model model) {

//
request.getRequestDispatcher("/admin/index").forward(request, response);
return "index";

}

@ RequestMapping ("login")
public String login () throws ServletException, IOException {

request.getRequestDispatcher("/admin/index").forward(request, response);
return null;

}

Menu