springMVC redirect view时存在内存泄漏-解决方案

出问题的代码:return new ModelAndView("redirect:" + targetUrl); 其中targetUrl是需要转向的目标URL,可能包含动态的参数。

 

         springMVC在处理上述代码时,会认为这种redirect:XXX是一个View定义,会在其beanfactory中尝试寻找对应的 bean定义,在寻找过程中,会向其beanfactory内部的一个alreadyCreated Map变量中缓存本次寻找结果(key是viewName,value是布尔值true)。在存在巨量的重定向时,这个alreadyCreated就会引起内存泄漏。在spring的buglist中可以找到相同情况的说明:https://jira.springsource.org/browse/SPR-10065

   

         spring提供了解决该问题的方案,但是,是通过一些个人觉得比较晦涩的用法,很容易会误用。(https://jira.springsource.org/browse/SPR-10065有说明),因此建议最好的方法是:在重定向中尽可能的避免使用spring提供的redirect:view视图。而使用标准的JEE API:

    String encodedRedirectURL = response.encodeRedirectURL(targetUrl);

    response.sendRedirect(encodedRedirectURL);

 

         并且,该问题在测试过程中很容易会被忽略,因为每次向alreadyCreated Map中只增加一个URL字符串,每次泄漏的内存很微小,因此在压测过程中容易被忽略,但是长期积累就会引起问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整合Thymeleaf和Element-UI,需要在SpringMVC项目中配置Thymeleaf模板引擎和Element-UI前端框架。 首先,在Spring MVC项目中引入Thymeleaf和Element-UI的依赖。可以在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Element-UI --> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> <version>0.40</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>element-ui</artifactId> <version>2.10.1</version> </dependency> </dependencies> ``` 然后,在Spring MVC配置文件中配置Thymeleaf模板引擎。可以在application.properties或application.yml文件中添加以下配置: ```properties # Thymeleaf spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html;charset=UTF-8 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order=1 spring.thymeleaf.view-names=* spring.thymeleaf.check-template-location=true ``` 最后,在Thymeleaf模板中使用Element-UI的组件。可以在HTML文件中添加以下代码: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:t="http://www.thymeleaf.org/extras/thymeleaf-spring4" xmlns:el="http://www.thymeleaf.org/extras/element-ui" lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- Element-UI --> <link rel="stylesheet" href="/webjars/element-ui/2.10.1/theme-chalk/index.css"> <script src="/webjars/element-ui/2.10.1/index.js"></script> </head> <body> <!-- 使用Element-UI的组件 --> <el-button>Button</el-button> </body> </html> ``` 以上就是整合Thymeleaf和Element-UI的步骤,希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值