SpringBoot自定义starter 野性酷女 2022-05-25 01:28 227阅读 0赞 ## 分析 ## 自定义 `starter` 分为两个模块: * `starter` 启动器模块,空的 `jar` 模块,`pom` 引入自动配置模块。别人引用 `starter` 时只需引入 `starter`启动器模块即可 * 自动配置模块 命名规范: 后缀:`-spring-boot-starter` 范例:`mybatis-spring-boot-starter` 自定义 `starter` 分析(自动配置模块): 1. 我们的 `starter` 需要什么依赖 2. 是否需要属性配置 3. 配置类中需要提供哪些 `Bean` 交给 `Spring` 容器管理 4. `starter` 加载是否依赖某些条件 5. 提供 `META-INF/spring.factories` 文件供`@EnableAutoConfiguration`扫描 ## demo ## 1. `maven` 项目 2. 创建 `mytest-spring-boot-starter` 启动器模块,选择 `jar` 3. 创建 `mytest-spring-boot-starter-autoconfigure` 自动配置模块,选择 `jar` 4. 在 `mytest-spring-boot-starter` 的 `pom` 中引入 `mytest-spring-boot-starter-autoconfigure` <dependencies> <dependency> <groupId>spring-boot</groupId> <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> 5. 配置`mytest-spring-boot-starter-autoconfigure`的pom文件 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.4.7.RELEASE</version> </dependency> </dependencies> 6. `mytest-spring-boot-starter-autoconfigure`代码 // 属性配置,这个配置在真正的项目中,需要在 application.yml 中配置 p7.hello 的配置项 @ConfigurationProperties(prefix = "p7.hello") public class HelloProperties { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } } // 我们要让别人使用这个类 public class HelloService { private HelloProperties helloProperties; public String sayHello(String name) { return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix(); } public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } } // 配置类 @Configuration // 在 Web 环境下才被加载 @ConditionalOnWebApplication // 使配置属性立即生效 @EnableConfigurationProperties(value = HelloProperties.class) public class HelloServiceAutoConfiguration { @Autowired private HelloProperties helloProperties; @Bean public HelloService helloService() { HelloService service = new HelloService(); service.setHelloProperties(helloProperties); return service; } } // 在 src/main/resources 下创建 META-INF 文件夹,再创建 spring.factories 文件,配置我们的配置类 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.p7.autoconfigure.HelloServiceAutoConfiguration 7. 创建 `Springboot` 的 `web` 工程,在 `pom` 中引入 `starter` 启动器 <dependency> <groupId>spring-boot</groupId> <artifactId>mytest-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> 8. 在 `application.yml` 中配置 `starter` 中需要的属性 p7: hello: prefix: p7 suffix: hello 9. 创建测试 `Controller` @RestController("/hello") public class HelloController { @Autowired private HelloService helloService; @GetMapping public String hello(){ return helloService.sayHello("say"); } } 10. 启动 `web` 项目,访问 `localhost:8080/hello` // web 启动类 @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } ![这里写图片描述][70] [70]: /images/20220525/3476cede90bb44f1b72ec34533ee2841.png
还没有评论,来说两句吧...