环境准备
创建工程
pom.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tx.springboot</groupId>
<artifactId>tx_demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tx_demo2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类TxDemo2Application
@SpringBootApplication
public class TxDemo2Application {
public static void main(String[] args) {
SpringApplication.run(TxDemo2Application.class, args);
}
}
测试类TestController
@RestController
public class TestController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
第一种:直接main方法启动TxDemo2Application
测试访问
第二种:通过maven插件来启动
输入:
C:\Users\rlsl180506\Desktop\tx_demo2>mvn spring-boot:run
测试访问
第三种 打jar包来访问
C:\Users\rlsl180506\Desktop\tx_demo2>mvn clean package
进入jar所在路径执行
C:\Users\rlsl180506\Desktop\tx_demo2\target>java -jar tx_demo2-0.0.1-SNAPSHOT.jar
测试结果:
第四种 通过docker容器虚拟化运行
首先我要在linux的docker环境下。其次把第三步打好的jar拷贝到linux下的指定目录,修改名字成tx_demo2.jar
mv tx_demo2-0.0.1-SNAPSHOT.jar tx_demo2.jar
创建DockerFile文件
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 10001
ENTRYPOINT ["java","-jar","/app.jar"]
tx_demo2.jar和DockerFile在同一个路径构建镜像
docker build --build-arg JAR_FILE=tx_demo2.jar -t tx_demo2:1.0 .
启动容器:
docker run -p 8080:8080 tx_demo2:1.0
访问测试
到此这篇关于springboot的四种启动方式的文章就介绍到这了,更多相关springboot 启动方式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!