###注解
java的注解也称作元数据,可以在代码中以注解的形式添加一些信息,以便在别的地方可以获取这些信息来做一些事情,java注解是SE5引入的。java注解应用是很广泛的,例如在spring,mybatis,javax。。等框架中都有应用,注解是低入侵式的,很方便代码的维护,而且注解是受到编译器保护的,减少了代码运行时出错的可能。
我们看几个lang包的注解,都是很常见的。
@Override ,表示子类覆盖父类的方法
@Deprecated 表示被注解的元素,已经过时了,不被推荐使用了
@SuppressWarnings 关闭编译器的不适当的警告
###元注解
元注解就是专门负责注解其他注解,用于注解的定义
- 列表内容
| @Target | 表示该注解可以用在什么位置,枚举类ElementType包含所有的类型,CONSTRUCTOR:构造器的声明,FILED:字段声明,LOCAL_VARIABLE:局部变量声明,METHOD:方法法声明,PACKAGE:包声明,PARMARTER:参数声明,TYPE:类,接口,enum类型声明 |
| ---------- | — |
| Retention | 表示在什么级别保存注解信息,RetentionPolicy枚举类包含所有的类型。SOURCE:在编译后被丢弃,CLASS: 注解在class文件中可用,但会被jvm丢弃,RUNTIME:jvm将在运行期也保留注解信息,因此可以通过反射读取注解的信息(常用)|
| @Documented | 此注解将包含在Javadoc中|
|@Inherited| 允许子类继承父类的注解|
###注解的简单定义
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface AnnotationDemo {
boolean flag() default false;
int i() default 0;
String value() default "";
String[] strs() default {};
AnnotationDemoTwo annotationDemoTwo() default @AnnotationDemoTwo;
}
成员变量的类型有
- 所有基本类型(boolean,int …)
- String类型
- Class类型
- enum枚举类型
- 注解类型
- 上述类型中的数组类型
变量后边必须带括号,每个成员变量都必须要有默认值,跟在default后边。
###使用注解
定义了注解如果不定义注解处理器的话,那么注解将和注释没什么区别了使用注解需要注意,如果注解的成员变量名是value(),那么像下面的例子,在赋值的时候不需要指定key,就可以默认赋给value()变量,但是,此时只能给value()赋值,如果要同时给多个变量就需要全部指定了。
@AnnotationDemo("test")
public class AnnotationDemoTest {
String s;
public static void main(String[] args){
Class<?> c = AnnotationDemoTest.class;
AnnotationDemo ad = (AnnotationDemo) c.getDeclaredAnnotations()[0];
System.out.println(ad.value());
}
}
@AnnotationDemo(value = "test", strs={"abc","acb"})
public class AnnotationDemoTest {
String s;
public static void main(String[] args){
Class<?> c = AnnotationDemoTest.class;
AnnotationDemo ad = (AnnotationDemo) c.getDeclaredAnnotations()[0];
System.out.println(ad.value());
System.out.println(Arrays.toString(ad.strs()));
}
}
###注解小例子
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
* 表名注解
* @author sky_
*
*/
public @interface Table {
public String name() default "";
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StringType {
String name() default "";
int length() default -1;
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IntegerType {
String name() default "";
}
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.annotation.table.IntegerType;
import com.annotation.table.StringType;
import com.annotation.table.Table;
@Table(name="Student")
public class Student implements Serializable,Comparable<Student>{
/**
*
*/
private static final long serialVersionUID = 1L;
@StringType(name = "name",length=20)
private String name;
@IntegerType(name = "age")
private int age;
public Student() {
super();
}
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student(String string, Object object) {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import com.ly.entity.Student;
public class CreatSqlByAnnotation {
public static void main(String[] args){
List<String> columns = new ArrayList<String>();
Class<?> c = Student.class;
Table table = c.getAnnotation(Table.class);
if(table == null){
throw new RuntimeException("no table annotation");
}
for(Field field : c.getDeclaredFields()){
String columnName = "";
Annotation[] annotations = field.getDeclaredAnnotations();
if(annotations.length < 1){
continue;
}
for(Annotation annotation:annotations){
if(annotation instanceof IntegerType){
IntegerType IntegerType = (IntegerType) annotation;
if(IntegerType.name().length()<1){
columnName = field.getName().toUpperCase();
}else{
columnName = IntegerType.name();
}
columnName+=" INT";
columns.add(columnName );
}
if(annotation instanceof StringType){
StringType StringType = (StringType) annotation;
int length = StringType.length();
if(StringType.name().length()<1){
columnName = field.getName().toUpperCase();
}else{
columnName = StringType.name();
}
columnName += " varchar2"+ (length == -1 ? "":"("+length+")") ;
columns.add(columnName);
}
}
}
StringBuffer stringBuffer = new StringBuffer("create table "+table.name() + "(");
for(String columnStr : columns){
stringBuffer.append("\n " + columnStr +",");
}
String sqlCommand = stringBuffer.toString().substring(0,stringBuffer.toString().length()-1)+"\n )";
System.out.println(sqlCommand);
}
}
关于注解的使用建议大家多去看看spring等开源框架的使用,利用注解解决问题,可以写出更优雅的代码来。