Annotation的大材小用

最近在开发一些通用的excel数据导入的功能,由于涉及到导入的模块很多,所以开发了一个比较通用的excel导入模板类文件。并且使用annotation作为验证数据的配置。

package com.hp.dylan.jv;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelAnnotation {
	
//excel column name
	public String importCellName();

//excel cell data weather is required
	public boolean isRequired();

//excel cell data datalength.	
	public int importDataLen();

//excel cell data type.	
	public String importDataType();
	
//excel cell mapping index.
	public int importCellIndex();

//excel data weather eixts in db.	
	public boolean isEixtance();
}
看看需要导入excel的模型类的定义:(AdditionJvImportModel)
package com.hp.dylan.jv;
public class AdditionJvImportModel {
 @ExcelAnnotation( importCellName = "EN1",isRequired=true ,importDataLen=100,importDataType="String",importCellIndex = 1,isEixtance=true)
 private String sellEntity;
 @ExcelAnnotation(importCellName = "SE2",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex =2, isEixtance=false)
 private String sellSe;
 @ExcelAnnotation(importCellName = "DI3",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 3, isEixtance=false)
 private String sellDi;
 @ExcelAnnotation(importCellName = "DEPT4",isRequired=false ,importDataLen=8,importDataType="String",importCellIndex = 4, isEixtance=false)
 private String sellDept;
 @ExcelAnnotation(importCellName = "SF5",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 5, isEixtance=false)
 private String sellSf;
 @ExcelAnnotation(importCellName = "PT6",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 6, isEixtance=false)
 private String sellPt;
 @ExcelAnnotation(importCellName = "PL7",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 7, isEixtance=false)
 private String sellPl;
 @ExcelAnnotation(importCellName = "SL8",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 8, isEixtance=false)
 private String sellSl;
 @ExcelAnnotation(importCellName = "Amount9",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 9, isEixtance=false)
 private String sellAmout;
 @ExcelAnnotation(importCellName = "EN10",isRequired=true ,importDataLen=100,importDataType="String",importCellIndex = 10, isEixtance=true)
 private String buyEntity;
 @ExcelAnnotation(importCellName = "SE11",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 11, isEixtance=false)
 private String buySe;
 @ExcelAnnotation(importCellName = "DI12",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 12, isEixtance=false)
 private String buyDi;
 @ExcelAnnotation(importCellName = "DEPT13",isRequired=false ,importDataLen=8,importDataType="String",importCellIndex = 13, isEixtance=false)
 private String buyDept;
 @ExcelAnnotation(importCellName = "SF14",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 14, isEixtance=false)
 private String buySf;
 @ExcelAnnotation(importCellName = "PT15",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 15, isEixtance=false)
 private String buyPt;
 @ExcelAnnotation(importCellName = "PL16",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 16, isEixtance=false)
 private String buyPl;
 @ExcelAnnotation(importCellName = "SL17",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 17, isEixtance=false)
 private String buySl;
 @ExcelAnnotation(importCellName = "LSA Factor19",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 19, isEixtance=false)
 private String tranFactor;
 @ExcelAnnotation(importCellName = "GrossICPurchase20",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 20, isEixtance=false)
 private String tranGross;
 @ExcelAnnotation(importCellName = "EX Code21",isRequired=false ,importDataLen=5,importDataType="String",importCellIndex = 21, isEixtance=false)
 private String tranExCode;
 @ExcelAnnotation(importCellName = "Transaction Description24",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 24, isEixtance=false)
 private String tranDesr;
 @ExcelAnnotation(importCellName = "Line Comments25",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 25, isEixtance=false)
 private String tranLineComm;
 @ExcelAnnotation(importCellName = "Additional Line Comments30",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 30, isEixtance=false)
 private String tranAddLinComm;
 @ExcelAnnotation(importCellName = "FROM VATID31",isRequired=false ,importDataLen=6,importDataType="String",importCellIndex = 31, isEixtance=false)
 private String tranFVatId;
 @ExcelAnnotation(importCellName = "TO VATID32",isRequired=false ,importDataLen=6,importDataType="String",importCellIndex = 32, isEixtance=false)
 private String tranTVatId;
}
excel模型的解析中如何获取到此模型的描述配置。也就是获取他的annotation。
Map<Integer,ExcelAnnotation> mapping=new HashMap<Integer,ExcelAnnotation>();
   for (int i = 0; i < filed.length; i++) {
    Field f = filed[i];
    ExcelAnnotation excel = f.getAnnotation(ExcelAnnotation.class);
    if (excel != null) {
     mapping.put(excel.importCellIndex(), excel);
    }
   }
