SpringBoot项目

88 阅读1分钟

创建项目(本人电脑的正常操作)

IDEA中新建project:File->New->project...

image.png 点击创建

找到设置,配置gradle

image.png 应用并确定

build.gradle引入spring-boot-starter-web包

plugins {
    id 'idea'
    id 'java'
    id 'org.springframework.boot' version '2.7.5'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'

}

group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = '1.8'


repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

test {
    useJUnitPlatform()
}

刷新下gradle

image.png

在java下创建主启动类:DemoApplication

image.png

代码如下:

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
 SpringApplication.run(DemoApplication.class, args);
  }
}

image.png

创建一个demo接口

package org.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/main")
public class HelloWorldController {
 @GetMapping("/hello")
 public String test() {
 return "Hello World";
   }
}

若端口被占用,则修改端口

server:
  port: 8088

启动项目

对主启动类进行右键操作:Run/Debug 'DemoApplication'即可

image.png

生成jar包

在gradle内,点击Tasks->build->build/jar

image.png

部署

服务自动启动

  • 下载WinSW工具
  • 把我们下载好的两个文件放到和我们的项目jar同一个目录下面,然后把WinSW.NET4.exe改个名 字。我这里是改成了springboot.exe,同时把sample-minimal.xml也改成和exe文件一样的名 字,也就是springboot.xml。
  • 接下来需要对xml文件进行一下编辑,如下。
  • 注册服务:在springbootDemo.exe 所在目录打开dos命令窗口,为了防止出现问题,最好用管理 员身份打开。
<service>
  
  <!-- 安装成windows服务后的服务名-->
  <id>springbootDemo</id>
  <!-- 显示的服务名称 -->
  <name>springbootDemo</name>
  <!-- 服务描述 -->
  <description>这是winsw生成的服务,专门用于启动springboot项目</description>
    <!-- jdk可执行程序的路径,我们已经安装了jdk,只要“java” 即可-->
  <executable>java</executable>
  <!--参数-->
  <arguments> -jar  springbootDemo-1.0-SNAPSHOT.jar</arguments>
</service>
  • 输入命令 之后进入到电脑的服务找到我们的服务:springbootDemo,右键启动即可。