Spring项目热部署方案

来源:互联网 发布:电脑看淘宝直播 编辑:程序博客网 时间:2024/06/06 03:47

最近做的项目改个文件需要重新启动项目,特别浪费时间,所以想探索下热部署的方案。

Spring-boot项目热部署

经过自测:Spring-boot 1.5.6.RELEASE版本在Intellj可以做到自带热部署

搭建一个Spring-boot项目

maven依赖:

<properties>        <spring.boot.version>1.5.6.RELEASE</spring.boot.version></properties><dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>            <version>${spring.boot.version}</version></dependency>

代码文件:
Application项目启动文件

@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application {    public  static  void  main(String [] args){        SpringApplication.run(Application.class, args);    }}

TestController控制器

@RestControllerpublic class TestController {    @RequestMapping("/user")    public User thing() {        User user =  new User() ;        user.setId(2);        user.setName("姓名");        user.setAge(6);        return  user ;    }}

User实体类

public class User {    private Integer id;    private String name;    private Integer age;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

启动项目测试

启动不用做任何修改,直接“Debug ‘Application’” 即可

浏览器输入:http://localhost:8080/user
返回:{“id”:2,”name”:”姓名”,”age”:6}

然后修改TestController类,将age改为20,然后快捷键:Ctrl+Shift+F9重新编译
这时Debug窗口的左下角有个提示(可以忽略这一步):
Application: 1 class reloaded Stop debug session

然后刷新浏览器:
返回:{“id”:2,”name”:”姓名”,”age”:20}

思考

目前搞不清楚这是Spring-boot的新特性还是Intellj IDEA 2017 的新特性。但是,能用就行啊。

下面提出一些其他人的针对Spring-boot热启动的方案,可能比较适合其他情况:

pom.xml显式添加Spring-boot的热部署模块

<!-- 热部署模块 --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-devtools</artifactId>    <optional>true</optional> <!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖boot项目的项目如果想要使用devtools,需要重新引入 --></dependency>

普通Spring项目的热部署方案

这个项目就是使用Spring-Loaded来热部署了,好处是不用仅仅限制与Spring-Boot项目了

github地址:https://github.com/spring-projects/spring-loaded
使用很简单:
1、下载最新版本的jar包
2、在运行的VM Option加上:

-javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify