Java Annotation of Java Annotation implementation code

  • 2020-04-01 01:49:48
  • OfStack

If you want to know what Java annotation is, right? You can see: "(the link: http://www.infoq.com/articles/Annotation-Hammer)"

Here's a demo I did:

Project structure:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050215523931.png "width = 349 height = 223 >

Operation effect:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050215523932.png "width = 589 height = 152 >

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

Code part:

Note: many people consider the question, "what is the purpose of this? We can make a configuration file (XML, properties, etc.) that is not more convenient than this... Or,

Write our configuration information directly to the program... It won't parse our notes..."

But what are the pros and cons of annotations versus configuration files like XML, properties, etc..

Personal opinion: when writing comments, it's more convenient... Can improve the efficiency of development. Useful to annotate the framework, such as: Hibernate, Struts, Spring, etc

Going back to the original topic, "what's the purpose of this? "-- this is just a demo to show you how annotations work... In a lot of what we developed

We rarely use our own annotations in the process, and if we do, this blog post might be helpful.. ^_^

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

/ 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;

 
 @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;

 
 @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 Load comment complete ...");
         }
     }

     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));
             }
             //Close recordset
             if (rs != null) {
                 try {
                     rs.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }

             //Closing statement
             if (ps != null) {
                 try {
                     ps.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }

             //Close the link object
             if (conn != null) {
                 try {
                     conn.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

 }


Related articles: