[Spring Boot] SpringBoot introduction and environment configuration

1. Introduction to SpringBoot

1.1 Definition of SpringBoot

Spring Boot is a Java-based open source framework released by the Pivotal team in 2014. Its goal is to simplify the initial setup and development process of Spring applications. Spring Boot makes it easy to create standalone, production-grade Spring-based applications. We can use Spring Boot to create applications that can be run directly. It has embedded servers such as Tomcat and Jetty, and does not need to be packaged as a WAR file.

The main goals of Spring Boot are:

  • Provide a quick and convenient way to start Spring applications;
  • Works out of the box, but with custom configuration or code you can start setting things up outside of the default settings;
  • Provides a series of non-functional features commonly used in large-scale projects, such as embedded servers, security, indicators, health checks, externalized configurations, etc.;
  • No XML configuration required.

1.2 SpringBoot features

SpringBoot has many powerful features, including:

  1. Automatic configuration: SpringBoot can automatically provide your application with all the Spring configuration you may need. For example, if Spring Boot sees a MySQL database connector dependency and a data source configuration property on the classpath, it will automatically configure a data source.

    //示例代码:application.properties
    spring.datasource.url=jdbc:mysql://localhost/test
    spring.datasource.username=dbuser
    spring.datasource.password=dbpass
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
  2. Starter dependencies: SpringBoot provides a simplified dependency management through "starter" dependencies. Developers no longer need to add dependencies one by one, but can just add relevant "starter dependencies".

    <!-- 示例代码:在pom.xml中添加Spring Boot Web Starter依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
  3. Embedded server: SpringBoot allows applications to embed Tomcat, Jetty or Undertow, making it possible to run independent Spring applications.

  4. Health Checks: SpringBoot provides a number of automatically configured health checks.

  5. External configuration: SpringBoot allows developers to override the default configuration through external configuration, such as application.properties files.

1.3 Why choose SpringBoot

  1. Simplified configuration: SpringBoot's automatic configuration feature eliminates a large number of configuration files in Spring applications, making the project lighter.

  2. Rapid development: Starting dependencies and automatic configuration functions allow developers to quickly launch new projects and new features.

  3. Embedded server: The embedded server simplifies the deployment process, and developers do not need to tediously configure the server.

  4. Production-ready features: SpringBoot has built-in production-level features, such as security, health checks, and external configuration.

When choosing Spring Boot, developers need to evaluate their needs and goals and whether the features provided by Spring Boot meet those needs. If yes, then SpringBoot may be an ideal choice for developing Java applications.

2. SpringBoot environment requirements

To run a SpringBoot application, you need to ensure that your development environment meets the following requirements:

2.1 Java environment

SpringBoot 2.x requires Java 8 or higher. At the time of writing this article, the recommended Java version is Java 11. You can check your Java version by running the following command in the terminal or command line:

java -version

If Java is not installed on your system or the version is too low, you need to go to the Oracle official website or the OpenJDK official website to download and install the Java version suitable for your operating system.

2.2 Maven or Gradle environment

SpringBoot applications can use Maven or Gradle as build tools. You need to make sure your system has the correct version of the build tools installed. At the time of writing this article, SpringBoot 2.x recommends using Maven 3.3 or above or Gradle 4.4 or above.

Check your Maven version:

mvn -v

Check your Gradle version:

gradle -v

If Maven or Gradle has not been installed on your system, or the version is too low, you can go to the Maven official website or Gradle official website to download and install the version suitable for your operating system.

2.3 IDE selection

Although you can use any text editor to write Spring Boot applications, using an integrated development environment (IDE) will make the development process more efficient. Here are some popular Java IDEs:

  • IntelliJ IDEA: Developed by JetBrains, this is a powerful Java IDE that provides a powerful set of tools and features, including code auto-completion, code navigation, refactoring tools, version control, etc. It has a free community version and a paid professional version.

  • Eclipse: This is an open source Java IDE that provides a powerful set of functions, including code editing, debugging, testing and code management.

  • Spring Tool Suite (STS): This is an IDE designed specifically for Spring developers. It is based on Eclipse and contains many functions for Spring development.

Which IDE you choose depends mainly on your personal preferences and development needs. After installing and configuring your IDE, you need to make sure that the IDE supports Java and your build tool of choice (Maven or Gradle).

3. SpringBoot environment configuration

After installing the necessary development tools, we need to perform some environment configuration to enable smooth SpringBoot development.

3.1 Java environment configuration

The installation of Java is usually very intuitive, just download the corresponding installation package and install it. The point is that environment variables need to be configured. The following are the steps to configure Java environment variables in a Windows environment:

  1. Right-click My Computer and select Properties.
  2. Click Advanced system settings.
  3. In the System Properties window, click Environment Variables.
  4. Find Path in the system variables and click Edit.
  5. Add the path to your Java bin directory at the end of the variable value. For example C:\Program Files\Java\jdk-11\bin, separate each path with a semicolon.
  6. Similarly, add a new system variable JAVA_HOMEand set the variable value to the JDK installation directory, such as C:\Program Files\Java\jdk-11.

3.2 Maven environment configuration

Maven's environment configuration is similar to Java. After downloading and unzipping Maven, you need to configure environment variables. The following are the steps to configure Maven environment variables in a Windows environment:

  1. Find Path in the system variables and click Edit.
  2. Add the path to your Maven bin directory at the end of the variable value. For example C:\Program Files\Apache\maven\bin, separate each path with a semicolon.
  3. Similarly, add a new system variable M2_HOMEand set the variable value to the Maven installation directory, such as C:\Program Files\Apache\maven.

3.3 Configuration of SpringBoot project in IDE

Taking IntelliJ IDEA as an example, the steps to create and configure a SpringBoot project are as follows:

  1. Open IntelliJ IDEA and click File -> New -> Project.
  2. In the new window, select Spring Initializr on the left.
  3. Select your project SDK (this should be the Java JDK you installed earlier) and Initializr Service URL (default is https://start.spring.io), click Next.
  4. Fill in the Group, Artifact, Name, Description and other information of your project and click Next.
  5. Select the dependencies you need. For example, if you want to create a Web project, you can select Spring Web in Spring Web and click Next.
  6. Confirm your project settings and click Finish.

In this way, you create a SpringBoot project in IntelliJ IDEA. The IDE will automatically configure the project environment for you, and you can start writing code directly.

4. Create a basic SpringBoot project

SpringBoot makes it very easy to create basic Java applications. You can use Spring Initializr to quickly create a project, or you can create it manually.

4.1 Create a project using Spring Initializr

Spring Initializr is an online tool that can help you quickly create a SpringBoot project. Here are the steps to create a project using Spring Initializr:

  1. Open the Spring Initializr website (https://start.spring.io/).
  2. Select the Maven or Gradle, Java version, and Spring Boot version you need.
  3. Fill in your project information in the "Project Metadata" section, including GroupId, ArtifactId, Name, etc.
  4. In the "Dependencies" section, select the dependencies you need. For example, if you plan to create a web application, you should add "Spring Web" dependency.
  5. Click the "Generate" button, and Spring Initializr will generate a project compressed package containing all the dependencies you selected and some basic code. You can download and unzip this compressed package, and then open it in your IDE.

4.2 Manually create a SpringBoot project

If you want to understand the basic structure of a SpringBoot project, you can try to create a project manually. The following are the basic steps for manually creating a SpringBoot project:

  1. In your IDE, create a new Maven or Gradle project.
  2. In your project's pom.xml (for Maven projects) or build.gradle (for Gradle projects) file, add Spring Boot's parent project as follows:

For Maven projects:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.x.x</version>
</parent>

For Gradle projects:

plugins {
    
    
    id 'org.springframework.boot' version '2.x.x'
    id 'io.spring.dependency-management' version '1.x.x'
    id 'java'
}
  1. Likewise, in the pom.xml or build.gradle file, add the dependencies you need. For example, add "Spring Web" dependency:

For Maven projects:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

For Gradle projects:

dependencies {
    
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
  1. Create a Java class, for example "Application.java", and add the following code:
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);
    }

}

This class is the entry point of the SpringBoot application.

The above are the basic steps for manually creating a SpringBoot project. Although creating the project manually requires more steps, it can help you better understand how a SpringBoot project is structured and works.

