广告
返回顶部
首页 > 资讯 > 精选 >Spring Boot如何自定义starter
  • 249
分享到

Spring Boot如何自定义starter

2023-06-02 17:06:58 249人浏览 八月长安
摘要

这篇文章主要介绍Spring Boot如何自定义starter,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、简介SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我

这篇文章主要介绍Spring Boot如何自定义starter,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、简介

SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。

二、如何自定义starter

1.实例

如何编写自动配置 ?

我们参照@WEBmvcAutoConfiguration为例,我们看看们需要准备哪些东西,下面是WebMvcAutoConfiguration的部分代码:

@Configuration@ConditionalOnWebApplication@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})@AutoConfigureOrder(-2147483638)@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})public class WebMvcAutoConfiguration {    @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})    @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})    public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {        @Bean        @ConditionalOnBean({View.class})        @ConditionalOnMissingBean        public BeanNameViewResolver beanNameViewResolver() {            BeanNameViewResolver resolver = new BeanNameViewResolver();            resolver.setOrder(2147483637);            return resolver;        }    }}

我们可以抽取到我们自定义starter时同样需要的一些配置。

@Configuration  //指定这个类是一个配置类@ConditionalOnXXX  //指定条件成立的情况下自动配置类生效@AutoConfigureOrder  //指定自动配置类的顺序@Bean  //向容器中添加组件@ConfigurationProperties  //结合相关xxxProperties来绑定相关的配置@EnableConfigurationProperties  //让xxxProperties生效加入到容器中自动配置类要能加载需要将自动配置类,配置在META-INF/spring.factories中org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
模式

我们参照 spring-boot-starter 我们发现其中没有代码:

Spring Boot如何自定义starter

我们在看它的pom中的依赖中有个 springboot-starter

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

我们再看看 spring-boot-starter 有个 spring-boot-autoconfigure

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

关于web的一些自动配置都写在了这里 ,所以我们有总结

启动器starter只是用来做依赖管理需要专门写一个类似spring-boot-autoconfigure的配置模块用的时候只需要引入启动器starter,就可以使用自动配置了
命名规范

官方命名空间

  • 前缀:spring-boot-starter-

  • 模式:spring-boot-starter-模块名

  • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名空间

  • 后缀:-spring-boot-starter

  • 模式:模块-spring-boot-starter

  • 举例:mybatis-spring-boot-starter

三、自定义starter实例

我们需要先创建两个工程 hello-spring-boot-starterhello-spring-boot-starter-autoconfigurer

1. hello-spring-boot-starter

1.pom.xml
<?xml version="1.0" encoding="UTF-8"?><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">    <modelVersion>4.0.0</modelVersion>    <groupId>com.gf</groupId>    <artifactId>hello-spring-boot-starter</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>hello-spring-boot-starter</name>    <!-- 启动器 -->    <dependencies>        <!-- 引入自动配置模块 -->        <dependency>            <groupId>com.gf</groupId>            <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>            <version>0.0.1-SNAPSHOT</version>        </dependency>    </dependencies></project>

同时删除 启动类、resources下的文件,test文件。

2. hello-spring-boot-starter-autoconfigurer

1. pom.xml
<?xml version="1.0" encoding="UTF-8"?><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">    <modelVersion>4.0.0</modelVersion>    <groupId>com.gf</groupId>    <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>hello-spring-boot-starter-autoconfigurer</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.9.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!-- 引入spring-boot-starter,所有starter的基本配合 -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>    </dependencies></project>
2. HelloProperties
package com.gf.service;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "gf.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;    }}
3. HelloService
package com.gf.service;public class HelloService {    HelloProperties helloProperties;    public HelloProperties getHelloProperties() {        return helloProperties;    }    public void setHelloProperties(HelloProperties helloProperties) {        this.helloProperties = helloProperties;    }    public String sayHello(String name ) {        return helloProperties.getPrefix()+ "-" + name + helloProperties.getSuffix();    }}
4. HelloServiceAutoConfiguration
package com.gf.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@ConditionalOnWebApplication //web应该生效@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration {    @Autowired    HelloProperties helloProperties;    @Bean    public HelloService helloService() {        HelloService service = new HelloService();        service.setHelloProperties( helloProperties  );        return service;    }}
5. spring.factories

resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,内容如下:

# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.gf.service.HelloServiceAutoConfiguration

到这儿,我们的配置自定义的starter就写完了 ,我们hello-spring-boot-starter-autoconfigurer、hello-spring-boot-starter 安装成本地jar包。

三、测试自定义starter

我们创建个项目 hello-spring-boot-starter-test,来测试系我们写的stater。

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?><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">    <modelVersion>4.0.0</modelVersion>    <groupId>com.gf</groupId>    <artifactId>hello-spring-boot-starter-test</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>hello-spring-boot-starter-test</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.9.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!-- 引入自定义starter -->        <dependency>            <groupId>com.gf</groupId>            <artifactId>hello-spring-boot-starter</artifactId>            <version>0.0.1-SNAPSHOT</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

2. HelloController

package com.gf.controller;import com.gf.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @Autowired    HelloService helloService;    @GetMapping("/hello/{name}")    public String hello(@PathVariable(value = "name") String name) {        return helloService.sayHello( name + " , " );    }}

3. application.properties

gf.hello.prefix = higf.hello.suffix = what's up man ?

我运行项目访问 http://127.0.0.1:8080/hello/zhangsan,结果如下:

hi-zhangsan , what's up man ?

以上是“Spring Boot如何自定义starter”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!

--结束END--

本文标题: Spring Boot如何自定义starter

本文链接: https://www.lsjlt.com/news/230771.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring Boot如何自定义starter
    这篇文章主要介绍Spring Boot如何自定义starter,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、简介SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我...
    99+
    2023-06-02
  • spring boot 自定义starter的实现教程
    spring boot 使用 starter 解决了很多配置问题, 但是, 他是怎么来解决这些问题的呢?这里通过一个简单的例子, 来看一下, starter是怎么来设置默认配置的.一. 建 starter 项目自定义的starter...
    99+
    2023-05-30
    spring boot 自定义
  • Spring boot 自定义 Starter及自动配置的方法
    目录Starter 组件简介自定义 Starter 组件Starter 组件使用 StarterStarter 传参自身与第三方维护Starter 组件简介 Starter 组件是 ...
    99+
    2022-12-08
    Spring boot 自定义 Starter Spring boot自动配置
  • 如何定制标准Spring Boot starter
    这篇文章主要介绍“如何定制标准Spring Boot starter”,在日常操作中,相信很多人在如何定制标准Spring Boot starter问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何定制标准S...
    99+
    2023-06-16
  • Spring Boot自定义Starter组件开发实现配置过程
    目录自定义starter为什么要自定义starter自定义starter的命名规则实现方法引入依赖编写测试类创建配置类创建spring.factories文件乱码问题解决方案:1. ...
    99+
    2022-11-13
  • SpringBoot如何自定义starter
    目录1. 什么是starter2. 自动配置原理2.1 自动配置生效3. 自定义starter3.1 命名规范4.总结4.1为什么要自定义starter4.2 自定义starter的...
    99+
    2022-11-12
  • Spring Boot自定义 Starter并推送到远端公服的详细代码
    目录一、新建项目,完善pom文件二、编写业务逻辑三、编写自动配置类AutoConfig 四、编写spring.factories文件加载自动配置类五、maven打包六、推送...
    99+
    2022-11-13
  • Spring Boot中如何使用Starter
    本篇内容主要讲解“Spring Boot中如何使用Starter”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Boot中如何使用Starter”吧!SpringBoot简介Spri...
    99+
    2023-06-16
  • 如何使用SpringBoot自定义starter
    这篇文章主要介绍了如何使用SpringBoot自定义starter,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。springboot是什么springboot一种全新的编程规...
    99+
    2023-06-14
  • 详解SpringBoot如何自定义Starter
    目录阅读收获本章源码下载什么是Starter为什么使用StarterSpringboot自动配置spring.factoriesStarter开发常用注解Full全模式和Lite轻量...
    99+
    2022-11-12
  • spring boot如何实现自定义配置源
    这篇文章给大家分享的是有关spring boot如何实现自定义配置源的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。概述我们知道,在Spring boot中可以通过xml或者@ImportResource 来引入自...
    99+
    2023-05-30
    springboot
  • JavaSpringBoot自定义starter详解
    目录一、什么是SpringBoot starter机制二、为什么要自定义starter ?三、什么时候需要创建自定义starter?四、自定义starter的开发流程(案例...
    99+
    2022-11-12
  • SpringBoot怎么自定义Starter
    这篇文章给大家分享的是有关SpringBoot怎么自定义Starter的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。什么是StarterStarter是Spring Boot中的一个非常重要的概念,Starter...
    99+
    2023-06-22
  • spring boot装载自定义yml文件
    yml格式的配置文件感觉很人性化,所以想把项目中的.properties都替换成.yml文件,主要springboot自1.5以后就把@configurationProperties中的location参数去掉,各种查询之后发现可以用Yam...
    99+
    2023-05-31
    spring boot yml
  • Spring Boot之FilterRegistrationBean-自定义Filter详解
    Spring Boot之FilterRegistrationBean-自定义Filter 项目老的用spring写的,新的升级到了springboot,原代码中有在web.xml中定...
    99+
    2022-11-12
  • 详解Spring Boot中如何自定义SpringMVC配置
    目录前言一、SpringBoot 中 SpringMVC 配置概述二、WebMvcConfigurerAdapter 抽象类三、WebMvcConfigurer 接口四、WebMvc...
    99+
    2022-11-12
  • 使用spring-boot如何实现整合dubbo中的Spring-boot-dubbo-starter
    使用spring-boot如何实现整合dubbo中的Spring-boot-dubbo-starter?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。在application.p...
    99+
    2023-05-31
    springboot art dubbo
  • SpringBoot项目为何引入大量的starter?如何自定义starter?
    目录1 前言2 @EnableConfigurationProperties实现自动装配2.1 创建一个starter项目2.2 创建一个需要自动装配的Bean2.3 自动装配类实现...
    99+
    2022-11-13
  • 在Spring Boot项目中如何实现自定义PropertySourceLoader
    今天就跟大家聊聊有关在Spring Boot项目中如何实现自定义PropertySourceLoader,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。SpringBoot 的配置文件...
    99+
    2023-05-31
    propertysourceloader springboot ce
  • Spring Boot下如何自定义Repository中的DAO方法
     环境配置介绍jdk 1.8, spring Boot 1.5.3.RELEASE, MySQL, Spring Data, JPA问题描述Spring Data提供了一套简单易用的DAO层抽象与封装,覆盖的CURD的基本功能,但...
    99+
    2023-05-31
    spring boot repository
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作