知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

      • Theory:Introduction to Spring boot
      • Theory:Getting started with Spring Boot
      • Theory:Basic project structure
        • Gradle as a skeleton
        • The application properties
        • The Application class
        • Changing the default logo
      • Theory:IoC container
      • Theory:Spring beans
  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Spring boot
Jim
2022-07-14
目录

Theory:Basic project structure

In this topic, you will consider the basic structure of any Spring Boot application. If you don't have such an application, just visit the Spring Initializr site (opens new window) and generate it with Gradle and Java or Kotlin.

# Gradle as a skeleton

The basic structure of a Spring Boot application depends on a build tool that we use for the project. Since we use Gradle, our project has the build.gradle file that describes how to build and manage the dependencies of the project.

  • Java

    .
    ├── build.gradle
    ├── gradle
    │   └── ...
    ├── gradlew
    ├── gradlew.bat
    ├── HELP.md
    ├── settings.gradle
    └── src
        ├── main
        │   ├── java
        │   │   └── org
        │   │       └── hyperskill
        │   │           └── demo
        │   │               └── DemoApplication.java
        │   └── resources
        │       └── application.properties
        └── test
            └── java
                └── org
                    └── hyperskill
                        └── demo
                            └── DemoApplicationTests.java
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
  • Kotlin

    .
    ├── build.gradle
    ├── gradle
    │   └── ...
    ├── gradlew
    ├── gradlew.bat
    ├── HELP.md
    ├── settings.gradle
    └── src
        ├── main
        │   ├── kotlin
        │   │   └── org
        │   │       └── hyperskill
        │   │           └── demo
        │   │               └── DemoApplication.kt
        │   └── resources
        │       └── application.properties
        └── test
            └── kotlin
                └── org
                    └── hyperskill
                        └── demo
                            └── DemoApplicationTests.kt
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

There are also other Gradle-related files that you probably already know.

The initially generated Spring Boot application has several dependencies specified in the build.gradle file.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
1
2
3
4

The first dependency adds the Spring Boot framework to this project, and the second one brings test libraries integrated with Spring Boot. It is enough for the simplest Spring Boot application. As you can see, no boring configurations or excessive amount of dependencies!

提示

These dependencies are called starters and there are more of them. Each such starter dependency is a group of related dependencies. Instead of specifying a lot of related dependencies and their versions (Spring approach), we can add a Spring Boot starter that contains a group of tested-for-compatibility dependencies. All the starters follow a similar naming pattern: spring-boot-starter**-\***, where ***** denotes a particular type of application. For example, if we want to use Spring Web for creating web apps, we can include the spring-boot-starter**-web** dependency that contains web-related dependencies, or if we want to secure our app we can add spring-boot-starter**-security** instead of a bunch of security-related dependencies. This approach greatly simplifies dependency management, is less error-prone, and speeds up the development process.

The source code is placed in the src directory in main and test subdirectories.

# The application properties

Spring Boot uses Convention Over Configuration approach. It means that a developer only needs to specify unconventional aspects of the application, while all other aspects work by default.

To configure some aspects of a Spring Boot application, there is an application.properties file located in src/main/resources. In a newly generated project, this file is empty, but the application still works thanks to default implicit configurations.

We will modify the properties in the next topics.

The properties can also be stored in the YAML format within the application.yml file. We intend to add their examples in the future.

# The Application class

The entry point of our application is DemoApplication class located in org.hyperskill.demo package. This class contains the main method that is commonly-known among developers.

  • Java

    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
  • Kotlin

    @SpringBootApplication
    class DemoSpringApplication
    
    fun main(args: Array<String>) {
        runApplication<DemoSpringApplication>(*args)
    }
    
    1
    2
    3
    4
    5
    6

The presented program looks very simple, almost like a typical hello-world program. Inside main, the SpringApplication.run() or runApplication() method is invoked to launch the application with given arguments.

There is also an important annotation @SpringBootApplication that does all the Spring Boot magic! It makes your application work with incredible abilities: autoconfiguration, managing lifecycle of application components and a lot of other useful things that we will consider later. This annotation shows the convenient approach in Spring Boot: annotating classes and their members to get all features provided by Spring Boot.

提示

@SpringBootApplicantion encapsulates (opens new window) @Configuration, @EnableAutoConfiguration, @ComponentScan.

That is all about the basic structure of Spring Boot applications. In the next topics, you will create new classes and set up configurations to develop an application that behaves as you like.

# Changing the default logo

As you know, when starting a Spring Boot application, you can see the default Spring logo. Let's take a look at one simple but amazing feature as a bonus: you can change the logo to any other, e.g. the logo of your company or the project. To change the logo, first, you need to create a file named banner.txt that contains your custom logo and then put it in the /src/main/resources directory (next to the application.properties file).

Here is our result after running an application with a custom logo:

 __    __   __          __    __       _______. __  
|  |  |  | |  |        |  |  |  |     /       ||  | 
|  |__|  | |  |        |  |__|  |    |   (----`|  | 
|   __   | |  |        |   __   |     \   \    |  | 
|  |  |  | |  |  __    |  |  |  | .----)   |   |__| 
|__|  |__| |__| (_ )   |__|  |__| |_______/    (__) 
                 |/                                 
1
2
3
4
5
6
7

To create a stunning logo, you can use this Spring Boot Banner Generator (opens new window). We hope this small bonus will help you have more fun and remember the structure of a Spring Boot project!

编辑 (opens new window)
#Framework#Spring boot
上次更新: 2022/10/12, 17:01:25
Theory:Getting started with Spring Boot
Theory:IoC container

← Theory:Getting started with Spring Boot Theory:IoC container→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式