SpringBoot自定义starter

r囧r小猫 2021-12-11 09:45 544阅读 0赞

SpringBoot自定义starter

按照一般的模式, 我们创建一个启动器, 但是该启动器只用来做依赖导入

然后创建另外一个自动配置模块, 用来定义自动配置

启动器依赖自动配置, 别人只需要引入启动器

在这里插入图片描述

在这里插入图片描述

我们可以看到, 在mybatis-spring-boot-starter中没有任何java代码,只是在pom文件中定义了依赖, 而自动配置的代码都在mybatis-spring-boot-autoconfiguration中

1. 创建自动配置模块

我们来创建一个springboot项目, pom.xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.1.6.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>com.sqt.starter</groupId>
  11. <artifactId>sqt-spring-boot-autoconfigurer</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <properties>
  14. <java.version>1.8</java.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter</artifactId>
  20. </dependency>
  21. </dependencies>
  22. </project>

我们只需要在其中引入spring-boot-starter依赖, 其他可以删除

在其中创建HelloProperties.java

  1. package com.sqt.starter;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties("sqt.hello")
  4. public class HelloProperties {
  5. private String suffix;
  6. private String prefix;
  7. public String getSuffix() {
  8. return suffix;
  9. }
  10. public void setSuffix(String suffix) {
  11. this.suffix = suffix;
  12. }
  13. public String getPrefix() {
  14. return prefix;
  15. }
  16. public void setPrefix(String prefix) {
  17. this.prefix = prefix;
  18. }
  19. }

创建HelloService.java

  1. public class HelloService {
  2. HelloProperties helloProperties;
  3. public HelloProperties getHelloProperties() {
  4. return helloProperties;
  5. }
  6. public void setHelloProperties(HelloProperties helloProperties) {
  7. this.helloProperties = helloProperties;
  8. }
  9. public String sayHello(String name){
  10. return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
  11. }
  12. }

创建自动配置类

  1. @Configuration
  2. @ConditionalOnWebApplication //只在web环境中起作用
  3. @EnableConfigurationProperties({ HelloProperties.class})
  4. public class HelloServiceAutoConfiguraion {
  5. @Autowired
  6. HelloProperties helloProperties;
  7. @Bean
  8. public HelloService helloService(){
  9. HelloService helloService = new HelloService();
  10. helloService.setHelloProperties(helloProperties);
  11. return helloService;
  12. }
  13. }

在resources目录下创建META-INF/spring.factories

  1. #定义自动配置类
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.sqt.starter.HelloServiceAutoConfiguraion

目录如下:
在这里插入图片描述

其他没用的东西可以删掉

创建starter工程

我们创建一个starter工程, 因为这个工程只用来做依赖管理和导入, 所以我们可以创建一个空的maven工程

在pom.xml文件中引入我们的自动配置工程

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.sqt.starter</groupId>
  5. <artifactId>sqt-spring-boot-starter</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>com.sqt.starter</groupId>
  10. <artifactId>sqt-spring-boot-autoconfigurer</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. </dependency>
  13. </dependencies>
  14. </project>

然后分别将我们的autoConfiguration模块和starter模块install到本地仓库

编写测试

我们创建一个springboot项目, 在其中引入我们的starter项目

  1. <dependency>
  2. <groupId>com.sqt.starter</groupId>
  3. <artifactId>sqt-spring-boot-starter</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>

在application.properties配置文件中编写配置

这个配置是在我们autoConfiguration工程中定义的

  1. sqt.hello.suffix = hello world
  2. sqt.hello.prefix = zhangsan

我们来创建一个测试类

  1. @Controller
  2. @ResponseBody
  3. public class LoginController {
  4. @Autowired
  5. HelloService helloService;
  6. @GetMapping("/login")
  7. public String login(){
  8. String s = helloService.sayHello("张三");
  9. return s;
  10. }
  11. }

启动项目

在这里插入图片描述

可以看到, 我们在autoConfiguration工程中配置的HelloService被@Autowired了进来, 而不需要我们自动配置.

发表评论

表情:
评论列表 (有 0 条评论,544人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot定义starter

    SpringBoot自定义starter 按照一般的模式, 我们创建一个启动器, 但是该启动器只用来做依赖导入 然后创建另外一个自动配置模块, 用来定义自动配置 启动