(一)案例1:JdbcUtils @Retention(RetentionPolicy.RUNTIME) @interface DbInfo{ String driver(); String url(); String username(); String password(); } class JdbcUtils{ private static String driver; private static String url; private static String username; private static String password; static{ try { Method method = JdbcUtils.class.getMethod("getConnetion", null); DbInfo info = method.getAnnotation(DbInfo.class); driver = info.driver(); url = info.url(); username = info.username(); password = info.password(); System.out.println(driver); } catch (Exception e) { e.printStackTrace(); } } @DbInfo(driver="com.mysql.jdbc.Driver",url="",username="",password="") public static Connection getConnetion(){ return null; } }
|