springboot框架的基本构建

Spring Boot 是一个用于简化 Spring 应用初始搭建以及开发过程的框架。它通过提供默认配置和约定优于配置的原则,让开发者能够快速启动和运行项目。本文将介绍 Spring Boot 的基础配置,帮助你快速上手并理解其核心概念。

## 环境准备

在开始之前,确保你的开发环境已经准备就绪。你需要以下工具和库:

- Java Development Kit (JDK) 8或更高版本
- 一个IDE(例如IntelliJ IDEA或Eclipse)
- Maven或Gradle(用于依赖管理)

## 创建一个Spring Boot项目

按照以下步骤操作:

1. 创建一个新的Maven项目。
2. 在`pom.xml`文件中添加Spring Boot的依赖:(记得在</project>上方添加)

```xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>
```

3. 创建一个启动类:(记得在Java包中再创建一个包,在自己创建的包里创建类

```java
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);
    }
}
```

## 基础配置

### application.properties

Spring Boot 使用 `application.properties` 或 `application.yml` 文件来进行配置。你可以在 `src/main/resources` 目录下找到这个文件。以下是一些常见的配置示例:(至于服务器端口不一定必须8080,不过必须确定不能重复,确定但当前没人使用)

```properties
# 服务器端口
server.port=8080

# 数据库配置
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

# JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
```

### 外部配置

Spring Boot 支持多种外部配置方式,例如命令行参数、环境变量、外部配置文件等。你可以通过 `@Value` 注解来注入配置值:

```java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    @Value("${app.message}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}
```

在 `application.properties` 中添加配置:

```properties
app.message=Hello, Spring Boot!
```

## 运行应用

在IDE中运行 `Application` 类,Spring Boot 将会启动嵌入式的Tomcat服务器,并监听默认的8080端口。你可以通过浏览器或工具(如Postman)访问 `http://localhost:8080/message`,你应该会看到“Hello, Spring Boot!”的响应。

通过本文,你已经学会了如何使用 Spring Initializr 创建一个 Spring Boot 项目,并进行了基础配置。Spring Boot 的自动配置和约定优于配置的原则使得开发变得更加简单和高效。随着你对 Spring Boot 的深入了解,你会发现它提供了更多的功能和模块,帮助你构建复杂的企业级应用。

希望这篇博客能帮助你快速上手 Spring Boot,并在你的开发旅程中发挥作用。祝你编程愉快!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值