SpringBoot

发布于:2025-02-10 ⋅ 阅读:(55) ⋅ 点赞:(0)

目录

“Spring程序和SpringBoot程序的主要区别“”

“如何用SpringBoot整合SSM”

一、SpringBoot

1.1 SpringBoot概述

1.1.1 起步依赖

1.1.2 SpringBoot引导类

1.1.3 内置服务器

二、配置文件

2.1 格式

2.2 优先级

2.3 YAML文件的语法规则

2.4 读取配置数据

2.4.1 使用 @Value注解

2.4.2 Environment对象

2.4.3 自定义对象

2.5 多环境配置

2.5.1 yaml文件

2.5.2 properties文件

2.5.3 命令行启动参数设置

2.6 配置文件的加载位置及优先级


“Spring程序和SpringBoot程序的主要区别“”

  1. 依赖管理(坐标)

    • 在传统的Spring程序中,开发者需要自己手动编写Maven或者Gradle坐标,常常需要引入多个依赖,还需要注意版本兼容问题,非常繁琐。
    • 而在Spring Boot中,坐标可以通过Starter简化。创建工程时可以通过工具(如Spring Initializr)勾选功能,自动生成所需的依赖,且版本由Spring Boot Parent统一管理,减少了开发者的配置工作量。
  2. Web配置类

    • 在Spring程序中,开发者需要手动书写Web配置类,配置复杂,如配置ViewResolver(视图解析器)、HandlerMapping(处理器映射)、静态资源处理等,配置步骤多且容易出错。
    • 在Spring Boot中,框架自动提供了默认的Web配置(如Spring MVC的默认配置)。这些配置开箱即用,开发者只需根据需要进行少量修改或扩展,大幅减少了代码量。
  3. Spring/Spring MVC配置类

    • 传统Spring或Spring MVC程序中,开发者需要手动编写各种配置类,例如数据源配置类、事务管理配置类、组件扫描配置等。这些配置非常繁琐,且需要开发者对Spring有深刻的了解。
    • 在Spring Boot中,大多数配置由框架自动完成,开发者无需手动书写。例如,Spring Boot根据application.propertiesapplication.yml中的内容自动完成数据源、事务等配置。

“如何用SpringBoot整合SSM”

  1. 引入依赖:

    • 在 pom.xml 中添加 SpringBoot 的 starter 依赖,比如 spring-boot-starter-web 和 mybatis-spring-boot-starter,同时引入数据库驱动(如 MySQL)。
  2. 配置文件:

    • 使用 application.yml 或 application.properties 配置数据库连接信息,比如 URL、用户名、密码,以及 MyBatis 的相关配置(如 Mapper 路径)。
  3. 整合 MyBatis:

    • 编写实体类(Entity),定义 Mapper 接口和 XML 映射文件(如果需要)。
    • 确保 MyBatis 的 Mapper 被扫描到,通常使用 @MapperScan 注解。
  4. 开发业务逻辑:

    • 实现 Service 层,用于封装业务逻辑。
    • Controller 层接收请求,通过 Service 调用 Mapper,返回数据。
  5. 启动和测试:

    • 使用 SpringBoot 引导类 SpringApplication.run() 启动内置 Tomcat,访问接口验证是否整合成功。

一、SpringBoot

1.1 SpringBoot概述

1.1.1 起步依赖

 起步依赖封装了常用技术的所有坐标,只需引入简单的依赖,无需手动逐个配置复杂的依赖。

常见起步依赖:

spring-boot-starter-web:包含 Spring Web、SpringMVC 和内置 Tomcat。

spring-boot-starter-data-jpa:用于整合 JPA。

spring-boot-starter-test:包含单元测试相关工具(如 JUnit 和 Mock)。

父工程中通过dependencyManagement锁定了常用依赖的版本号,开发者只需指定groupIdartifactId即可使用默认版本。

1.1.2 SpringBoot引导类

引导类是 SpringBoot 项目的启动入口,用于启动 Spring 容器和内置服务器。

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

@SpringBootApplication:是一个组合注解,整合了@Configuration@EnableAutoConfiguration@ComponentScan

1.1.3 内置服务器

  1. 默认服务器: SpringBoot 默认使用 Tomcat 作为内置服务器。
    • 运行引导类时,Tomcat 会自动启动,无需手动配置。
  2. 切换服务器:
    • 可以通过依赖管理的方式切换为其他服务器,如 Jetty。

 步骤:

  1. pom.xml中排除 Tomcat 依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

   2. 添加 Jetty 依赖:

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

 启动日志中会显示 Jetty 服务器。

二、配置文件

