Springboot3
- Java17以上
1.依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/>
</parent>
2.新特性
2.1 Jakarta EE
JavaEE更改为Jakarta EE,javax包名对应变为Jakarta
2.2 框架升级
spring框架变成spring6
2.3 支持 GraalVM 原生镜像
2.4 提高应用可观察性
3.第三方集成
3.1 MybatisPlus
<!--mybatisPlus
springBoot3 使用spring6 删除 NestedIOException
mybatisPlus version >= 3.5.3-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
- 分页配置
更新后直接使用Page分页会不成功,需要加入配置
@Configuration
//mybatis扫包
@MapperScan("org.example.dao")
public class MybatisPlusConfig {
/**
* 分页插件 3.5.X
*/
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInterceptor.setMaxLimit(-1L);
paginationInterceptor.setDbType(DbType.MYSQL);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setOptimizeJoin(true);
return paginationInterceptor;
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.setInterceptors(Collections.singletonList(paginationInnerInterceptor()));
return mybatisPlusInterceptor;
}
}
出现问题:
Correct the classpath of your application so that it contains a single, compatible version of net.sf.jsqlparser.schema.Column
修改依赖
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
<exclusions>
<exclusion>
<artifactId>jsqlparser</artifactId>
<groupId>com.github.jsqlparser</groupId>
</exclusion>
</exclusions>
</dependency>
3.2 Swagger3
- 只能使用SpringDoc
<!--swagger springdoc-->
<!-- springboot3用不了-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-boot-starter</artifactId>-->
<!-- <version>3.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<version>2.0.2</version>
</dependency>
- SwaggerConfig
@Configuration
public class SpringDocConfig {
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(info())
/*添加对JWT对token的支持(本步骤可选) 在添加OpenApiConfig类上添加Components信息:然后在OpenApi中注册Components:*/
.components(components())
.externalDocs(externalDocumentation());
}
private License license() {
return new License()
.name("MIT")
.url("https://opensource.org/licenses/MIT");
}
private Info info() {
return new Info()
.title("Open API")
.description("")
.version("v0.0.1")
.license(license());
}
private ExternalDocumentation externalDocumentation() {
return new ExternalDocumentation()
.description("")
.url("");
}
/* 添加对JWT对token的支持(本步骤可选)
在添加OpenApiConfig类上添加Components信息:*/
private Components components() {
return new Components()
.addSecuritySchemes("bearer-key", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT"));
}
/*然后在OpenApi中注册Components:*/
/* @Bean
public OpenAPI springShopOpenAPI2() {
return new OpenAPI()
.info(info())
.components(components())
.externalDocs(externalDocumentation());
}
*/
// /*在需要使用Authorization的接口上添加:*/
// @Operation(security = { @SecurityRequirement(name = "bearer-key") })
}
- 注解变化
@Api → @Tag(name = "Controller", description = "接口")
@ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
@ApiImplicitParam → @Parameter
@ApiImplicitParams → @Parameters
@ApiModel → @Schema
@ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
@ApiModelProperty → @Schema
@ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
@ApiParam → @Parameter
@ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")
4.跨域新配置
将.allowedOrigins替换成.allowedOriginPatterns即可。
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
CorsConfiguration config = new CorsConfiguration();
// 允许cookies跨域
config.setAllowCredentials(true);
// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
config.addAllowedOriginPattern("*");
// #允许访问的头信息,*表示全部
config.addAllowedHeader("*");
// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
config.setMaxAge(18000L);
// 允许提交请求的方法,*表示全部允许
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
5.依赖注入
5.1 基于属性的注入
@Autowired
5.2 setter方法的注入
public void setTaskGroupTemplateRepository(TaskGroupTemplateRepository taskGroupTemplateRepository,TaskGroupService taskGroupService){
ExcelListener2.taskGroupTemplateRepository = taskGroupTemplateRepository;
ExcelListener2.taskGroupService = taskGroupService;
}
5.3 构造方法的注入
@Autowired
public ExcelListener(@Qualifier("taskGroupService") TaskGroupService taskGroupService) {
this.taskGroupService = taskGroupService;
}
强制依赖就用构造器方式 可选、可变的依赖就用setter注入
Spring 团队提倡使用基于构造方法的注入,因为这样一方面可以将依赖注入到一个不可变的变量中 (注:final 修饰的变量),另一方面也可以保证这些变量的值不会是 null。
此外,经过构造方法完成依赖注入的组件 (注:比如各个 service),在被调用时可以保证它们都完全准备好了。与此同时,从代码质量的角度来看,一个巨大的构造方法通常代表着出现了代码结构问题,这个类可能承担了过多的责任。
基于 setter 的注入,则只应该被用于注入非必需的依赖,同时在类中应该对这个依赖提供一个合理的默认值。如果使用 setter 注入必需的依赖,那么将会有过多的 null 检查充斥在代码中。使用 setter 注入的一个优点是,这个依赖可以很方便的被改变或者重新注入。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-10-04 自定义组件
2022-10-04 OFF12 二维数组路径
2022-10-04 OFF14
2022-10-04 快速幂