注解基础
注解的本质,就是一个配置文件。
package ttt;
import java.lang.annotation.*;
@Retention(value=RetentionPolicy.RUNTIME)
@Target(value ={ElementType.METHOD})
public @interface MyMapping {
String path();
String method();
}
------------------
package ttt;
public class XXXController {
@MyMapping(path = "/order", method = "GET")
public String getOrder() {
System.out.println("getOrder()");
return "{}";
}
}
------------------
package ttt;
import java.lang.reflect.Method;
public class TestAnnotations {
public static void main(String[] args) throws Exception {
Method[] methods =Class.forName("ttt.XXXController").getDeclaredMethods();
for (Method method : methods) {
System.out.println("method: " + method);
MyMapping myMapping = method.getAnnotation(MyMapping.class);
System.out.println("path:" + myMapping.path() + " method:" + myMapping.method());
}
}
}
---------打印结果---------
method: public java.lang.String ttt.XXXController.getOrder()
path:/order method:GET
例子2
package ttt;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value=RetentionPolicy.RUNTIME)
@Target(value ={ElementType.METHOD})
public @interface Test_Value {
String value();
}
---------------
package ttt;
import java.lang.reflect.Method;
/**
* @Inherited 允许子类继承父类中的注解
* 但如果子类覆盖了父类的方法,则这个方法的注解无法继承
* 测试效果如下
*/
public class TestInherited {
public static void test(String classname) throws Exception {
//getDeclaredMethod获取类自身的public、private、以及interface中的方法。不包括父类的public方法。
//getMethod获取类的所有public方法,包括继承来的public方法
Method[] methods =Class.forName(classname).getDeclaredMethods();
for (Method method : methods) {
System.out.println(classname + " method: " + method);
Test_Value test_Value = method.getAnnotation(Test_Value.class);
if(test_Value==null) {
System.out.println("test_Value==null");
}else {
System.out.println("path:" + test_Value.value());
}
}
}
public static void main(String[] args) throws Exception {
test("ttt.A");
test("ttt.B");
}
}
class A{
@Test_Value("f1")
public void f1() {
}
@Test_Value("f2")
public void f2() {
}
}
class B extends A{
@Override
public void f2() {}
}
---------------打印结果----------
ttt.A method: public void ttt.A.f2()
path:f2
ttt.A method: public void ttt.A.f1()
path:f1
ttt.B method: public void ttt.B.f2()
test_Value==null
3、可以借助反射库
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
4、
Value属性,可以省略属性名,直接这样写 @ABC(“xxx”),等价于@ABC(value=“xxx”)
所以有的注解就设计了两个功能相同的属性,比如value prefix
/** * * Retention(中文意思是‘保留’)注解说明,这种类型的注解会被保留到那个阶段. 有三个值: * 1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略 * 2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略 * 3.RetentionPolicy.RUNTIME —— * 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用. * * ** @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: *ElemenetType.CONSTRUCTOR 构造器声明 *ElemenetType.FIELD 域声明(包括 enum 实例) ElemenetType.LOCAL_VARIABLE 局部变量声明 *ElemenetType.METHOD 方法声明 ElemenetType.PACKAGE 包声明 *ElemenetType.PARAMETER 参数声明 *ElemenetType.TYPE 类,接口(包括注解类型)或enum声明 */ |
5、注解是可以组合的,例如springboot的这个注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {