Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java SpringBoot Framework

Java SpringBoot Framework

Common Java Libraries Common Java Libraries


SpringBoot is an extension project of the Spring Framework, designed to simplify the initial setup and development process of Spring applications.

SpringBoot adopts the "convention over configuration" philosophy, enabling developers to quickly start and run Spring applications.

Core Features of SpringBoot

  1. Auto-Configuration: Automatically configures the Spring application based on the jar dependencies added to the project
  2. Standalone Execution: Embeds servers such as Tomcat, Jetty, or Undertow, eliminating the need to deploy WAR files
  3. Simplified Dependency Management: Simplifies Maven/Gradle configuration through starter dependencies
  4. Production-Ready: Provides features such as metrics, health checks, and externalized configuration
  5. No Code Generation: No XML configuration required

Core Components of SpringBoot

1. SpringBoot Starter

A SpringBoot Starter is a set of convenient dependency descriptors that provide everything you need in one place. For example:

2. SpringBoot AutoConfiguration

SpringBoot automatically configures the application based on the jars in the classpath. For example, if the H2 database is on the classpath, SpringBoot automatically configures an in-memory database.

3. SpringBoot Actuator

Provides production-grade features to help monitor and manage applications:


Creating Your First SpringBoot Application

1. Create a Project with Spring Initializr

Access the start.spring.io, and select:

2. Project Structure

A typical SpringBoot project structure:

src/
  main/
    java/
      com/example/demo/
        DemoApplication.java   # 主启动类
    resources/
      application.properties  # 配置文件
  test/
    java/
      com/example/demo/
        DemoApplicationTests.java # 测试类

3. Write a Simple REST Controller

Example

@RestController

@RequestMapping

(

"/api"

)

public

class

HelloController

{

    @GetMapping

(

"/hello"

)

   

public

String

sayHello

(

)

{

       

return

"Hello, SpringBoot!"

;

   

}

}

4. Run the Application

Run DemoApplication the main method, the application will start on the default port 8080.

Access the http://localhost:8080/api/hello You will see the returned "Hello, SpringBoot!" message.


SpringBoot Configuration

1. Configuration Files

SpringBoot supports multiple configuration approaches:

Example application.properties:

Example

server.port

=

9090

spring.datasource.url

=

jdbc:mysql://localhost:3306/mydb

spring.datasource.username

=

root

spring.datasource.password

=

secret

2. Multi-Environment Configuration

You can create different configuration files for different environments:

Pass spring.profiles.active Specify the active environment:

Example

spring.profiles.active

=

dev


SpringBoot Data Access

1. Using Spring Data JPA

Add the dependency:

Example

   

org.springframework.boot

   

spring-boot-starter-data-jpa

Define the entity class:

Example

@

Entity

public

class

User

{

    @Id

    @GeneratedValue

(

strategy

=

GenerationType.

IDENTITY

)

   

private

Long

id

;

   

private

String

name

;

   

private

String

email

;

   

// getters and setters

}

Create the Repository interface:

Example

public

interface

UserRepository

extends

JpaRepository

<

User, Long

{

    List

<

User

findByName

(

String

name

)

;

}

Use it in the service layer:

Example

@Service

public

class

UserService

{

    @Autowired

   

private

UserRepository userRepository

;

   

   

public

List

<

User

getUsersByName

(

String

name

)

{

       

return

userRepository.

findByName

(

name

)

;

   

}

}


SpringBoot Best Practices

  1. Layered Architecture: Follow the Controller-Service-Repository layering
  2. Exception handling: When using @ControllerAdvice Global Exception Handling
  3. Logging: Use SLF4J for logging
  4. Unit testing: Write sufficient tests for business logic
  5. API Documentation: Integrate Swagger to generate API documentation

Example: Global Exception Handling

Example

@ControllerAdvice

public

class

GlobalExceptionHandler

{

   

    @ExceptionHandler

(

ResourceNotFoundException.

class

)

   

public

ResponseEntity

<

ErrorResponse

handleResourceNotFound

(

ResourceNotFoundException ex

)

{

        ErrorResponse error

=

new

ErrorResponse

(

           

"NOT_FOUND"

,

            ex.

getMessage

(

)

       

)

;

       

return

new

ResponseEntity

<>

(

error, HttpStatus.

NOT_FOUND

)

;

   

}

}


SpringBoot Advanced Topics

  1. Spring Security: Authentication and authorization
  2. Spring Cloud: Microservices architecture
  3. Spring Batch: Batch processing
  4. WebFlux: Reactive programming
  5. Docker Integration: Containerized deployment

SpringBoot greatly improves the development efficiency of Java applications by simplifying configuration and providing out-of-the-box features. It is one of the indispensable frameworks in modern Java development.

Common Java Libraries Common Java Libraries