> 經過上個知識點自定義注解的學習之后,對自定義注解有了感性的認識,現在我們再來稍微深入了解各種元注解以及他們的含義。
# 元注解概念
在講解元注解概念之前,我們先建立元數據的概念。 元數據在英語中對應單詞 metadata, metadata在wiki中的解釋是:
Metadata is data [information] that provides information about other data
為其他數據提供信息的數據
這樣元注解就好理解了,元注解 meta annotation用于注解 **自定義注解** 的注解。
元注解有這么幾種:
```
@Target
@Retention
@Inherited
@Documented
@Repeatable (java1.8 新增)
```
接下來挨個講解
# @Target
@Target 表示這個注解能放在什么位置上,是只能放在類上?還是即可以放在方法上,又可以放在屬性上。
**自定義注解@JDBCConfig** 這個注解上的@Target是:@Target({METHOD,TYPE}),表示他可以用在方法和類型上(類和接口),但是不能放在屬性等其他位置。 可以選擇的位置列表如下:
```
ElementType.TYPE:能修飾類、接口或枚舉類型
ElementType.FIELD:能修飾成員變量
ElementType.METHOD:能修飾方法
ElementType.PARAMETER:能修飾參數
ElementType.CONSTRUCTOR:能修飾構造器
ElementType.LOCAL_VARIABLE:能修飾局部變量
ElementType.ANNOTATION_TYPE:能修飾注解
ElementType.PACKAGE:能修飾包
```
```
package annotation;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ METHOD, TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface JDBCConfig {
String ip();
int port() default 3306;
String database();
String encoding();
String loginName();
String password();
}
```
# @Retention
@Retention 表示生命周期,自定義注解@JDBCConfig 上的值是 RetentionPolicy.RUNTIME, 表示可以在運行的時候依然可以使用。 @Retention可選的值有3個:
* `RetentionPolicy.SOURCE`: 注解只在源代碼中存在,編譯成class之后,就沒了。**@Override** 就是這種注解。
* `RetentionPolicy.CLASS`: 注解在java文件編程成.class文件后,依然存在,但是運行起來后就沒了。@Retention的默認值,即當沒有顯式指定@Retention的時候,就會是這種類型。
* `RetentionPolicy.RUNTIME`: 注解在運行起來之后依然存在,程序可以通過反射獲取這些信息,**自定義注解@JDBCConfig** 就是這樣。
大家可以試試把自定義注解@JDBCConfig的@Retention改成其他兩種,并且運行起來,看看有什么不同
# @Inherited
@Inherited 表示該注解具有繼承性。如例,設計一個DBUtil的子類,其getConnection2方法,可以獲取到父類DBUtil上的注解信息。
```
package com.dodoke.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import annotation.JDBCConfig;
public class DBUtilChild extends DBUtil2 {
public static Connection getConnection2() throws SQLException, NoSuchMethodException, SecurityException {
JDBCConfig config = DBUtilChild.class.getAnnotation(JDBCConfig.class);
String ip = config.ip();
int port = config.port();
String database = config.database();
String encoding = config.encoding();
String loginName = config.loginName();
String password = config.password();
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException {
Connection c = getConnection2();
System.out.println(c);
}
}
```
# @Documented
@Documented 如圖所示, 在用javadoc命令生成API文檔后,DBUtil的文檔里會出現該注解說明。
注: 使用eclipse把項目中的.java文件導成API文檔步驟:
1. 選中項目
2. 點開菜單File
3. 點擊Export
4. 點開java->javadoc->點next
5. 點finish

# @Repeatable (java1.8 新增)
當沒有@Repeatable修飾的時候,注解在同一個位置,只能出現一次,如例所示:
```
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
```
重復做兩次就會報錯了。
使用@Repeatable之后,再配合一些其他動作,就可以在同一個地方使用多次了。
```
package com.dodoke.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import annotation.JDBCConfig;
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
public class DBUtil3 {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException, NoSuchMethodException, SecurityException {
JDBCConfig config = DBUtil3.class.getAnnotation(JDBCConfig.class);
String ip = config.ip();
int port = config.port();
String database = config.database();
String encoding = config.encoding();
String loginName = config.loginName();
String password = config.password();
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException {
Connection c = getConnection();
System.out.println(c);
}
}
```
# @Repeatable 運用舉例
查找文件內容:
假設你的項目目錄是 e:/project,遍歷這個目錄下所有的文件(包括子文件夾),使用@Repeatable 這個元注解來表示,文件后綴名的范圍可以是java, html, css, js 等等。
為了緊湊起見,把注解作為內部類的形式放在一個文件里。
1. 注解FileTypes,其value()返回一個FileType數組
2. 注解FileType,其@Repeatable的值采用FileTypes
3. 運用注解:在work方法上重復使用多次@FileType注解
4. 解析注解: 在work方法內,通過反射獲取到本方法上的FileType類型的注解數組,然后遍歷本數組
```
package annotation;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class FindFiles {
@Target( METHOD)
@Retention( RetentionPolicy.RUNTIME )
public @interface FileTypes {
FileType[] value();
}
@Target( METHOD )
@Retention( RetentionPolicy.RUNTIME )
@Repeatable( FileTypes.class )
public @interface FileType {
String value();
};
@FileType( ".java" )
@FileType( ".html" )
@FileType( ".css" )
@FileType( ".js" )
public void work(){
try {
FileType[] fileTypes= this.getClass().getMethod("work").getAnnotationsByType(FileType.class);
System.out.println("將從如下后綴名的文件中查找文件內容");
for (FileType fileType : fileTypes) {
System.out.println(fileType.value());
}
System.out.println("查找過程略。。。");
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new FindFiles().work();
}
}
```