非常诡异的内存溢出,希望有大佬教育一波

项目框架 spring boot 2.0
我写了 父类 BaseController 里面自动注入 request 和 response


public  class BaseController{

    @Autowired
    public HttpServletRequest request;

    @Autowired
    public HttpServletResponse response;

}

IndexController 都继承 BaseController


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;
}

index 页面,没有任何数据操作,纯一个静态页面
<!DOCTYPE html>
<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: #FFFFFF; color: red; font-size: 40em; text-align: center">

<p>
    TEST
</p>

</body>
</html>


访问 http://localhost:9090/admin/login
出现如下错误,循环引用导致内存溢出
java.lang.StackOverflowError: null

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)

1.注意看我的login 就使用request转发到首页(注意,此时request 和 response 是父类的BaseController)

  1. 注意问题不是地址循环跳引发的问题

3.使用方法注入request 和 response就不会有问题
@RequestMapping("login")

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

有没有大佬解释一波。

汪汪一只猫
浏览 598回答 5
5回答

芜湖不芜

你先试试接口, @RequestMapping("cc") @ResponseBody public Object z() { String parameter = request.getParameter("zz"); System.out.println(parameter); return "你好"; } springboot 2.0.6 没有问题,先一步一步的排查,世界是科学的

婷婷同学_

@justjavac ac[justjavac]

慕田峪4524236

估计你这个 request.getRequestDispatcher("/admin/index").forward(request, response); forward到admin之后又redirect回login了吧?

交互式爱情

你这种写法相当于:@RequestMapping("/index")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; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java