SpringBoot自定义starter

SpringBoot自定义starter

在这里插入图片描述

源码地址 : https://github.com/YuSheng1223/SpringBoot

1.为什么要自定义starter?

SpringBoot提供的自动配置功能为我们构建工程和开发程序节省了大量的时间。作为一个码农,光会用可是不够的。这里我们尝试着自己去定义一个starter,在平时工作中,一个自定义的starter可以帮我们完成一些SpringBoot不支持组件的初始化工作,并且理解原理也能让我们面试大大加分。

2.如何自定义starter?

1.新建SpringBoot项目

这里就不放图了,按照我们平时新建工程的步骤来就可以了。关于命名尽量遵循格式。

自定义starter命名方式:

  • 官方 spring-boot-starter-模块名
  • 非官方(如我们自己编写的) 模块名-spring-boot-starter

2.pom中引入自动配置jar包

首先需要在pom文件中引入我们需要的jar包,如果没有其他需要,引入spring-boot-autoconfigure ,spring-boot-starter-web 这两个就足够了。

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
  </dependency>
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

将其他多余的jar包删除,不仅占用空间,而且还有可能引起错误。

spring-boot-maven-plugin 这个插件是一定要移除的。因为当我们打包的时候,MAVEN会将编译完的class文件放到BOOT-INF/classes/ 路径下,从而导致引用starter的工程无法从jar包的根路径下找到需要自动配置的类,程序启动失败。

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3.加载配置文件内容的类

这里需要写一个在配置文件中加载属性的类,从而让我们可以显式的进行配置。

import org.springframework.boot.context.properties.ConfigurationProperties;
//指定从配置文件中加载属性的前缀
@ConfigurationProperties(prefix = "print")
public class PrintProperties {

    private String text;

    public void setText(String text){
        this.text = text;
    }

    public String getText(){
        return this.text;
    }
}

4.要自动配置的类

public class PrintUtils {

    private static final Logger logger = LoggerFactory.getLogger(PrintUtils.class);

    private final String text;


    public PrintUtils(String text){
        this.text = text;
    }
    public void printText(){
        logger.info(" starter print text : {} ",text);
    }


}

这个类就是我们要进行自动配置的类,类似于RedisTemplate、JdbcTemplate等。如果想要使用,从spring容器中直接获取就可以了。

5.完成自动配置初始化的类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//开启自动配置
@EnableConfigurationProperties({PrintProperties.class})
public class PrintAutoConfiguration {

    @Autowired
    private PrintProperties printProperties;
	
    @Bean
    @ConditionalOnProperty(prefix = "print.config", name = "enable", havingValue = "true")
    public PrintUtils InitializePrintUtils(){
        return new PrintUtils(this.printProperties.getText());
    }


}

上面的 @ConditionalOnProperty,使得如果想要使用此starter,必须显式的指定print.config.enable=true才可以。

这个类开启了PrintProperties类的属性加载功能,从PrintProperties类中获取属性,将属性配置到PrintUtils类,并且将PrintUtils类注入到spring容器中。

6.让其他工程扫描到此starter的自动配置

新建META-INF/spring.factories文件,并且在spring.factories文件中指定需要开启自动配置的类。

注意spring.factories文件的位置。

spring.factories文件中的内容

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xs.starter.PrintAutoConfiguration

7.打包

这一步就不多说了,我们将此工程打成jar包就可以了。

8.在其他工程中引用starter

现在就可以正式使用我们自定义的starter了。首先引入自定义starter的jar包。

  <dependency>
            <groupId>com.xs</groupId>
            <artifactId>spring-boot-custom-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
  </dependency>

在yaml或者properties文件中进行配置:

print:
  config:
    enable: true //开启配置
  text: "海上月是天上月"  //要打印的文本

在代码中使用“

@Slf4j
@Controller
public class HelloController {
   //从容器中拿出我们注入的PrintUtils
    @Autowired
    PrintUtils printUtils;

    @RequestMapping("/hello03")
    public void testStarter(){
        printUtils.printText();
    }
}

请求/hello03,就可以从控制台看到打印了 starter print text : 海上月是天上月 ,说明我们自定义的starter生效了。

技术交流、技术文章请关注微信公众号: java晋升

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值