(三)用jar包启动springboot项目
1、首先需要在pom文件中添加依赖,spring-boot-starter-parent包含有打包的默认配置,如果要修改的话要可以进行重新定义,具体内容参考https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/maven-plugin/usage.html
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、这里以eclipse为例,将项目进行打包。右键项目 run as --> maven install,出现下图说明打包成功,jar包在项目的target目录下
3、通过命令行进入jar包所在文件夹,运行java -jar 命令,windows的命令为:
4、因为之前的项目为了节省时间,没有写入口类,所以出现了错误
5、对之前的项目进行改写,新建一个MyApplication的入口类,添加注解@SpringbootApplication,这个注解用来告诉springboot的程序入口,原来的controller只保留hello方法,去掉@EnableAutoConfiguration注解
@SpringBootApplication
public class MyApplication {
public static void main(String args[]) {
SpringApplication.run(MyApplication.class, args);
}
}
6、重新打包运行,出现以下日志说明启动成功,这时候访问http://localhost:8080/,就显示我们之前写的hello world