搭建簡單 Spring Boot 工程

參考:spring官方文檔   spring官網: spring.io 

  1.  idea 創建一個基礎maven工程
  2. 編輯創建工程的 pom文件如下:
    1. 工程繼承
      spring-boot-starter-parent
      
      <!-- Inherit defaults from Spring Boot -->
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.2.0.RELEASE</version>
      </parent>
    2. 加入spring boot 工程啓動依賴
      1. <!-- Add typical dependencies for a web application -->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
  3. 創建工程啓動類:
    1. Application.java
      1. 加上註解  
        @SpringBootApplication
      2. 如果當前啓動類沒有在對應controller同包下需要加上包掃描 
        @ComponentScan("org.yasser.controller")
  4. 添加配置文件 application.yml 文件
  5. 創建controller類

controller:

package org.yasser.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * SLOGAN: 做一個會寫四種茴,笨且自律的人
 *
 * @Author Yasser
 * @Date 2019-11-05
 * @Description
 */

@RestController
public class HelloWorldController {


    @GetMapping("hello-world")
    public String helloWorld() {
        return "hello world!";
    }


}

application.yml:

server:
  port: 8081

spring:
  application:
    name: yasser-spring-boot

Application.java :

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * SLOGAN: 做一個會寫四種茴,笨且自律的人
 *
 * @Author Yasser
 * @Date 2019-11-05
 * @Description
 */


@SpringBootApplication
@ComponentScan("org.yasser.controller")
public class Application {

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

}

 

pom 文件:

​​​​​

<?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>org.yasser.spring</groupId>
    <artifactId>spring-boot</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

工程目錄結構:

聯繫方式:

GitHub: https://github.com/jinyousen/blog.git

Email: [email protected]

QQ: 245156832

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章