Spring Boot(二)——项目热部署与程序发布
一、项目热部署
1.1 配置依赖
① pom.xml加入devtools依赖,如果scope是provided则无法实现热部署,参考。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
② maven插件配置如下。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
1.2 设置自动构建项目
在Settings/Build->Execution->Deployment/Compiler中勾选Build project automatically,如图所示。
1.3 Register配置
① 组合快捷键(Ctrl+Shift+Alt+/),弹出如下所示。
② 选择Register,找到并选中compiler.automake.allow.when.app.running,重启IDEA。
1.4 测试
重启IDEA后,启动项目,修改HelloController。
修改前代码如下:
@RequestMapping(value = "sayHello", method = RequestMethod.GET)
public String sayHello() {
return "Hello World!";
}
改后代码如下:
@RequestMapping(value = "sayHello", method = RequestMethod.GET)
public String sayHello() {
return "Hello Spring Boot!";
}
改前后效果对比。
二、程序发布
2.1 以jar形式发布
① 默认情况下,打包形式为jar。
<groupId>com.peter</groupId>
<artifactId>springboot-first-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
② 通过Maven执行install,项目相关的依赖也会打到一个jar里面。
③ 进入项目的target文件夹,cmd下运行java -jar **.jar即可。
默认情况下,测试类代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootFirstDemoApplicationTests {
@Test
public void contextLoads() {
}
}
报错如下:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.022 s <<< FAILURE! - in com.peter.SpringbootFirstDemoApplicationTests
[ERROR] initializationError(com.peter.SpringbootFirstDemoApplicationTests) Time elapsed: 0.003 s <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
根据提示,需要在SpringbootFirstDemoApplicationTests类上的注解加上@SpringBootTest(classes = SpringbootFirstDemoApplicationTests.class)。加上后,再次执行install即可打包成功。
2.2 以war形式发布
该部分参考。
① 通过Maven执行clean,清除原有的jar包或者war包;修改pom.xml中packing的值为war,代码如下:
<groupId>com.peter</groupId>
<artifactId>springboot-first-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
② 添加tomcat的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
③ 修改启动类文件,使其继承SpringBootServletInitializer类,并重写configure函数。代码如下:
package com.peter.springbootfirstdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringbootFirstDemoApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstDemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootFirstDemoApplication.class);
}
}
④ 通过Maven执行install,在target文件夹中生成war包。
⑤ 将springboot-first-demo-0.0.1-SNAPSHOT.war拷贝到Tomcat的webapps文件夹下,启动Tomcat。
⑥ 访问http://localhost:8080/springboot-first-demo-0.0.1-SNAPSHOT/sayHello。