游侠的博客 游侠的博客
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ranger

一名在校研究生
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Vue

  • SpringBoot2

    • 各种小知识
    • 一、SpringBoot基础入门
    • 二、SpringBoot的底层注解
    • 三、SpringBoot的自动配置
    • 四、SpringBoot的最佳实践
      • 16、最佳实践-SpringBoot应用如何编写
      • 17、最佳实践-Lombok简化开发
      • 18、最佳实践-dev-tools
      • 19、最佳实践-Spring Initailizr
        • 自动依赖引入
        • 自动创建项目结构
        • 自动编写好主配置类
    • 五、SpringBoot的配置文件
    • 六、SpringBoot的Web场景
    • 七、SpringBoot的请求处理(源码分析)
  • JavaWeb

  • SSM

  • SpringBoot3

  • 技术
  • SpringBoot2
yangzhixuan
2023-05-10
目录

四、SpringBoot的最佳实践

# 16、最佳实践-SpringBoot应用如何编写

  • 引入场景依赖

    • 官方文档 (opens new window)
  • 查看自动配置了哪些(选做)

    • 自己分析,引入场景对应的自动配置一般都生效了

    • 配置文件中debug=true开启自动配置报告。会打印下列信息

      Positive matches:
      -----------------
      
         AopAutoConfiguration matched:
            - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
      
         AopAutoConfiguration.ClassProxyingConfiguration matched:
            - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
            - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
      
         DispatcherServletAutoConfiguration matched:
            - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
            - found 'session' scope (OnWebApplicationCondition)
      
         DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
            - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration' (OnClassCondition)
            - Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
      .......
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      • Negative(不生效)
      • Positive(生效)
  • 是否需要修改

    • 参照文档修改配置项
      • 官方文档 (opens new window)
      • 自己分析。xxxxProperties绑定了配置文件的哪些。
    • 自定义加入或者替换组件
      • @Bean、@Component...
    • 自定义器 XXXXXCustomizer;
    • ......

# 17、最佳实践-Lombok简化开发

Lombok用标签方式代替构造器、getter/setter、toString()等鸡肋代码。

spring boot已经管理Lombok。引入依赖:

 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
</dependency>
1
2
3
4

IDEA中File->Settings->Plugins,搜索安装Lombok插件。

@NoArgsConstructor // 无参构造器
// @AllArgsConstructor // 全参构造器
@Data
@ToString
@EqualsAndHashCode
public class User {

    private String name;
    private Integer age;

    private Pet pet;

    public User(String name,Integer age){
        this.name = name;
        this.age = age;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

简化日志开发

@Slf4j
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(@RequestParam("name") String name){
        log.info("请求进来了....");
        return "Hello, Spring Boot 2!"+"你好:"+name;
    }
}
1
2
3
4
5
6
7
8
9

# 18、最佳实践-dev-tools

Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools module can be included in any project to provide additional development-time features.——link (opens new window)

Applications that use spring-boot-devtools automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE, as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a directory is monitored for changes. Note that certain resources, such as static assets and view templates, do not need to restart the application (opens new window).——link (opens new window)

Triggering a restart

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using:

  • In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.
  • In IntelliJ IDEA, building the project (Build -> Build Project)(shortcut: Ctrl+F9) has the same effect.

令代码修改快速生效,其实只是重启项目,并不是热更新,若只是更新了静态页面也不用重启

添加依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
1
2
3
4
5
6
7

在IDEA中,项目或者页面修改以后:Ctrl+F9。

# 19、最佳实践-Spring Initailizr

Spring Initailizr (opens new window)是创建Spring Boot工程向导。

在IDEA中,菜单栏New -> Project -> Spring Initailizr。

会自动帮我们创建主程序类以及项目结构,自动引入所需依赖

# 自动依赖引入

image

# 自动创建项目结构

image

# 自动编写好主配置类

image

编辑 (opens new window)
上次更新: 2024/05/30, 07:49:34
三、SpringBoot的自动配置
五、SpringBoot的配置文件

← 三、SpringBoot的自动配置 五、SpringBoot的配置文件→

最近更新
01
tensor比较大小函数
05-30
02
Large Language Models can Deliver Accurate and Interpretable Time Series Anomaly Detection
05-27
03
半监督学习经典方法 Π-model、Mean Teacher
04-10
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Ranger | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式