2.1 格式

SpringBoot 程序的配置文件名必须是 application ,只是后缀名不同而已。

SpringBoot 支持以下三种核心配置文件格式:

application.properties

  • 最传统的配置文件格式,按键值对书写,如:
server.port=80

application.yml

  • YAML 格式的配置文件,层次结构清晰,可读性更强,如:
server:
  port: 80

 application.yaml

  • 与 .yml 格式相同,只是后缀不同,使用较少。

2.2 优先级

当同时存在多种格式的配置文件时,优先级如下:

application.properties > application.yml > application.yaml

2.3 YAML文件的语法规则

  1. 大小写敏感: 属性名称区分大小写。
  2. 层次结构: 使用缩进表示层级关系,缩进必须使用空格,不能使用 Tab 键。
  3. 数组: 使用 - 表示数组元素。
  4. 注释: 使用 #

2.4 读取配置数据

2.4.1 使用 @Value注解

@RestController
@RequestMapping("/books")
public class BookController {
    
    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_00;
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_00);
        return "hello , spring boot!";
   }
}

2.4.2 Environment对象

在开发中我们很少使用。因为它获取配置数据的方式较为零散,每次都需要通过 key 手动查询配置,代码不够简洁和规范。

上面方式读取到的数据特别零散, SpringBoot 还可以使用 @Autowired 注解注入 Environment 对象的方式读取数据。这 种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用 Environment 对象的 getProperty(String name) 方法获取。具体代码如下:

@RestController
@RequestMapping("/books")
public class BookController {
    
    @Autowired
    private Environment env;
    
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(env.getProperty("lesson"));
        System.out.println(env.getProperty("enterprise.name"));
        System.out.println(env.getProperty("enterprise.subject[0]"));
        return "hello , spring boot!";
   }
}

2.4.3 自定义对象

配置文件数据可以封装到自定义的实体类中。

步骤

(1)创建实体类,并添加 @Component 和 @ConfigurationProperties 注解。

(2)在控制器中注入实体类,直接访问属性。

enterprise:
  name: itcast
  age: 16
  tel: 4006184000
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;

    // Getter and Setter
}

@RestController
public class BookController {
    @Autowired
    private Enterprise enterprise;

    @GetMapping("/enterprise")
    public String getEnterprise() {
        return enterprise.toString();
    }
}

2.5 多环境配置

2.5.1 yaml文件

为什么需要多环境配置

  • 不同环境(开发、测试、生产)下的配置项不同,例如数据库连接、端口号等。
  • 手动切换配置文件容易出错,SpringBoot 提供了多环境支持。

在 application.yml 中使用 --- 来分割不同的配置

#生产
spring:
 profiles: pro #给生产环境起的名字
server:
 port: 81
---
#测试
spring:
 profiles: test #给测试环境起的名字
server:
 port: 82
---

 spring.profiles 是用来给不同的配置起名字的。

设置启用环境

#设置启用的环境
spring:
 profiles:
   active: dev  #表示使用的是开发环境的配置

 application.yml 配置文件内容如下

#设置启用的环境
spring:
 profiles:
   active: dev
---
#开发
spring:
 profiles: dev
server:
 port: 80
---
#生产
spring:
 profiles: pro
server:
 port: 81
---
#测试
spring:
 profiles: test
server:
 port: 82
---

spring.profiles 配置项已经过时。最新用来起名字的配置项是

#开发
spring:
 config:
   activate:
     on-profile: dev

2.5.2 properties文件

  • 创建多个配置文件:

    • application-dev.properties:开发环境配置。
    • application-test.properties:测试环境配置。
    • application-prod.properties:生产环境配置。
  • application.properties 中设置启用的环境:

spring.profiles.active=dev

2.5.3 命令行启动参数设置

SpringBoot 提供了在运行 jar 时设置开启指定的环境的方式

java –jar xxx.jar –-spring.profiles.active=test

临时修改端口号

java –jar xxx.jar –-server.port=88

 同时设置多个配置,比如即指定启用哪个环境配置,又临时指定端口

java –jar springboot.jar –-server.port=88 –-spring.profiles.active=test

 命令行设置的端口号优先级高

2.6 配置文件的加载位置及优先级

SpringBoot 可以从多个位置加载配置文件,优先级从高到低:

  1. file:config/application.yml(外部目录中的 config 文件夹)。
  2. file:application.yml(外部目录)。
  3. classpath:config/application.yml(项目内部 resources 下的 config 文件夹)。
  4. classpath:application.yml(项目内部 resources 根目录)。

网站公告

今日签到

点亮在社区的每一天
去签到