創建自定義注解
*/
@Target({ ElementType.METHOD, ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)
br/>@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String value();
}
通過反射獲取注解
public class Test {
@TestAnnotation(value = "測試方法")
public static void main(String args[]){
try {
Class c=Test.class;
Method[] methods=c.getDeclaredMethods();
for(Method method:methods){
Annotation[] annotations=method.getDeclaredAnnotations();
for(Annotation annotation:annotations){
TestAnnotation testAnnotation= (TestAnnotation) annotation;
System.out.println(testAnnotation.value());
}
}
} catch (ClassNotFoundException e) {e.printStackTrace();
}
}
}
四、web開發中的運用
在web開發中,權限控制非常重要,所以有些接口會限制必須登錄之后才能訪問,但是個別接口并沒有這種限制。一種方式是把需要過濾的ThinkMarkets代理申請www.kaifx.cn/broker/thinkmarkets.html接口或者方法配置在文件中,每次請求時在攔截器中根據請求的路徑與配置文件中的對比過濾。其實還有另外一種方式就是通過注解方式。
定義一個注解NoLogin
@Target(ElementType.METHOD)
br/>e.printStackTrace();
}
}
}
四、web開發中的運用
在web開發中,權限控制非常重要,所以有些接口會限制必須登錄之后才能訪問,但是個別接口并沒有這種限制。一種方式是把需要過濾的ThinkMarkets代理申請www.kaifx.cn/broker/thinkmarkets.html接口或者方法配置在文件中,每次請求時在攔截器中根據請求的路徑與配置文件中的對比過濾。其實還有另外一種方式就是通過注解方式。
定義一個注解NoLogin
@Target(ElementType.METHOD)
br/>@Documented
}
標注在方法上
在攔截器中判斷方法上是否有NoLogin注解
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//支持兩種方式過濾 1、注解方式 添加@NoLogin注解
HandlerMethod handlerMethod= (HandlerMethod) o;
NoLogin noLogin=handlerMethod.getMethod().getDeclaredAnnotation(NoLogin.class);
if(null!=noLogin){
return true;
}
}
五、java內置的注解
除了上述的四個元注解,java還內置了另外三個注解:
@Override 它沒有任何的屬性,不能存儲任何其他信息。它只能作用于方法之上,編譯結束后將被丟棄。在java編譯器編譯成字節碼的時候,一旦發現某個方法被這個注解標識過,就會匹配父類中是否存在同一方法,如果不存在就回編譯失敗。@Target(ElementType.METHOD)
br/>@Target(ElementType.METHOD)
public @interface Override {
}
@Deprecated 棄用的注解@Documented
br/>@Documented
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
@SuppressWarnings 壓制警告,比如某段代碼中存在過時的方法,那么在編譯過程中,會有warn警告,如果不想出現類似的警告,可在方法上添加這個注解。這個注解有一個value的值,這個value表示需要壓制的警告類型。
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.SOURCE)
br/>@Retention(RetentionPolicy.SOURCE)
/**
- The set of warnings that are to be suppressed by the compiler in the
- annotated element. Duplicate names are permitted. The second and
- successive occurrences of a name are ignored. The presence of
- unrecognized warning names is <i>not</i> an error: Compilers must
- ignore any warning names they do not recognize. They are, however,
- free to emit a warning if an annotation contains an unrecognized
- warning name.
-
- <p> The string {@code "unchecked"} is used to suppress
- unchecked warnings. Compiler vendors should document the
- additional warning names they support in conjunction with this
- annotation type. They are encouraged to cooperate to ensure
- that the same names work across multiple compilers.
- @return the set of warnings to be suppressed
*/
String[] value();
}
六、注解的原理
1、java.lang.annotation.Annotation中有這么一句話:The common interface extended by all annotation types 所有的注解都繼承于這個接口。怎么理解呢?其實剛才上面例子中的注解可以理解為:
public @interface TestAnnotation extends Annotation{
}
注解的本質就是一個繼承了 Annotation 接口的接口
為了方便理解和掌握注解,還是以剛才的TestAnnotation注解為例。在idea中配置啟動參數,方便查看代理產生的類,參數如下:
Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
main方法運行結束后,會在/com/sun/proxy目錄下生成一個代理類,反編譯之后是這樣的:
代理類proxy1重寫了TestAnnotation的所有方法,包括value()和從Annotation繼承來的equals()、hashCode()等方法。
package com.sun.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import org.fy.annotation.TestAnnotation;
public final class $Proxy1 extends Proxy implements TestAnnotation {
private static Method m1;
private static Method m2;
private static Method m4;
private static Method m0;
private static Method m3;
public $Proxy1(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final Class annotationType() throws {
try {
return (Class)super.h.invoke(this, m4, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final String value() throws {
try {
return (String)super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m4 = Class.forName("org.fy.annotation.TestAnnotation").getMethod("annotationType");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
m3 = Class.forName("org.fy.annotation.TestAnnotation").getMethod("value");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
在看proxy1構造函數中有一個接口InvocationHandler,這個接口的實例化對象又是誰?
java中有一個專門用于注解類型的代理對象AnnotationInvocationHandler,位于sun.reflect.annotation包中。同樣是invoke方法用于處理具體的業務。
public Object invoke(Object var1, Method var2, Object[] var3) {
String var4 = var2.getName();
Class[] var5 = var2.getParameterTypes();
if (var4.equals("equals") && var5.length == 1 && var5[0] == Object.class) {
return this.equalsImpl(var3[0]);
} else if (var5.length != 0) {
throw new AssertionError("Too many parameters for an annotation method");
} else {
byte var7 = -1;
switch(var4.hashCode()) {
case -1776922004:
if (var4.equals("toString")) {
var7 = 0;
}
break;
case 147696667:
if (var4.equals("hashCode")) {
var7 = 1;
}
break;
case 1444986633:
if (var4.equals("annotationType")) {
var7 = 2;
}
}
switch(var7) {
case 0:
return this.toStringImpl();
case 1:
return this.hashCodeImpl();
case 2:
return this.type;
default:
Object var6 = this.memberValues.get(var4);
if (var6 == null) {
throw new IncompleteAnnotationException(this.type, var4);
} else if (var6 instanceof ExceptionProxy) {
throw ((ExceptionProxy)var6).generateException();
} else {
if (var6.getClass().isArray() && Array.getLength(var6) != 0) {
var6 = this.cloneArray(var6);
}
return var6;
}
}
}
}