首钢建设集团山东公司网站,如何做付款网站,全国卫生机构建设管理系统网站,中国十大培训机构影视后期现在很多开源的组件都会提供对应的 springboot-starter 包给我们去用#xff0c;要做一个 starter 包并不难。参照Spring内置的实现就好了#xff1a; 1、在工程里引入 starter 打包相关的依赖。 2、在我们工程内建 spring.factories 文件#xff0c;编写我们配置类的全限类… 现在很多开源的组件都会提供对应的 springboot-starter 包给我们去用要做一个 starter 包并不难。参照Spring内置的实现就好了 1、在工程里引入 starter 打包相关的依赖。 2、在我们工程内建 spring.factories 文件编写我们配置类的全限类名。 一、自定义步骤详解 使用AOP实现拦截方法执行和打印日志的功能 1.1、创建Starter项目
名称 zhinian-log-spring-boot-starter , 版本号是 1.0-SNAPSHOT 。且同时引入下方依赖。
1.2、引入pom依赖
?xml version1.0 encodingUTF-8?
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/modelVersiongroupIdcom.zn.opit.base/groupIdartifactIdzhinian-log-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdversion2.7.13/versionoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactIdversion2.7.13/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationclassifierexec/classifier/configuration/plugin/plugins/build/project1.3、定义Starter需要的配置类(Properties)
package com.zn.opit.base.config;import org.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefix zn.log)
public class ZhiNianLogProperties {private boolean enabled;public boolean isEnabled() {return enabled;}public void setEnabled(boolean enabled) {this.enabled enabled;}
}如果我们需要从 application.yaml 或 application.properties 中拿到一些使用者配置的数据那么我们就需要定义一个properties类。这个properties类主要作用是将 application.yaml 或 application.properties 中的配置信息映射成实体类比如我们这里指定 prefix “zn.log” 这样我们就能将以zn.log为前缀的配置项拿到了。
1.4、编写Starter项目的业务功能
AOP切面逻辑
package com.zn.opit.base.component;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;Aspect
Component
public class WebLogAspect {Pointcut(execution(* *..*Controller.*(..)))public void webLog(){}Before(webLog())public void doBefore(JoinPoint joinPoint) throws Throwable {//方法开始前打印开始日志System.out.println(zn-starter --------doBefore);}AfterReturning(returning ret, pointcut webLog())public void doAfterReturning(Object ret) throws Throwable {// 处理完请求打印结束日志System.out.println(zn-starter --------doAfterReturning);}
}1.5、编写自动配置类
package com.zn.opit.base.config;import com.zn.opit.base.component.WebLogAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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({ZhiNianLogProperties.class}) // 该注解的作用是为 xxxProperties 开启属性配置功能并将这个类以组件的形式注入到容器中
ConditionalOnProperty(prefix zn.log, value enabled) // 当指定的配置项等于你想要的时候配置类生效
public class ZnLogAutoConfig {Bean // Bean该注解用于将方法的返回值以 Bean 对象的形式添加到容器中ConditionalOnMissingBean // ConditionalOnMissingBean(xxx.class)该注解表示当容器中没有 xxx 类时该方法才生效public WebLogAspect webLogAspect() {return new WebLogAspect();}
}1.6、编写spring.factories文件加载自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfigurationcom.zn.opit.base.config.ZnLogAutoConfig1.7、构建starter制品包
buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationclassifierexec/classifier/configuration/plugin/plugins
/build在项目根目录下执行mvn clean install打包并放到maven仓库中(maven仓库地址和配置有关)
显示build success表示starter制品包打包完成
二、其它项目引用验证 2.1、pom中引入自定义starter依赖
?xml version1.0 encodingUTF-8?
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/modelVersiongroupIdcom.zn.opit.base/groupIdartifactIdzhinian-log-starter-test/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion2.7.13/version/dependencydependencygroupIdcom.zn.opit.base/groupIdartifactIdzhinian-log-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies
/project2.2、写一个Controller接口
(要符合starter里的aop拦截表达式规则否则拦截不到)
package com.zn.opit.base.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RequestMapping(/hello)
RestController
public class HelloController {GetMapping(/test)public String test() {return SUCCESS;}
}2.3、配置文件 application.properties
zn.log.enabledtrue
server.port80772.4、验证
启动业务项目访问接口验证
至此一个简单的starter示例就写完了。