前言:什么是Starter POMs
Starter POMs是预配置的依赖集合,旨在提供一种快速的方式来引入和管理Spring及相关技术栈的依赖。每个Starter POM都是针对特定的Spring模块或技术场景设计的。使用Starter POM,开发者只需要添加一个依赖项,就能拉取所有必需的库。
核心优势:
- 简化依赖管理:避免了手动指定和管理多个相关依赖的版本。
- 提升开发效率:使开发者能够快速启动新项目,专注于业务逻辑而非配置细节。
- 保证兼容性:Spring团队维护的Starter POMs确保依赖之间的版本兼容。
本文重点:创建自定义Starter
首先我们先自定义一个starter,并且添加自定义配置
项目结构如下:
package com.nextjava.customexceptionhandlerstarter;
// 包名已省略请自行导入。。。
@Configuration
@ConditionalOnWebApplication // 仅在Web应用中激活
@EnableConfigurationProperties(CustomExceptionHandlerProperties.class)
public class CustomExceptionHandlerAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GlobalExceptionHandler globalExceptionHandler() {
return new GlobalExceptionHandler();
}
// 全局异常处理器
@ControllerAdvice
public static class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<Object> handleException(Exception e) {
// 处理异常,返回自定义响应
return new ResponseEntity<>("出现异常: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
package com.nextjava.customexceptionhandlerstarter;
// 包名已省略请自行导入。。。
@ConfigurationProperties(prefix = "custom.exception.handler")
public class CustomExceptionHandlerProperties {
// 在这里定义配置属性,例如启用或禁用异常处理器
private boolean enabled = true; // 默认启用
// Getter 和 Setter
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
package com.nextjava.customexceptionhandlerstarter;
// 包名已省略请自行导入。。。
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
在resources/META-INF目录下创建org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,里面内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nextjava.customexceptionhandlerstarter.CustomExceptionHandlerAutoConfiguration
上面自定义starter就完成了,下面看如何使用:
步骤一:导入starter
创建一个新的工程,导入自定义starter,我们现在就以demo4为例:
首先我们在pom中加入刚才自定义的starter,如何加入呢?就是我们先把之前的starter使用mvn clear install 一下推送到Maven仓库中,或者直接使用IDEA中的Maven插件直接操作
这样我们就可以引入对应的依赖文件
<dependency>
<groupId>com.nextjava</groupId>
<artifactId>custom-exception-handler-starter</artifactId>
<version>0.1.0</version>
</dependency>
步骤二:创建一个controller,测试是否生效
代码如下:
package com.nextjava.demo04.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
throw new NumberFormatException("test");
}
}
package com.nextjava.demo04;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// 若自定义starter没有和本应用处在同一个包名中,需要使用scan进行二次扫描
@SpringBootApplication(scanBasePackages = "com.nextjava")
public class Demo04Application {
public static void main(String[] args) {
SpringApplication.run(Demo04Application.class, args);
}
}
最重要的是我们自定义配置属性的开启:
在application.properties中加入
custom.exception.handler=true
现在我们启动服务
得到如下结果
这样我们这次的学习内容就结束了,大家可以自己多多练习
结束语
本文中的自定义starter 可以使我们最大能力进行自由的开发插件,通过理解和利用自动配置starter,开发者可以更有效地使用Spring Boot,并根据需要定制和扩展自动配置。下一章节我们将开始学习如何SpringBoot如何和关系型数据库的整合,敬请期待