@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);
}
}
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[]{});//反射改方法
}
}
}
http://127.0.0.1:8095/iportal-dev/v1/sms/sending/list?stime=20161001&etime=20161130
20161001 00:00:00< 时间段 <20161130 23:59:59
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 "";
}
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, Class<?> fieldType) {
return getFormatter(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;
}
}
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;
}
}
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
@Override
public void addFormatters(FormatterRegistry registry) {
MyDataTimeFormatAnnotationFormatterFactory annoFormater =new MyDataTimeFormatAnnotationFormatterFactory();
registry.addFormatterForFieldAnnotation(annoFormater);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有