小工具知识网

当前位置: 首页 > 行业知识 > SpringBoot 手写 Starter

行业知识

SpringBoot 手写 Starter

2026-11-11 11:44:09 来源:互联网转载

1.介绍

SpringBoot��的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。

优点

一般认为,SpringBoot 微框架从两个主要层面影响 Spring 社区的开发者们:

  • 基于 Spring 框架的“约定优先于配置(COC)”理念以及最佳实践之路。

  • 提供了针对日常企业应用研发各种场景的 spring-boot-starter 自动配置依赖模块,如此多“开箱即用”的依赖模块,使得开发各种场景的 Spring 应用更加快速和高效。

    ​ SpringBoot 提供的这些“开箱即用”的依赖模块都约定以 spring-boot-starter- 作为命名的前缀,并且皆位于 org.springframework.boot 包或者命名空间下(虽然 SpringBoot 的官方参考文档中提到不建议大家使用 spring-boot-starter- 来命名自己写的类似的自动配置依赖模块,但实际上,配合不同的 groupId,这不应该是什么问题)。

    2.starter自定义

    springboot 官方建议springboot官方推出的starter 以spring-boot-starter-xxx的格式来命名,第三方开发者自定义的starter则以xxxx-spring-boot-starter的规则来命名,事实上,很多开发者在自定义starter的时候往往会忽略这个东西。

    自定义一个登录拦截的启动器,authority-spring-boot-starter

    开发步骤:

    2.1新建工程

    引入以下依赖:

                              org.springframework.boot            spring-boot-autoconfigure                            org.springframework            spring-webmvc                            javax.servlet            javax.servlet-api            4.0.1            provided                        org.slf4j            slf4j-api        

    2.2创建登录拦截器

    import org.springframework.web.servlet.HandlerInterceptor;public class AuthorityInteceptor implements HandlerInterceptor {    public boolean preHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler) throws Exception {        System.out.println("-------------------同志你被拦截了 -------------");        return  true;    }}

    2.3创建属性绑定文件

    @ConfigurationProperties(prefix = "spring.auth")@Datapublic class AuthorityProperties {    /**     * 是否启用     */    boolean enabled;    /**     * 拦截的路径     */    String pathPatterns;    /**     * 不拦截的路径     */    String excludePathPatterns;}

    2.4注册拦截器

    //@EnableConfigurationProperties(AuthorityProperties.class)@Order( Ordered.HIGHEST_PRECEDENCE)@Import(AuthorityProperties.class)public class WebConfig implements WebMvcConfigurer {   @Autowired   private AuthorityProperties properties;    public void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(new AuthorityInteceptor())                .addPathPatterns(properties.getPathPatterns())             .excludePathPatterns(properties.getExcludePathPatterns());    }   }

    2.5创建自动配置类AuthorityAutoConfigruation

    @ConditionalOnProperty(        prefix = "spring.auth",        value = "enabled",        havingValue = "true",        matchIfMissing = false)@Import({WebConfig.class})public class AuthorityAutoConfigruation {    }

    2.5配置自动配置

    在resourecs文件目录下创建META-INF,并创建我们自己的spring.factories,并把我们的 MemberStaterAutoConfiguration 添加进去

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.beiyou.AuthorityAutoConfigruation

    最后打包成jar包,在我们的项目里面测试

    2.6测试

    引入依赖

                        com.beiyou            spring-test            0.0.2-SNAPSHOT        

    在application.properties配置文件中添加我们相应的配置

    spring.auth.enabled=falsespring.auth.pathPatterns=/**spring.auth.excludePathPatterns=/hello

    启动测试指定的路径是否拦截

    3.使用spring-boot-configuration-processor生产配置元数据

    3.1.引入Maven依赖

                 org.springframework.boot            spring-boot-configuration-processor            true  

    3.2.Idea配置如下(非必需)

    3.3 编译打包后,自动生成spring-configuration-metadata.json文件

    3.4 其它App应用使用时,就可以有如下提示

    4.如何把源码上传到私仓库

    4.1引入mvn依赖

                                         org.apache.maven.plugins                maven-compiler-plugin                3.8.1                                    1.8                    1.8                    UTF-8                                                        org.apache.maven.plugins                maven-source-plugin                3.2.1                                                            attach-sources                        package                                                    jar                                                                                                                                            

    4.2 打包如下图

    4.3私仓

    参考:

    https://codeup.aliyun.com/62858d45487c500c27f5aab5/202/auth-spring-boot-starter

上一篇:解决mysql报错ERROR 1049 (42000): Unknown database ‘数据库的方法

下一篇:dw怎么设置站点?