元注解也是一个注解: 修饰注解的 注解.
自定义一个注解:
核心运行类:
- public class CoreRunner {public static void main(String[] args) {
- // 反射: 获得类的字节码对象. Class
- Class clazz = AnnotationDemo3.class;
- // 获得测试类中的所有的方法:
- Method[] methods = clazz.getMethods();
- // 遍历数组:
- for (Method method : methods) {
- // System.out.println(method.getName());
- // 判断方法上是否有 @MyTest 注解:
- boolean flag = method.isAnnotationPresent(MyTest.class);
- // System.out.println(method.getName()+" "+flag);
- if(flag){
- // 让这个方法执行:
- try {
- method.invoke(clazz.newInstance(), null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
1.2.4 总结:
使用注解完成 JDBC 工具类的编写:(了解)
- public class JDBCUtils {
- private static String driverClass;
- private static String url;
- private static String username;
- private static String password;
来源: http://www.bubuko.com/infodetail-2631607.html