江苏建设网站公司php网站搬家教程

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

江苏建设网站公司,php网站搬家教程,wordpress 购物插件,xshell如何做网站spring-boot-starter 模块 1.介绍 SpringBoot中的starter是一种非常重要的机制#xff0c;能够抛弃以前繁杂的配置#xff0c;将其统一集成进starter#xff0c;应用者只需要在maven中引入starter依赖#xff0c;SpringBoot就能自动扫描到要加载的信息并启动相应的默认配… spring-boot-starter 模块 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的时候往往会忽略这个东西。 自定义一个分页pagex-spring-boot-starter 开发步骤: 2.1新建工程 2.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/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.11.RELEASE/versionrelativePath/ !– lookup parent from repository –/parentpackagingpom/packaginggroupIdcn.jyx/groupIdartifactIdwin-root/artifactIdversion1.0-SNAPSHOT/versionpropertiesjava.version8/java.version/propertiesmodulesmodulewin-model/modulemodulewin-core/modulemodulepagex-spring-boot-starter/modulemodulelogtimex-spring-boot-starter/module/modulesdependencyManagementdependenciesdependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.16/version/dependencydependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper-spring-boot-starter/artifactIdversion1.4.6/version/dependency/dependencies/dependencyManagement /project 2.3model 模块 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/modelVersionparentgroupIdcn.jyx/groupIdartifactIdwin-root/artifactIdversion1.0-SNAPSHOT/versionrelativePath../pom.xml/relativePath/parentartifactIdwin-model/artifactIddependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-source-plugin/artifactIdexecutionsexecutionidattach-sources/idphasepackage/phasegoalsgoaljar/goal/goals/execution/executions/plugin/plugins/build /project 响应返回对象 package cn.win.model;import lombok.Data;Data public class ResponseDTO {private int code;private String msg;private Object data;public static ResponseDTO success(Object data){ResponseDTO responseDTO new ResponseDTO();responseDTO.setCode(0);responseDTO.setData(data);return responseDTO;}public static ResponseDTO error(int code,String msg){ResponseDTO responseDTO new ResponseDTO();responseDTO.setCode(code);responseDTO.setMsg(msg);return responseDTO;} }2.4core模块 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/modelVersionparentgroupIdcn.jyx/groupIdartifactIdwin-root/artifactIdversion1.0-SNAPSHOT/versionrelativePath../pom.xml/relativePath/parentartifactIdwin-core/artifactIddependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-source-plugin/artifactIdexecutionsexecutionidattach-sources/idphasepackage/phasegoalsgoaljar/goal/goals/execution/executions/plugin/plugins/build/project 声明调用注解 package cn.win.core.annotations;import java.lang.annotation.*;Documented Retention(RetentionPolicy.RUNTIME) Target(ElementType.METHOD) public interface PageX { }package cn.win.core.exceptioin;public class BizException extends RuntimeException {private int code;private String msg;public int getCode() {return code;}public BizException(int code, String msg) {super();this.code code;this.msg msg;}public void setCode(int code) {this.code code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg msg;} }pagex-spring-boot-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/modelVersionparentgroupIdcn.jyx/groupIdartifactIdwin-root/artifactIdversion1.0-SNAPSHOT/versionrelativePath../pom.xml/relativePath/parentartifactIdpagex-spring-boot-starter/artifactIddependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactId/dependencydependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper-spring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactId/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactId/dependencydependencygroupIdorg.apache.tomcat.embed/groupIdartifactIdtomcat-embed-core/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependencydependencygroupIdcn.jyx/groupIdartifactIdwin-core/artifactIdversion1.0-SNAPSHOT/versionscopecompile/scope/dependencydependencygroupIdcn.jyx/groupIdartifactIdwin-model/artifactIdversion1.0-SNAPSHOT/versionscopecompile/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-source-plugin/artifactIdexecutionsexecutionidattach-sources/idphasepackage/phasegoalsgoaljar/goal/goals/execution/executions/plugin/plugins/build/project 全局响应处理类 advice 用于处理Spring MVC框架中的异常和响应体的转换 package cn.win.logtimex.advice;import cn.hutool.json.JSONUtil; import cn.win.core.exceptioin.BizException; import cn.win.model.ResponseDTO; import com.github.pagehelper.Page;import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import java.util.HashMap;RestControllerAdvice public class MyResponseAdvice implements ResponseBodyAdvice {Overridepublic boolean supports(MethodParameter methodParameter, Class aClass) {return true;}ExceptionHandler(Exception.class)public Object exceptionHandler(Exception ex){ex.printStackTrace();if(ex instanceof BizException){BizException bizException (BizException) ex;return ResponseDTO.error(bizException.getCode(),bizException.getMsg());}return ResponseDTO.error(500, ex.getMessage());}Overridepublic Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {if (body instanceof ResponseDTO){return body;}ResponseDTO dto ResponseDTO.success(body);if(body instanceof Page){Page page (Page) body;long total page.getTotal();HashMapString,Object map new HashMap();map.put(total,total);map.put(items,body);dto ResponseDTO.success(map);}if(aClass StringHttpMessageConverter.class){return JSONUtil.toJsonStr(dto);}return dto;} }AOP package cn.win.logtimex.aop;import cn.hutool.core.util.ObjectUtil; import com.github.pagehelper.PageHelper; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;Component Aspect public class PagexAop {//切点表达式表示该切面将应用于所有被PageX注解标记的方法。Pointcut(annotation(cn.win.core.annotations.PageX))public void point(){}Around(point())public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println(执行方法之前);RequestAttributes requestAttributes RequestContextHolder.getRequestAttributes();HttpServletRequest request ((ServletRequestAttributes) requestAttributes).getRequest();String pageNum request.getParameter(pageNum);String pageSize request.getParameter(pageSize);if (ObjectUtil.isNotEmpty(pageNum)ObjectUtil.isNotEmpty(pageSize)){PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));}Object result pjp.proceed(); // 调用目标方法System.out.println(执行方法之后);return result;} } 创建自动配置类AuthorityAutoConfigruation   package cn.win.logtimex.autoconfiguration;import cn.win.logtimex.advice.MyResponseAdvice; import cn.win.logtimex.aop.PagexAop; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;Configuration Import( {PagexAop.class, MyResponseAdvice.class}) // cn.smart.pagex.enbale xxx ConditionalOnProperty(prefix cn.win.pagex, value enable,havingValuetrue,matchIfMissingtrue) public class PageXAutoConfiguration { // 领头羊 } 配置类 package cn.win.logtimex.properties;import lombok.Data; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties;Data ConfigurationProperties(prefix cn.win.pagex) EnableAutoConfiguration public class PageXProperties {private boolean enabletrue; }配置自动配置 在resourecs文件目录下创建META-INF并创建我们自己的spring.factories并把我们的 MemberStaterAutoConfiguration 添加进去 org.springframework.boot.autoconfigure.EnableAutoConfiguration\cn.win.logtimex.autoconfiguration.PageXAutoConfiguration