Spring Boot 简明教程

Spring Boot - Code Structure

Spring Boot 没有可供使用的代码布局。但是,有一些最佳做法将有助于我们。本章将详细讨论这些做法。

Spring Boot does not have any code layout to work with. However, there are some best practices that will help us. This chapter talks about them in detail.

Default package

一个不包含任何 package 声明的类被视为一个 default package 。请注意,通常不建议使用默认的 package 声明。当您使用默认 package 时,Spring Boot 将导致问题,例如自动配置或组件扫描出现故障。

A class that does not have any package declaration is considered as a default package. Note that generally a default package declaration is not recommended. Spring Boot will cause issues such as malfunctioning of Auto Configuration or Component Scan, when you use default package.

Note - Java 为 package 声明推荐的命名约定是反转域名。例如 − com.tutorialspoint.myproject

Note − Java’s recommended naming convention for package declaration is reversed domain name. For example − com.tutorialspoint.myproject

Typical Layout

Spring Boot 应用程序的典型布局如下面给出的图片所示 −

The typical layout of Spring Boot application is shown in the image given below −

typical layout of spring boot application

Application.java 文件应使用 @SpringBootApplication 声明 main 方法。观察下面给出的代码以更好地理解 −

The Application.java file should declare the main method along with @SpringBootApplication. Observe the code given below for a better understanding −

package com.tutorialspoint.myproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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