欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Annotation(Java 注解)的实现代码

程序员文章站 2023-12-15 10:34:16
如果你想知道java annotation是什么?你可以先看看:“http://www.infoq.com/articles/annotation-hammer” 下面是...

如果你想知道java annotation是什么?你可以先看看:“http://www.infoq.com/articles/annotation-hammer

下面是我做的一个demo:

项目结构:

Java Annotation(Java 注解)的实现代码

运行效果:

Java Annotation(Java 注解)的实现代码

====================================================

代码部分:

注:很多人会考虑这个问题,“这样做的目的是什么?我们可以做一个配置文件(xml,properties等),不是比这个跟方便...或者说

直接把我们的配置信息写入程序...这样也不会去解析我们写的注释..”

但是annotation和xml,properties等配置文件的优缺点是什么呢..

个人观点:写注释的时候,比较方便...可以提高开发的效率.有用到注释的框架,如:hibernate,struts,spring等

回到原话题,“这样做的目的是什么?“---这里只是做一个demo,让大家知道annotation是怎么一回事儿....在很多我们开发的

过程中,很少用到我们自己定义的注释(annotation),如果真的用到了,那么这篇blog也许就有帮助了..^_^

====================================================

/java_annotation/src/com/b510/hongten/annotation/jdbcannotation.java

复制代码 代码如下:

/**
  *
  */
 package com.b510.hongten.annotation;

 import java.lang.annotation.documented;
 import java.lang.annotation.elementtype;
 import java.lang.annotation.retention;
 import java.lang.annotation.retentionpolicy;
 import java.lang.annotation.target;

 /**
  * jdbc annotation
  *
  * @author hongten
  * @date 2013-4-10
  */
 @documented
 @retention(retentionpolicy.runtime)
 @target(elementtype.type)
 public @interface jdbcannotation {

     string driver() default "com.mysql.jdbc.driver";

     string dbname() default "";

     string encoding() default "utf-8";

     string port() default "3306";

     string host() default "localhost";

     string username() default "root";

     string password() default "";

 }

/java_annotation/src/com/b510/hongten/jdbc/jdbcutil.java
复制代码 代码如下:

/**
  *
  */
 package com.b510.hongten.jdbc;

 import com.b510.hongten.annotation.jdbcannotation;

 /**
  * @author hongten
  * @date 2013-4-12
  */
 @jdbcannotation(dbname = "db_lucene", port = "3306", host = "192.168.0.119", username = "root", password = "root")
 public class jdbcutil {

     private static string driver;
     private static string dbname;
     private static string encoding;
     private static string port;
     private static string host;
     private static string passwrod;
     private static string username;
     private static string url;

     public void checkinterceptor(class<?> cl) throws exception {
         boolean flag = cl.isannotationpresent(jdbcannotation.class);
         if (flag) {
             jdbcannotation jdbcannotation = cl.getannotation(jdbcannotation.class);
             driver = jdbcannotation.driver();
             dbname = jdbcannotation.dbname();
             encoding = jdbcannotation.encoding();
             port = jdbcannotation.port();
             host = jdbcannotation.host();
             username = jdbcannotation.username();
             passwrod = jdbcannotation.password();
             url = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?characterencoding=" + encoding;
             system.out.println("jdbcutil加载注释完成...");
         }
     }

     public jdbcutil() {
         try {
             checkinterceptor(jdbcutil.class);
         } catch (exception e) {
             e.printstacktrace();
         }
     }

     public static string getdriver() {
         return driver;
     }

     public static void setdriver(string driver) {
         jdbcutil.driver = driver;
     }

     public static string getdbname() {
         return dbname;
     }

     public static void setdbname(string dbname) {
         jdbcutil.dbname = dbname;
     }

     public static string getencoding() {
         return encoding;
     }

     public static void setencoding(string encoding) {
         jdbcutil.encoding = encoding;
     }

     public static string getport() {
         return port;
     }

     public static void setport(string port) {
         jdbcutil.port = port;
     }

     public static string gethost() {
         return host;
     }

     public static void sethost(string host) {
         jdbcutil.host = host;
     }

     public static string getpasswrod() {
         return passwrod;
     }

     public static void setpasswrod(string passwrod) {
         jdbcutil.passwrod = passwrod;
     }

     public static string getusername() {
         return username;
     }

     public static void setusername(string username) {
         jdbcutil.username = username;
     }

     public static string geturl() {
         return url;
     }

     public static void seturl(string url) {
         jdbcutil.url = url;
     }

    
 }

/java_annotation/src/com/b510/hongten/jdbc/jdbctest.java
复制代码 代码如下:

/**
  *
  */
 package com.b510.hongten.jdbc;

 import java.sql.connection;
 import java.sql.drivermanager;
 import java.sql.preparedstatement;
 import java.sql.resultset;
 import java.sql.sqlexception;

 /**
  *
  * @author hongten</br>
  * @date 2012-7-16
  *
  */
 public class jdbctest {
     @suppresswarnings("static-access")
     public static void main(string[] args) {
         jdbcutil jdbcutil = new jdbcutil();
         string sql = "select * from mymails";
         try {
             class.forname(jdbcutil.getdriver());
             connection conn = drivermanager.getconnection(jdbcutil.geturl(), jdbcutil.getusername(), jdbcutil.getpasswrod());
             preparedstatement ps = conn.preparestatement(sql);
             resultset rs = ps.executequery();
             while (rs.next()) {
                 system.out.println("id : " + rs.getint(1) + " name : " + rs.getstring(2) + " mail : " + rs.getstring(3));
             }
             // 关闭记录集
             if (rs != null) {
                 try {
                     rs.close();
                 } catch (sqlexception e) {
                     e.printstacktrace();
                 }
             }

             // 关闭声明
             if (ps != null) {
                 try {
                     ps.close();
                 } catch (sqlexception e) {
                     e.printstacktrace();
                 }
             }

             // 关闭链接对象
             if (conn != null) {
                 try {
                     conn.close();
                 } catch (sqlexception e) {
                     e.printstacktrace();
                 }
             }
         } catch (exception e) {
             e.printstacktrace();
         }
     }

 }

上一篇:

下一篇: