b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Spring MVC通过添加自定义注释来格式化数据

电脑杂谈  发布时间:2020-03-21 16:04:29  来源:网络整理

springmvc 注解_springmvc注解详解_springmvc自定义注解

由Script House收集和整理的本文主要介绍Spring MVC通过添加自定义注释来格式化数据的方法. Script House的编辑感觉很好. 现在springmvc自定义注解,我将与您分享并提供参考.

Springmvc自定义注释和自定义注释分析

I. 自定义注释名称

@Target({ElementType.TYPE,ElementType.METHOD})  //类名或方法上
@Retention(RetentionPolicy.RUNTIME)//运行时
@component//自定义多个注解,且在一个类中添加两个或以上的,只需要加一个 否则会实例化多次。
public @interface SocketMapping {
 String value() default "";//参数
}

第二,测试课程

@SocketMapping("/a")
public class TestAnno {
 @SocketMapping(value="/b")
 public void ss(){
 System.out.println(11);
 }
 
}

springmvc自定义注解_springmvc注解详解_springmvc 注解

三,分析测试类所在的包,反思

ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
 
  Resource[] res = rpr.getResources("classpath*:websocket/**.class");//测试类的包
  
  for(int i=0;i<res.length;i++){
  String className = res[i].getURL().getPath();
  className = className.split("(classes/)|(!/)")[1];
  className = className.replace("/",".").replace(".class","");//获取到文件结构 com.xl.joe.testAnno
  Class<?> cla = Class.forName(className);//获取到文件类
  
   if(cla.isAnnotationPresent(SocketMapping.class)){//判断是否存在自定义注解
   System.out.println(cla.getAnnotation(SocketMapping.class).value());//获取自定义注解的属性值
   
   }
   
   Object o = SpringContextUtil.getBean("testAnno");//获取类对象
   
   Method[] methods = cla.getMethods();//获取类的方法
   for(Method method:methods){
   if(method.isAnnotationPresent(SocketMapping.class)){//找到自定义注解
    method.invoke(o,new Object[]{});//反射改方法
   }
   }
  
  }

我遇到了问题

传入的开始时间和结束时间,格式为yyyyMMdd. 查询数据必须按给定的时间段进行过滤.

例如

http://127.0.0.1:8095/iportal-dev/v1/sms/sending/list?stime=20161001&etime=20161130

springmvc自定义注解_springmvc 注解_springmvc注解详解

但是在服务器接受时间后,根据业务要求,格式应为

20161001 00:00:00< 时间段 <20161130 23:59:59

stime可以使用springMVC提供的默认@DateTimeFormat(pattern =“ yyyyMMdd”)来获取正确的开始时间,但是在结束时间,默认格式注释无法完成要求〜

模仿@DateTimeFormat自定义界面

由于它是被模仿的,因此可以继承使用的某些方法无需进行重大更改.

@MyDateTimeFormat批注

springmvc注解详解_springmvc自定义注解_springmvc 注解

package cn.jpush.iportal.common.support;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import java.lang.annotation.*;
/**
 * 使用方法与@DateTimeFormat一致,但是通过它进行注解的字段,会格式化为当天的23:59:59.
 * 其他格式的用法也可以支持.
 * @author Administrator
 *
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER,ElementType.ANNOTATION_TYPE})
public @interface MyDateTimeFormat {
 String style() default "SS";
 ISO iso() default ISO.NONE;
 String pattern() default "";
}

@MyDateTimeFormat批注处理类

package cn.jpush.iportal.common.support;
import org.springframework.context.support.EmbeddedValueResolutionSupport;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Formatter;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
import java.util.*;
public class MyDataTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
  implements AnnotationFormatterFactory<MyDateTimeFormat> {
 private static final Set<Class<?>> FIELD_TYPES;
 static {
  Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
  fieldTypes.add(Date.class);
  fieldTypes.add(Calendar.class);
  fieldTypes.add(Long.class);
  FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
 }
 @Override
 public Set<Class<?>> getFieldTypes() {
  return FIELD_TYPES;
 }
 @Override
 public Printer<?> getPrinter(MyDateTimeFormat annotation,Class<?> fieldType) {
  return getFormatter(annotation,fieldType);
 }
 @Override
 public Parser<?> getParser(MyDateTimeFormat annotation,fieldType);
 }
 protected Formatter<Date> getFormatter(MyDateTimeFormat annotation,Class<?> fieldType) {
  MyDateFormatter formatter = new MyDateFormatter();
  formatter.setStylePattern(resolveEmbeddedValue(annotation.style()));
  formatter.setIso(annotation.iso());
  formatter.setPattern(resolveEmbeddedValue(annotation.pattern()));
  return formatter;
 }
}

解析接口超载

通过调用原始处理函数super.parse(文本,语言环境),获取转换后的Date对象,然后添加相关的处理业务springmvc自定义注解,然后返回Date.

package cn.jpush.iportal.common.support;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.format.datetime.DateFormatter;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class MyDateFormatter extends DateFormatter {
 @Override
 public Date parse(String text,Locale locale) throws ParseException {
  Date target = super.parse(text,locale);
  //+1天
  Date date = DateUtils.ceiling(new Date(target.getTime() + 1),Calendar.DATE);
  //减1ms,得出23:59:59
  Date result =new Date(date.getTime()-1);
  return result;
 }
}

springmvc自定义注解_springmvc 注解_springmvc注解详解

使用SpringMVC注册我们的自定义注释处理类

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
 @Override
 public void addFormatters(FormatterRegistry registry) {
  MyDataTimeFormatAnnotationFormatterFactory annoFormater =new MyDataTimeFormatAnnotationFormatterFactory();
  registry.addFormatterForFieldAnnotation(annoFormater);
 }

摘要

以上是本文的全部内容. 希望本文的内容对每个人的学习或工作都有一定的参考和学习价值. 如有任何疑问,可以留言和交流,谢谢您对编程技巧的支持.

以上是Spring MVC的全部内容,由脚本公司通过添加自定义注释来格式化数据来为您收集和组织. 我希望本文可以通过添加自定义注释来格式化数据来帮助您解决Spring MVC遇到的问题. 程序开发问题.

如果您认为脚本库网站的内容还不错,请向程序员朋友推荐脚本库网站.


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-147328-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...