作者(zhang854429783) eclipse通过tomcat热部署web项目

来源:互联网 发布:淘宝日系男装店铺排行 编辑:程序博客网 时间:2024/05/16 17:39
 

eclipse通过tomcat热部署web项目

标签: eclipsehot-deploytomcat
 246人阅读 评论(1) 收藏 举报
 分类:
  

目录(?)[-]

  1. 怎么配置eclipse热部署
    1. 热部署例子
      1. 1 源代码
      2. 2 修改代码添加一些日志
    2. 在eclipse里面配置热部署
      1. 1 单击Overview选项卡
        1. 11 展开Server Options勾选Serve modules without publishing选项
        2. 12 展开publishing勾选Automatically publish when resources change选项这个配置是用来热部署资源如JSPXML和properties文件等
      2. 2 单击Modules选项卡确保Auto Reload是被禁用的
      3. 4 以DEBUG模式启动tomcat热部署在DEBUG模式下才可用
    3. 限制

怎么配置eclipse热部署

本文章将会演示怎么配置eclipse调试器在不重启服务器的情况下支持热部署,热插拔或者热码替换。 
环境:

  • Eclipse 4.4(老版本也一样支持)
  • Eclipse Tomcat 插件

1. 热部署例子

通过这个简单的热部署例子,可以在不用重启tomcat的情况下是代码修改生效。假设一个简单的Spring MVC Web项目已经通过eclipse部署到tomcat里。

1.1 源代码

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Controller</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">TaskController</span> {</span>    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@RequestMapping</span>(value = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/task"</span>, method = RequestMethod.GET)    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> ModelAndView <span class="hljs-title" style="box-sizing: border-box;">index</span>() {        logger.debug(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"index()"</span>);        ModelAndView model = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ModelAndView();        model.setViewName(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"index"</span>);        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> model;    } ......</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

访问 : http://localhost:8080/project/task

//output 
DEBUG c.m.o.web.controller.TaskController - index()

1.2 修改代码,添加一些日志

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Controllerpublic</span> class TaskController {    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@RequestMapping</span>(value = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/task"</span>, method = RequestMethod.GET)    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> ModelAndView <span class="hljs-title" style="box-sizing: border-box;">index</span>() {        logger.debug(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"index() - NEW - NO RESTART"</span>);        ModelAndView model = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ModelAndView();        model.setViewName(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"index"</span>);        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> model;    } ......</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

再次访问:http://localhost:8080/project/task

//output 
DEBUG c.m.o.web.controller.TaskController - index() - NEW - NO RESTART

2. 在eclipse里面配置热部署

双击tomcat,打开tomcat的配置界面,按如下几个步骤配置eclipse热部署: 
这里写图片描述

2.1 单击“Overview”选项卡

2.1.1 展开“Server Options”,勾选“Serve modules without publishing”选项

这里写图片描述

2.1.2 展开”publishing“,勾选”Automatically publish when resources change“选项。这个配置是用来热部署资源,如:JSP,XML和properties文件等。

双击tomcat 
勾选自动发布当资源改变时

2.2 单击”Modules“选项卡,确保”Auto Reload“是被禁用的

设置

2.4 以DEBUG模式启动tomcat,热部署在DEBUG模式下才可用

3. 限制

热部署只支持方法内代码的修改,如果添加了新的类或方法依然需要重启才能生效。 
为了模拟这种情况,尝试添加了一个新方法,弹出了提示框,JVM不能热切换这个修改的代码。 
替换失败

1
0 0
原创粉丝点击