如何根据annotation的描述来验证。
if(anno.isRequired()){
       if(cellValue==null||"".equals(cellValue.trim())){
            sb.append(anno.importCellName()+" can not be empty!"); 。。。}
     }
如果需要跟db交互来进行检验的话,只需要定义一个abstract校验方法,具体的校验规则交给其实现者来完成
public abstract String validateExist(String value);

Noted:

你可以annotation完全的看做一种配置,你只需要获取你的model种的annotation标识,然后根据标识来进行一个校验规则的解析

你也可以结合 class reflection 机制来使用annotation,获取到model的annotation描述之后,根据class reflection 来动态的激活调用一些model中的方法,

 

 

转载于:https://my.oschina.net/ITBoy/blog/17648

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: OxyPlot Annotation是一个在OxyPlot库中功能强大的注释工具。注释是在图表上添加额外信息以增强数据可视化的一种方式。 使用OxyPlot Annotation,我们可以在图表上添加文本、线条、箭头、圆形等注释元素。这些注释元素可以帮助我们更好地描述和解释图表中的数据。 例如,我们可以使用注释添加一个标题来说明该图表的主题或目的。我们可以选择注释的位置、字体、大小和颜色等样式,以及背景色和边框等外观效果。 除了文本注释,我们还可以使用注释绘制线条,比如表示趋势线或连接两个数据点之间的关系。线条注释有许多样式和参数可以定制,例如线的颜色、线宽、线型和箭头的形状等。 另外,我们还可以使用注释绘制圆形或矩形等形状。这些形状可以用来突出显示某个数据点或数据区域,或者用来标记某个特殊事件发生的位置。 总之,OxyPlot Annotation提供了丰富的注释功能,使我们能够更好地展示和解释数据。通过使用不同的注释元素和样式,我们可以设计出具有吸引力和易读性的图表,帮助用户更好地理解和分析数据。 ### 回答2: OxyPlot Annotation是一个功能强大的注释工具,可以在OxyPlot图表中添加文本、箭头、线条等。它提供了一种方便的方式来标记和注释图表上的特定区域或数据点。 通过OxyPlot Annotation,用户可以在图表中添加静态或动态的注释,以便更好地理解数据的含义。这些注释可以用于指示特定数据点或趋势、说明数据变化的原因、提示特定事件的发生等。 使用OxyPlot Annotation,用户可以自定义注释的外观和位置。例如,可以调整文本的颜色、字体、大小和对齐方式;可以设置箭头的样式、宽度和长度;可以绘制自定义的线条并将其连接到图表上的任何位置。 除了静态注释,OxyPlot Annotation还支持动态注释。用户可以根据特定的事件或条件,在图表上动态地添加、更新或删除注释。这使得用户可以根据数据的变化自动更新注释,以便更好地呈现动态或实时的数据。 总之,OxyPlot Annotation是一个非常实用的工具,能够让用户在OxyPlot图表中添加各种注释,以便更好地理解和展示数据。无论是在科学研究、数据可视化、教育培训还是其他领域,OxyPlot Annotation都能为用户提供强大而灵活的注释功能。 ### 回答3: OxyPlot是一个用于绘制图形和图表的开源.NET绘图库。Annotation(注释)是OxyPlot中的一个重要功能,用于向绘图中添加额外的注释信息,以便更好地展示数据。 OxyPlot中的Annotation提供了多种类型的注释,包括文本注释、线条注释、矩形注释、椭圆注释等。通过调整注释的位置、大小、颜色、字体等属性,我们可以将注释放置在适当的位置,并与图形数据进行关联。 首先,我们可以使用文本注释来添加带有说明或说明的文字。通过指定文字的位置、大小、颜色等属性,我们可以将文字注释放置在图形的特定位置,并使其与相应的数据点相关联。 其次,线条注释可以用于在图形中添加直线或曲线。通过指定线条的起始点和终点,以及颜色、宽度等属性,我们可以将线条注释放置在图形中,并以可视化方式突出显示数据之间的关联。 矩形注释和椭圆注释可以用于在图形中创建一个矩形或椭圆形区域。通过指定其位置、大小、颜色等属性,我们可以将矩形或椭圆注释放置在图形中的指定区域,并突出显示该区域的特定数据。 综上所述,OxyPlot中的Annotation提供了丰富的注释功能,能够帮助我们更好地理解和分析绘图数据。通过使用不同类型的注释对象,并设置其属性,我们可以轻松地将注释添加到图表中,并使其与相应的数据点或数据区域相关联。这样,我们就能够更直观地呈现数据,并更好地传达数据的含义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值