5. Run the SpringBoot project

After creating the SpringBoot project, the next step is to run the project. You can run the project in the IDE or via the command line.

5.1 Run the project in the IDE

Running a SpringBoot project in the IDE is usually very simple. Here are the steps to run a project in IntelliJ IDEA:

  1. In the project view, find your main class (contained public static void main(String[] args)class), for example "Application.java".
  2. Right-click on the class and select "Run 'Application.main()'".

In this way, your SpringBoot project will be running in the IDE, and you should be able to see the Spring startup log in the console.

5.2 Run the project through the command line

In addition to running projects in the IDE, you can also run projects through the command line. This is very useful for deploying projects. Here are the steps to run Maven and Gradle projects through the command line:

For Maven projects, open the command line, switch to the root directory of the project, and run the following command:

mvn spring-boot:run

For Gradle projects, run the following command:

./gradlew bootRun

In this way, your SpringBoot project will be running on the command line, and you should be able to see Spring's startup log.

Note that if your system does not have Maven or Gradle environment variables configured correctly, you may need to use the full path to run the mvn or gradle command.

6. Structure of SpringBoot project

Understanding the SpringBoot project structure and configuration files is very important to use SpringBoot effectively. In this section, we will analyze the project's directory structure and basic configuration files.

6.1 Analysis of project directory structure

The directory structure of a typical SpringBoot project is as follows:

myproject
|-- src
|   |-- main
|   |   |-- java
|   |   |   |-- com
|   |   |   |   |-- mycompany
|   |   |   |   |   |-- myproject
|   |   |   |   |   |   |-- Application.java
|   |   |-- resources
|   |   |   |-- static
|   |   |   |-- templates
|   |   |   |-- application.properties
|   |-- test
|   |   |-- java
|   |   |   |-- com
|   |   |   |   |-- mycompany
|   |   |   |   |   |-- myproject
|   |-- pom.xml
|-- .gitignore
|-- README.md

Here's an explanation of each directory and file:

  • src/main/java: This directory contains all Java source code, usually organized according to a package structure. Application.javaIt is the entry point of the application.
  • src/main/resources: This directory contains all resource files, such as configuration files and static resources.
  • src/main/resources/static: This directory contains all static resources such as HTML, CSS and JavaScript files. SpringBoot will automatically serve the files in this directory as static resources.
  • src/main/resources/templates: This directory contains all view templates, such as Thymeleaf or FreeMarker templates. SpringBoot will automatically process the files in this directory as view templates.
  • src/main/resources/application.properties: This file is the main configuration file of SpringBoot. You can configure your application in this file, such as database connections, server ports, etc.
  • src/test/java: This directory contains all test code, usually organized according to the package structure.
  • pom.xml: This file is the Maven configuration file, which defines the project's dependencies and build settings.

6.2 Introduction to the basic configuration file of the project

SpringBoot's main configuration file is application.propertieslocated in src/main/resourcesthe directory. You can configure your application in this file, for example:

# 服务器端口
server.port=8080

# 数据库连接
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# JPA设置
spring.jpa.hibernate.ddl-auto=update

You can also use application.ymlan alternative application.properties, using YAML formatted configuration. This format is easier to read and write, but requires additional dependencies.

The above is the basic structure and configuration file of the SpringBoot project. Understanding these can help you use SpringBoot more effectively.

7. Summary

In this blog, we introduce SpringBoot in detail, including its definition, features and why we choose SpringBoot. We also introduced in detail how to configure the SpringBoot development environment, how to create and run a SpringBoot project, and the directory structure and configuration files of the SpringBoot project.

SpringBoot is a powerful framework that makes creating Java applications very easy. Through SpringBoot, you can quickly create applications with enterprise-level features, such as database access, security, transaction management, messaging services, etc.

In the next blog, we will delve deeper into the features and functionality of SpringBoot, including how to create a RESTful API, how to access the database, how to add security, and how to test SpringBoot applications.

I hope this blog will be helpful to you. If you have any questions or suggestions, please leave a message for discussion.

Guess you like

Origin blog.csdn.net/weixin_52665939/article/details/131883061