【Java编程思想笔记】注解--自定义注解
文章参考自: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); } }