自定义注解
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Repeatable(MyAnnotions.class)//需要使用重复注解指定容器
@Target(value={java.lang.annotation.ElementType.TYPE,java.lang.annotation.ElementType.FIELD,java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.PARAMETER,java.lang.annotation.ElementType.CONSTRUCTOR,java.lang.annotation.ElementType.LOCAL_VARIABLE,java.lang.annotation.ElementType.TYPE_PARAMETER})
//目标 TYPE_PARAMETER 类型注解
@Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)//生命周期
public @interface MyAnnotation {
String value()default "test01";
}
自定义注解如果不太明白的可以看我博客中的另一文章元注解
重复注解需要的容器类
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
//重复注解需要的容器
@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})//目标
@Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)//生命周期
public @interface MyAnnotions {
MyAnnotation[] value();
}
测试类
//重复注解和类型注解
public class TestAnnotion {
@MyAnnotation("hello")
@MyAnnotation("java")
public void show(@MyAnnotation("A") String a){
}
@Test
public void test01() throws Exception{
Class<TestAnnotion> clazz= TestAnnotion.class;
Method[] me=clazz.getMethods();
for (Method method : me) {
MyAnnotation[] annotationsByType = method.getAnnotationsByType(MyAnnotation.class);
for (MyAnnotation myAnnotation : annotationsByType) {
System.out.println(myAnnotation.value());//输出hello java
}
}
}
}
总结:差不多2周的时间初步学习了java的新特性,因为项目上刚好使用的时1.8,便找时间学习了下,在实际开发中
也多少应用了一部分,确实 方便了很多,只是犹豫有的语法或函数什么的记得不是很牢固,使用起来也不是特别熟练,不过多多使用,最后肯定就会和你写sql一样的熟练,真实体验:java8-牛逼~。少写了很多代码,少了好多对象。。。。
最后,感谢尚硅谷的视频,比起之前看别人的博客,讲的很清楚,循序渐进。感谢。
尚硅谷java8 密码:su94