鞍山做网站企业excel做网站二维码

当前位置: 首页 > news >正文

鞍山做网站企业,excel做网站二维码,做外贸网站注册什么邮箱,承德市信息查询平台starter项目 先来看一下Starter的官方解释#xff1a; Spring Boot Starter 是一种方便的依赖管理方式#xff0c;它封装了特定功能或技术栈的所有必要依赖项和配置#xff0c;使得开发者可以快速地将这些功能集成到Spring Boot项目中。Spring Boot官方提供了一系列的Star…starter项目 先来看一下Starter的官方解释  Spring Boot Starter 是一种方便的依赖管理方式它封装了特定功能或技术栈的所有必要依赖项和配置使得开发者可以快速地将这些功能集成到Spring Boot项目中。Spring Boot官方提供了一系列的Starters涵盖了从Web开发到安全、数据访问等多个方面 其实Starter是一种SDK思想基于SDK高度抽象快速构建功能块或技术块是当下企业技术部门实现技术资产快速开发的利器基于Spring的starter自然就是最高效的方法。 starter项目结构分析 我们先来看一下mybatis-spring-boot-starter的项目结构 抛开mybatis的jdbc等逐步演化的核心功能依赖只专注我们想要参观的starter部分可以看到mybatis-spring-boot-starter被引入后是以两个jar的形式存在即 mybatis-spring-boot-autoconfiguremybatis-spring-boot-starter  其中mybatis-spring-boot-starter 项目中并无代码仅仅一个pom文件指明依赖 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot/artifactIdversion2.1.4/version/parentartifactIdmybatis-spring-boot-starter/artifactIdnamemybatis-spring-boot-starter/namepropertiesmodule.nameorg.mybatis.spring.boot.starter/module.name/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency!–mybatis-starter的代码项目–dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-autoconfigure/artifactId/dependency!– 支撑mybatis-starter的其他依赖–dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactId/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactId/dependency/dependencies /project 也就是说 mybatis-starter与SpringBoot对接的主要逻辑集中在mybatis-spring-boot-autoconfigure 中mybatis-spring-boot-starter  仅仅是个壳子它统一管理mybatis在适配SpringBoot中的资源依赖统一管理。 根据mybatis-starter以及其他标准的start开发、SpringBoot的官方资料我们进行自定义一个starter的形式应该是以maven的多module形式创建一个项目该项目下包含两个module一个是XXX-spring-boot-starter另一个是XXX-spring-boot-autoconfigure且XXX-spring-boot-starter 仅仅是一个只包含pom依赖的的空白项目。 编写一个自定义的starter包的流程 在之前的SpringBoot中通过自定义注解使用AOP里我们使用AOP实现了日志监听这里还是以这个为例子将它改造为一个开箱即用的starter。 第一步创建module 根据SpringBoot的官方结构创建module项目lognote-spring-boot project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdlognote-spring-boot/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmodulelognot-spring-boot-starter/modulemodulelognote-spring-boot-autoconfgure/module/modulespropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project lognote-spring-boot下分别包含两个module作为公共对外的lognote-spring-boot-starter和与SpringBoot框架完成自动注入对接的lognote-spring-boot-autoconfigure  第二步管理依赖 根据上述分析mybatis的例子对starter子module的pom进行设置让其依赖autoconfigure以下是lognote-spring-boot-starter的pom文件依赖 dependenciesdependencygroupIdorg.example/groupIdartifactIdlognote-spring-boot-autoconfgure/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies 对autoconfigure项目进行所需依赖引入引入对接SpringBoot的核心依赖autoconfigure和一些常用的依赖以下是lognote-spring-boot-autoconfigure的pom文件  dependencies!– starter包必须引入该依赖完成对SpringBoot的适配 –dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactIdversion2.3.6.RELEASE/version/dependency!–其他相关依赖–dependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.30/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.12/version/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.7.8/version/dependency/dependencies 第三步autoconfigure项目中增加配置文件 剩下的相关功能及相关配置代码均在lognote-spring-boot-autoconfigure项目中完成 使用新建基础配置启动类 Configuration ComponentScan(basePackages org.example.lognote.*) public class ApplicationConfiguration { } 配置spring.factories文件 在resources目录下新建META-INFO目录新建spring.factories文件文件中指定lognote-spring-boot-autoconfigure项目的启动类路径即可 org.springframework.boot.autoconfigure.EnableAutoConfigurationorg.example.lognote.ApplicationConfiguration 配置yml文件及相关配置内容可选 可根据组件情况是否进行yml相关配置这里进行是否启用日志记录的开关配置需要在yml中增加配置参数 #yml文件下新增配置默认为关闭 lognote:enable: false 当被其他SpringBoot项目引用时可在其yml文件中进行配置即可。 创建参数的实体类 ConfigurationProperties(prefix lognote) Data Configuration public class LogNoteProperties {private Boolean enable false; } 创建判定条件逻辑类,可基于配置内容决定是否注入或者使用内部相关功能在下面具体代码中会使用该条件判定 public class LogNoteCondition implements Condition {Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {String able conditionContext.getEnvironment().getProperty(lognote.enable);if(null! able Boolean.parseBoolean(able) ){return true;}return false;} 这里提供的是基于实现条件接口进行自定义代码的逻辑处理方式来判定条件也可以使用ConditionalOnProperty等注解来实现这个诉求  第四步:  封装功能编写代码 上述内容完成后对于lognote-spring-boot-autoconfigure来说直接进行相关代码的编写就像在SpringBoot项目中一样可以直接使用SpringBoot中的相关注解等信息我们在starter中创建的bean以及一些对象在starter被SpringBoot的项目引用后会一并交由引用方的Spring上下文去管理。 相关AOP及注解内容、逻辑细节可在 SpringBoot中通过自定义注解使用AOP中了解这里仅仅放一下核心代码部分。 首先定义一个注解 Target(ElementType.METHOD) // 指定注解的适用范围 Retention(RetentionPolicy.RUNTIME) //指定运行时 //根据条件确定该注解是否生效 Conditional(LognoteCondition.class) public interface LogNote {//方法描述String desc() default ;//是否记录方法执行耗时boolean timeSpan() default true; } 进行切面逻辑开发 Component Aspect Slf4j(topic LogNote) public class LogNoteAspect {Around(annotation(org.example.LogNote))public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable {MethodSignature signature (MethodSignature) point.getSignature();//获取被调用方法Method method signature.getMethod();//取出被调用方法的注解方便后续使用注解中的属性ApiLog loglinstener method.getAnnotation(ApiLog.class);log.info(———————-method[{}]start——————–,method.getName());log.info(方法描述:{},loglinstener.desc());log.info(参数 :{},point.getArgs());long startTime System.currentTimeMillis();Object proceed point.proceed();long endTime System.currentTimeMillis();log.info(耗时:{}ss,endTime-startTime);log.info(———————-method[{}] end——————–\n,method.getName())return proceed;} } 第五步测试打包 使用maven将整个lognote-spring-boot-starter项目进行打包将该依赖引入到自己项目中在yml中进行相关配置开启然后进行使用 !– 在相关项目中引入自定义的lognote-spring-boot-starter依赖 –
dependencygroupIdorg.example/groupIdartifactIdlognote-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/version /dependency 使用 LogNote(desc 执行Es查询)public JSONObject seachEsData(String indexName, SearchSourceBuilder searchSourceBuilder) {JSONObject resultMap new JSONObject();…….return resultMap;} 运行效果 2023-06-05 20:00:00 [LogNote] :——————–method[searchESData]start————— 2023-06-05 20:00:00 [LogNote] :方法描述执行Es查询 2023-06-05 20:00:00 [LogNote] :参数 {query:{match_all:{},size:1,from:0}},log 2023-06-05 20:00:00 [LogNote] :耗时 58ss 2023-06-05 20:00:00 [LogNote] :——————–method[searchESData] end————— 补充starter项目的规范结构 结合上面的例子其实不难发现starter的开发核心是与SpringBoot进行连接包括Spring上下文的串通、SpringBoot的自动化配置串通等其核心是在于lognote-spring-boot-autoconfigure甚至直接进行lognote-spring-boot-configure开发第三方直接引用lognote-spring-boot-configure都可以。亦或者只创建一个lognote-spring-boot-starter项目把原本写在autoconfigure的代码和spring.factories配置放到里面都行这样甚至更简便那么官方为什么还要引导使用module的形式呢。此种方式可以参考这个项目 其实官方的推荐是站在组件的角度去考虑starter是以组件级为基础单位所以采用module的方式进行开发具有更好的解耦性可以明确层次的组件边界的概念。 使用该结构的优势主要有两点 组件开发管理规范化 使用module可以使组件的开发更加规范化同一组件相关逻辑和内容可以集中在一个module中而不是四散的各个独立的项目。 另外对于开发中的依赖管理也可以更加友好例如原本在lognote-spring-boot-autoconfigure中的一堆依赖目前仅仅是单独在autoconfigure中如果再有新的组件扩展需要新项目可能还要再额外进行引入依赖这中间各个项目的版本管理以及组件本身的版本管理 如果使用module集中管理的方式组件相关的扩展以及核心代码都在XXX-spring-boot这个父项目中各子包、组件所需的依赖也可以统一在一处管理对于组件的版本、发布都是十分规范的。 例如上述例子中可以将autoconfigure的通用依赖统一放到lognote-spring-boot这个父类的pom中统一引入管理 modelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdlognote-spring-boot/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmodulelognot-spring-boot-starter/modulemodulelognote-spring-boot-autoconfgure/module/modulespropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties!– 公共依赖 –dependencyManagementdependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.30/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.12/version/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.7.8/version/dependency/dependencyManagement 组件的扩展和演进更友好 XXX-spring-boot-starter作为整个组件的唯一出口是对外交流的所以只需要是“接口”、“封面性质即可autoconfigure是作为与SpringBoot的Spring上下文和自动化配置对接的一个衔接逻辑本质也是在对接层而核心的组件则可以以jar进行灵活开发。例如mybatis它的核心jdbc以及mybtais相关逻辑组件都可以独立进行开发最后统一通过autoconfigure进行与SpringBoot进行对接即可兼容SpringBoot日后再有其他框架也可以在autoconfigure层进行适配。其核心部分