欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

搭建简单 Spring Boot 工程

程序员文章站 2022-05-17 08:28:44
...

参考: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>

工程目录结构:

搭建简单 Spring Boot 工程联系方式:

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

Email: aaa@qq.com

QQ: 245156832

相关标签: spring spring-boot