1. 程式人生 > >【Java編程思想筆記】註解--自定義註解

【Java編程思想筆記】註解--自定義註解

password admin lamp sta 如果 amp 部分 oot nbsp

文章參考自:https://www.cnblogs.com/xdp-gacl/p/3622275.html

學習網站:how2java.cn

一、自定義註解的創建過程

第一步:(元註解) 使用元註解對註解類進行相關約束,如@Target定義作用目標,@Retention定義生命周期,@Inherited等等註解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation {
}

第二步: (添加屬性) 為註解添加屬性,當使用註解時,為這些註解賦值,並在註解的類型中使用

語法是: 類型 屬性名();

public @interface MyAnnotation {
    String name();
    int id();
    boolean isCool();
}
@MyAnnotation(name="Right",id=1,isCool = false)
class MyUse{
//通過反射獲得註解中的數據
MyAnnotation ma = MyUse.class.getAnnotation(MyAnnotation.class);
String name = ma.name();
int id = ma.id();
boolean isCool = ma.isCool();
}

第三步:(默認值) 這一步視需要而定,主要是為某些屬性在註解時如果沒有賦值,就指定默認值

語法: 類型 屬性名() default 默認值;

public @interface MyAnnotation {
    String name();
    int id();
    boolean isCool() default true;
}

@MyAnnotation(name
="Right",id=1) class MyUse{ MyAnnotation ma = MyUse.class.getAnnotation(MyAnnotation.class); String name = ma.name(); int id = ma.id(); boolean isCool = ma.isCool(); }

第四步:(value屬性) 如果你只想設置一個value值,或者除了value值外,其他值都有默認值,那麽可以省略掉“value=”的部分

public @interface MyAnnotation {
    String value();
    boolean isCool() default true;
}

@MyAnnotation("Right")
class MyUse{
    MyAnnotation ma = MyUse.class.getAnnotation(MyAnnotation.class);
    String value = ma.value();
    boolean isCool = ma.isCool();
}

第五步:(高級屬性) 你可以向註解中添加更多種屬性,如數組,枚舉等

1.數組:

public @interface MyAnnotation {
    int[] a() default {1,2,4};
    int[] b();
}

@MyAnnotation(a={2,3,4},b=2)
class MyUse{
    MyAnnotation ma = MyUse.class.getAnnotation(MyAnnotation.class);
    int[] a = ma.a();
    int[] b = ma.b();
}

2.枚舉:

public @interface MyAnnotation {
    EumTrafficLamp lamp() default EumTrafficLamp.RED;
}

enum EumTrafficLamp{
    RED,//
    YELLOW,//
    GREEN,//
}

@MyAnnotation(lamp = EumTrafficLamp.GREEN)
class MyUse{
    MyAnnotation ma = MyUse.class.getAnnotation(MyAnnotation.class);
    EumTrafficLamp lamp = ma.lamp();
}

3.註解類型 : 實際上這個代碼是運行不了的,應當將MetaAnnotaion創建一個類文件,再在這裏面引用,這裏為了好理解所以放在了一起

public @interface MyAnnotation {
    MetaAnnotation metadata() default @MetaAnnotation("Wrong")
}

@interface MetaAnnotation{
    String value();
}

@MyAnnotation()
class MyUse{
    MyAnnotation ma = MyUse.class.getAnnotation(MyAnnotation.class);
//註意,這裏調用metadata()返回的是一個MetaAnnotation對象,再使用對象獲取其中的值 MetaAnnotation metadata
= ma.metadata(); String value = metadata.value(); }

測試用例:

一般在不使用框架的時候,我們使用JDBC需要自己進行創建連接,定義參數之類的操作,在不使用註解的情況下,會導致如果想要修改連接的數據庫,就需要更改代碼內容。

代碼如下:

public class DBUtil {
    static String ip = "127.0.0.1";
    static int port = 3306;
    static String database = "test";
    static String encoding = "UTF-8";
    static String loginName = "root";
    static String password = "admin";

    static{
        try{
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s",ip,port,database,encoding);
        return DriverManager.getConnection(url,loginName,password);
    }
    
}

而如果我們自定義註解,將需要的信息放入註解當中,就可以動態地更改數據庫的連接信息,而不需要修改代碼內容

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JDBCConfig {
    String ip();
    int port() default 3306;
    String database();
    String encoding();
    String loginName();
    String password();
}
@JDBCConfig(ip="127.0.0.1",database = "test",encoding = "UTF-8",loginName = "root",password = "admin")
public class DBUtil {
    static{
        try{
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        //從JDBCConfig中獲取信息
        JDBCConfig config = DBUtil.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);
    }

}

【Java編程思想筆記】註解--自定義註解