首页 > 作文

Java SpringBoot自定义starter详解

更新时间:2023-04-04 14:01:15 阅读: 评论:0

目录
一、什么是springboot starter机制二、为什么要自定义starter?三、什么时候需要创建自定义starter?四、自定义starter的开发流程(案例:为短信发送功能创建一个starter)1、细节:命名规范2.必须引入的依赖3.编写相关属性类(xxxproperties):例如 smsproperties.java4.编写starter项目的业务功能5.编写自动配置类autoconfig6.编写spring.factories文件加载自动配置类7.打包安装8.其它项目引用总结

一、什么是springboot starter机制

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

二、为什么要自定义starter?

在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,
然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。
如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,
springboot为我们完成自动装配,简直不要太爽

三、什么时候需要创建自定义starter?

在我们的日常开发工作中,可能会需要开发一个通用模块,以供其它工程复用。springboot就为我们提供这样的功能机制,
我们可以把我们的通用模块封装成一个个starter,这样其它工程复用的时候只需要在pom中引用依赖即可,
由springboot为我们完成自动装配。

常见场景:
1.通用模块-短信发送模块
2.基于aop技术实现日志切面

3.分布式雪花id,long–>string,解决精度问题
jackson2/fastjson

4.微服务项目的数据库连接池配置
5.微服务项目的每个模块都要访问redis数据库,每个模块都要配置redistemplate
也可以通过starter解决

自动加载核心注解说明

四、自定义starter的开发流程(案例:为短信发送功能创建一个starter)

创建starter项目

starter项目和springboot工程结构没有什么区别

1、细节:命名规范

springboot官方命名方式
格式:spring-boot-starter-{模块名}
举例:spring-boot-starter-web
自定义命名方式
格式:{模块名}-spring-boot-starter
举例:mystarter-spring-boot-starter

由于的这里只做个案例给大家看,就只需要添加这几项就好了….

2.必须引入的依赖

 <!--表示两个项目之间依赖不传递;不设置optional或者optional是fal,表示传递依赖-->       <!--例如:project1依赖a.jar(optional=true),project2依赖project1,则project2不依赖a.jar-->       <dependency>           <groupid>org.springframework.boot</groupid>           <artifactid>spring-boot-configuration-processor</artifactid>           <optional>true</optional>       </dependency>

该依赖在上面像我一样选择了的,它会自动生成在pom.xml文件中

3.编写相关属性类(xxxproperties):例如 smsproperties.java

代码如下:

package com.zking.smscloudspringbootstarter.sms; import org.springframework.boot.context.properties.configurationproperties; @configurationproperties("smscloud.sms")public class smsproperties {     private string accesskeyid;//访问id、即帐号    private string accesskeycret;//访问凭证,即密码     public smsproperties() {    }     public string getaccesskeyid() {        return acsisterscesskeyid;    }     public void taccesskeyid(string accesskeyid) {        this.accesskeyid = accesskeyid;    }     public string getaccesskeycret() {        return accesskeycret;    }     public void taccesskeycret(string accesskeycret) {        this.accesskeycret = accesskeycret;    }}

@configurationproperties注解基本用法,前缀定义了哪些外部属性将绑定到类的字段上
根据 spring boot 宽松的绑定规则,类的属性名称必须与外部属性的名称匹配
我们可以简单地用一个值初始化一个字段来定义一个默认值
类本身谁家新燕啄春泥的上一句可以是包私有的
类的字段必须有公共 tter 方法

注意:smsproperties代码写完后会报如下错误,这是正常的,因为
还有配置类autoconfig和一个注解@enableconfigurationproperties没有加
not registered via @enableconfigurationproperties or marked as spring component

4.编写starter项目的业务功能

ismsrvice和smsrviceimpl

package com.zking.smscloudspringbootstarter.sms; public interface ismsrvice {     /**     * 发送短信     *     * @param phone        要发送的手机号     * @param signname     短信签名-在短信控制台中找     * @param templatecode 短信模板-在短信控制台中找     * @param data         要发送的内容     */    void nd(string phone, string signname, string templatecode, string data);}   public class smsrviceimpl implements ismsrvice{     private string accesskeyid;//访问id、即帐号    private string accesskeycret;//访问凭证,即密码     public smsrviceimpl(string accesskeyid, string accesskeycret) {        this.accesskeyid = accesskeyid;        this.accesskeycret = accesskeycret;    }     @override    public void nd(string phone,会议欢迎词 string signname, string templatecode, string data) {        system.out.println("接入短信系统,accesskeyid=" + accesskeyid + ",accesskeycret=" + accesskeycret);        system.out.println("短信发送,phone=" + phone + ",signname=" + signname + ",templatecode=" + templatecode + ",data=" + data);    }}

5.编写自动配置类autoconfig

1. @configuration:定义一个配置类
2. @enableconfigurationproperties:注解的作用是@configurationproperties注解生效。
如果只配置@configurationproperties注解,在ioc容器中是获取不到properties配置文件转化的bean的

代码如下:

package com.zking.smscloudspringbootstarter.config; import com.zking.smscloudspringbootstarter.sms.smsproperties;import com.zking.smscloudspringbootstarter.sms.smsrviceimpl;import org.springframework.boot.autoconfigure.enableautoconfiguration;import org.springframework.boot.context.properties.enableconfigurationproperties;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration; import javax.annotation.resource; @configuration //表示该类为配置类@enableconfiwindows 安全警报gurationproperties({smsproperties.class})public class smsautoconfig {    @resource    private smsproperties smsproperties;     @bean    public smsrviceimpl smsrviceimpl(){         return new smsrviceimpl(smsproperties.getaccesskeyid(),smsproperties.getaccesskeycret());    }}

6.编写spring.factories文件加载自动配置类

1.在resources下新建meta-inf文件夹,然后创建spring.factories文件
2.在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置

org.springframework.boot.autoconfigure.enableautoconfiguration=com.zking.zzcloudspringbootstarter.config.smsautoconfig

注:其中autoconfig是starter配置文件的类限定名,多个之间逗号分割,还可以\进行转义即相当于去掉后面换行和空格符号

7.打包安装

打包时需要注意一银河护卫队2百度云下,springboot项目打包的jar是可执行jar,它的类放在boot-inf目录下,如果直接作为其他项目的依赖,会找不到类。可以通过修改pom文件来解决,代码如下:

<plugin>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-maven-plugin</artifactid>         <configuration>             <classifier>exec</classifier>         </configuration>     </plugin>

然后再进行打包

打包成功后就可以再其他项目引用了,我们可以再maven仓库中看到

我们先查看下自己的maven仓库放哪了,然后去找到对应的地方

上面就是打包成功的样子

8.其它项目引用

1.首先在其他项目的pom.xnml中引入依赖

2、在application.yml文件中添加配置

然后我们可以写一个测试类来测试一下

以上就是案例的一个流程,可以看到我们是可以在其他项目中可以看到我们想要是功能的

总结

本文简单介绍了下springboot中starter机制,以及一个案例的编写。如有不足,欢迎补充

敖丙说过:你知道的越多,不知道的越多

到此这篇关于java springboot自定义starter详解的文章就介绍到这了,更多相关java springboot自定义starter内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 14:01:13,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/c976d07ac6d75d784b57e853a734aceb.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java SpringBoot自定义starter详解.doc

本文 PDF 下载地址:Java SpringBoot自定义starter详解.pdf

标签:自定义   项目   模块   注解
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图