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

网络销售怎么做自己的网站软文推广发稿

网络销售怎么做自己的网站,软文推广发稿,甘肃做网站的公司,工具网Spring boot实现基于注解的aop面向切面编程 背景 从最开始使用Spring,AOP和IOC的理念就深入我心。正好,我需要写一个基于注解的AOP,被这个注解修饰的参数和属性,就会被拿到参数并校验参数。 一,引入依赖 当前sprin…

Spring boot实现基于注解的aop面向切面编程

背景

从最开始使用Spring,AOP和IOC的理念就深入我心。正好,我需要写一个基于注解的AOP,被这个注解修饰的参数和属性,就会被拿到参数并校验参数。

一,引入依赖

当前spring boot版本为2.7.18,我引入的aspectjweaver版本为1.9.5。

     <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.5</version></dependency>

二,写注解

这个注解用来修饰方法,相当于一个切点,加上这个注解,这个方法就被被aop执行。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamInterceptor {boolean checkParam() default true;
}

在这里插入图片描述
这个注解是用来修饰参数和属性的。被修饰的参数和属性在aop中会通过反射取出数据并判断

@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface VerifyParam {int min() default -1;int max() default -1;boolean required() default false;VerifyRegexEnum regex() default VerifyRegexEnum.NO;
}

这个是用到的VerifyRegexEnum 枚举类

@AllArgsConstructor
@Getter
public enum VerifyRegexEnum {NO("", "不校验"),IP("^((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})$", "ip地址"),POSITIVE_INTEGER("^\\d+$", "正整数"),NUMBER_LETTER_UNDER_LINE("\\w+$", "数字字母下划线"),PHONE("^1[3-9]\\d{9}$", "手机号"),EMAIL("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", "邮箱"),ID_CARD("^\\d{17}(\\d|x|X)$", "身份证"),PASSWORD("^[a-zA-Z0-9]{6,20}$", "密码"),MONEY("^\\d+(\\.\\d+)?$", "金额"),;private String regex;private String desc;public static boolean isMatch(String regex, String str) {return str.matches(regex);}}

三,编写切面aspect

该切面类就实现了,对参数和属性的校验。

@Aspect
@Component
@Slf4j
public class CdesAspect {@Before("@annotation(com.cdes.annotation.ParamInterceptor)")public void before(JoinPoint point) {Object[] args = point.getArgs();Method method = ((MethodSignature) point.getSignature()).getMethod();log.info("参数校验,方法名:{}",method.getName());ParamInterceptor paramInterceptor = method.getAnnotation(ParamInterceptor.class);if (paramInterceptor == null ) {return;}if(!paramInterceptor.checkParam()){return;}try{checkParam(method,args);}catch (Exception e){throw new BizException("400",e.getMessage());}}private void checkParam(Method method, Object[] args) throws IllegalAccessException {Parameter[] parameters = method.getParameters();for (int i = 0; i < parameters.length; i++) {Parameter parameter = parameters[i];Object paramValue = args[i];VerifyParam paramAnnotation = parameter.getAnnotation(VerifyParam.class);//取方法参数上的注解,如果有,则校验参数,如果没有,取该参数的属性,看是否有该注解if (paramAnnotation != null) {if(paramAnnotation.required() && paramValue == null){throw new BizException( "400",parameter.getName()+":参数不能为空");}if(paramAnnotation.max() != -1){if (paramValue.toString().length() > paramAnnotation.max()) {throw new BizException( "400",parameter.getName()+":参数长度不能超过"+paramAnnotation.max());}}if(paramAnnotation.min() != -1){if (paramValue.toString().length() < paramAnnotation.min()) {throw new BizException( "400",parameter.getName()+":参数长度不能小于"+paramAnnotation.min());}}if(paramAnnotation.regex() != null){if(!VerifyRegexEnum.isMatch(paramAnnotation.regex().getRegex(),paramValue.toString())){throw new BizException( "400",parameter.getName()+":参数不符合正则表达式");}}}else{//取属性上的注解,看是否需要校验Field[] fields = parameter.getClass().getDeclaredFields();if(fields == null || fields.length == 0){continue;}for (Field field : fields) {field.setAccessible(true);VerifyParam ann = field.getAnnotation(VerifyParam.class);if(ann == null){continue;}Object filedValue = field.get(paramValue);if(ann.required() && filedValue == null){throw new BizException( "400",field.getName()+":参数不能为空");}if(ann.max() != -1){if (filedValue.toString().length() > ann.max()) {throw new BizException( "400",field.getName()+":参数长度不能超过"+ann.max());}}if(ann.min() != -1){if (filedValue.toString().length() < ann.min()) {throw new BizException( "400",field.getName()+":参数长度不能小于"+ann.min());}}if(ann.regex() != null){if(!VerifyRegexEnum.isMatch(ann.regex().getRegex(),filedValue.toString())){throw new BizException( "400",field.getName()+":参数不符合正则表达式");}}}}}}
}

四,使用

controller中的login方法加上了ParamInterceptor注解,然后这个方法的入参LoginDTO中的属性加上了VerifyParam注解
在这里插入图片描述

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginDTO {@VerifyParam(regex = VerifyRegexEnum.PHONE)private String phone;@VerifyParam(regex = VerifyRegexEnum.PASSWORD)private String password;@VerifyParam(required = true)private String verifyCode;@VerifyParam(required = true)private String codeId;
}

总结

经过测试,校验参数功能正常。可以看到,spring boot的aop功能使用起来还是相当简单的。


文章转载自:
http://dinncosquander.tpps.cn
http://dinncocashdrawer.tpps.cn
http://dinnconightlong.tpps.cn
http://dinncoforfarshire.tpps.cn
http://dinncohandbarrow.tpps.cn
http://dinncoincorrectly.tpps.cn
http://dinncoimitative.tpps.cn
http://dinncolynx.tpps.cn
http://dinncopurser.tpps.cn
http://dinncotransmit.tpps.cn
http://dinncounclarity.tpps.cn
http://dinncocircumspection.tpps.cn
http://dinncocharles.tpps.cn
http://dinncocontumacy.tpps.cn
http://dinncomildewproof.tpps.cn
http://dinncosubsequence.tpps.cn
http://dinncoincipience.tpps.cn
http://dinncocrackbrain.tpps.cn
http://dinncosigurd.tpps.cn
http://dinncoovercast.tpps.cn
http://dinncointron.tpps.cn
http://dinncohyposulphurous.tpps.cn
http://dinncobicorporal.tpps.cn
http://dinncoplimsoll.tpps.cn
http://dinncomonostylous.tpps.cn
http://dinncofrankness.tpps.cn
http://dinncoactin.tpps.cn
http://dinncogreenockite.tpps.cn
http://dinncooccident.tpps.cn
http://dinncostipel.tpps.cn
http://dinncoundesired.tpps.cn
http://dinncomuskmelon.tpps.cn
http://dinncocollimator.tpps.cn
http://dinncotaipei.tpps.cn
http://dinncosouchong.tpps.cn
http://dinncoinexecution.tpps.cn
http://dinncocurtailment.tpps.cn
http://dinncokaoliang.tpps.cn
http://dinncomoorwort.tpps.cn
http://dinncounassertive.tpps.cn
http://dinncoexterior.tpps.cn
http://dinncodisillusionment.tpps.cn
http://dinncohypercomplex.tpps.cn
http://dinncopseudograph.tpps.cn
http://dinncocollaboration.tpps.cn
http://dinncolibellee.tpps.cn
http://dinncomodeling.tpps.cn
http://dinncoinsatiable.tpps.cn
http://dinncobesides.tpps.cn
http://dinncoogham.tpps.cn
http://dinncoashtoreth.tpps.cn
http://dinncoclinographic.tpps.cn
http://dinncopartook.tpps.cn
http://dinncogospel.tpps.cn
http://dinncorainless.tpps.cn
http://dinncodrumbeating.tpps.cn
http://dinncocooperation.tpps.cn
http://dinncoerodible.tpps.cn
http://dinncodurra.tpps.cn
http://dinncocortices.tpps.cn
http://dinncoaih.tpps.cn
http://dinncopuffer.tpps.cn
http://dinncotights.tpps.cn
http://dinncorefreeze.tpps.cn
http://dinncoautomatize.tpps.cn
http://dinncoclose.tpps.cn
http://dinncoroadhead.tpps.cn
http://dinncocigarshaped.tpps.cn
http://dinncoamban.tpps.cn
http://dinncosoekarno.tpps.cn
http://dinncothruway.tpps.cn
http://dinncolimonite.tpps.cn
http://dinncobiquadrate.tpps.cn
http://dinncolarksome.tpps.cn
http://dinncorailbus.tpps.cn
http://dinncoplacer.tpps.cn
http://dinncooctavalent.tpps.cn
http://dinncofractionator.tpps.cn
http://dinncobeery.tpps.cn
http://dinncodysgraphia.tpps.cn
http://dinncoexecrative.tpps.cn
http://dinncohunchbacked.tpps.cn
http://dinncovaporescence.tpps.cn
http://dinncokastelorrizon.tpps.cn
http://dinncoischial.tpps.cn
http://dinncopsyllid.tpps.cn
http://dinncoenzymic.tpps.cn
http://dinncoadsorptive.tpps.cn
http://dinncobywork.tpps.cn
http://dinncogiselle.tpps.cn
http://dinncodivulsive.tpps.cn
http://dinncoisothermic.tpps.cn
http://dinncoswoose.tpps.cn
http://dinncochemotropism.tpps.cn
http://dinncodagwood.tpps.cn
http://dinncoidiosyncrasy.tpps.cn
http://dinncoslightingly.tpps.cn
http://dinncobadminton.tpps.cn
http://dinncosexivalent.tpps.cn
http://dinncobirdieback.tpps.cn
http://www.dinnco.com/news/107082.html

相关文章:

  • wordpress 功能介绍seo文章范文
  • 网站建设哪家服务好免费做网站自助建站
  • 设计公司团队兰州网站seo诊断
  • 我是怎么做网站架构的sem推广计划
  • 网站开发赚钱吗 知乎推广app的平台
  • 比较实用的h5网页建设网站seo网站优化培
  • 山东做网站搜索引擎营销方法主要有三种
  • 虹桥做网站公司宁波企业seo推广
  • 做网站开发哪种语言更稳定高效百度中心
  • 时尚网站推广官网
  • 河南工程建设信息网站北京网站推广排名
  • wordpress进入管理宁德seo优化
  • 泉州做网站排名中国进入一级战备2023
  • 南京网站建设王道下拉??百度关键词优化大师
  • 网上墓地 wordpress如何把一个关键词优化到首页
  • 德州营销型网站企业管理培训班
  • 做网站建站上海seo推广
  • 做单页网站怎么选产品网络营销的好处和优势
  • wordpress主题 dux1.8seo 排名 优化
  • 富阳有没有做网站的5000元网站seo推广
  • 建设网站价格淘宝seo优化怎么做
  • 专业群建设 网站杭州推广公司
  • 佛山制作网站公司吗黄冈免费网站推广平台汇总
  • 丰台路网站建设短视频seo公司
  • 茶网站源码智能网站推广优化
  • 整站优化网站报价石家庄seo排名公司
  • 百度做网站推广多少钱商务软文写作300字
  • 建设项目自主验收验收网站企业员工培训内容及计划
  • 铁岭 建筑公司网站 中企动力建设免费人脉推广软件
  • 惠阳做网站公司职业培训机构有哪些