自定义标签注入
来源:互联网 发布:淘宝加盟骗局公安查吗 编辑:程序博客网 时间:2024/06/08 06:28
1、首先创建一个自定义的标签 Label
@Target(FIELD)
@Retention(RUNTIME)
public @interface Label {
String value();
}
2、Label 标签的使用
public class Customer{
@Label("客户标识") //默认value
private Long customerId; //客户标识
@Label("分类标识")
private Long corpClassId; //分类标识
}
3、标签工具类
public class LabelUtils {
private static Map<Class, Map<String, String>> pool =
new HashMap<Class, Map<String, String>>();
private LabelUtils() {
}
public static Map<String, String> getLabels(Class c) {
if(pool.containsKey(c)) {
return pool.get(c);
}
Map<String, String> map = new HashMap<String, String>();
Class cc = c;
do {
Field[] fields = cc.getDeclaredFields();
for(Field field : fields) {
Label ann = field.getAnnotation(Label.class);
if(ann != null) {
map.put(field.getName(), ann.value());
}
}
cc = cc.getSuperclass();
} while(!cc.getName().equals("java.lang.Object"));
pool.put(c, map);
return map;
}
}
4、测试
public class Test{
public void static main(String[] args){
Map<String, String> map = LabelUtils .getLabels(Customer.class)
for(String key:map.keySet())
System.out.println(key+":"+map.get(key));
}
}
输出结果为:customerId :客户标识
corpClassId: 分类标识
- 自定义标签注入
- 自定义标签与spring注入
- 自定义标签与spring注入
- 自定义标签Spring注入的问题
- JSP自定义标签的spring注入
- 在struts 自定义标签中注入spring bean
- 阿录帮帮忙—ssh框架中,简单自定义标签SimpleTagSupport如何注入service
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- 自定义标签
- R语言与机器学习学习笔记(分类算法)(5)神经网络
- oracle 高水位线详解
- 由UIColor 生成 UIImage
- -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法
- Last_IO_Error: error connecting to master Last_IO_Errno: 2003解决方法
- 自定义标签注入
- 差强人意的iBeacons定位精度
- R语言与机器学习学习笔记(分类算法)(6)logistic回归
- iOS 多层级的immutable objects 转换成 mutable objects
- 网页关闭(解决window.close在火狐下不兼容问题) 区分浏览器类别
- Java获取目录文件信息
- maven学习笔记(1)--特点和简例
- 串口:交叉线与直连线的区别
- python+django初学之安装(一)