深入JAVA注解之屬性注解


項目目錄結構

實體類:

 1 package org.guangsoft.annotation.entity;  2 
 3 import java.lang.annotation.ElementType;  4 import java.lang.annotation.Retention;  5 import java.lang.annotation.RetentionPolicy;  6 import java.lang.annotation.Target;  7 
 8 @Target(value = {ElementType.FIELD, ElementType.METHOD})  9 @Retention(RetentionPolicy.RUNTIME) 10 public @interface DBInfo { 11     //屬性名參考com.mchange.v2.c3p0.DriverManagerDataSource
12     String driverClass() default "com.mysql.jdbc.Driver"; 13     String jdbcUrl() default "jdbc:mysql://192.168.50.30:3306/guanghe"; 14     String user() default "root"; 15     String password() default "root"; 16 }

DAO層:

 1 package org.guangsoft.annotation.dao;  2 
 3 import org.guangsoft.annotation.entity.DBInfo;  4 
 5 import com.mchange.v2.c3p0.ComboPooledDataSource;  6 
 7 public class CommDAO {  8 
 9  @DBInfo 10     private ComboPooledDataSource dataSource; 11     
12     public void setDataSource(ComboPooledDataSource dataSource) { 13         this.dataSource = dataSource; 14  } 15     
16     public ComboPooledDataSource getDataSource() { 17         return dataSource; 18  } 19     
20 }

service層:

 1 package org.guangsoft.annotation.service;  2 
 3 import java.sql.Connection;  4 
 5 import javax.sql.DataSource;  6 
 7 import org.guangsoft.annotation.dao.CommDAO;  8 import org.guangsoft.annotation.utils.JDBCUtil3;  9 
10 public class CommService { 11     
12     public static void main(String args[]) throws Exception { 13         CommDAO commDAO = JDBCUtil3.createCommDAO(); 14         DataSource dataSource = commDAO.getDataSource(); 15         Connection connection = dataSource.getConnection(); 16  System.out.println(connection); 17  } 18     
19 }

util類:

 1 package org.guangsoft.annotation.utils;  2 
 3 import java.beans.PropertyDescriptor;  4 import java.lang.reflect.Field;  5 import java.lang.reflect.Method;  6 
 7 import javax.sql.DataSource;  8 
 9 import org.guangsoft.annotation.dao.CommDAO; 10 import org.guangsoft.annotation.entity.DBInfo; 11 
12 public class JDBCUtil3 { 13     
14     //將注解注入到數據源類
15     private static DataSource getDataSourceByDBInfo (DBInfo dbInfo, DataSource dataSource) { 16         Method[] methods = DBInfo.class.getMethods(); 17         for(Method method : methods) { 18             String name = method.getName(); 19             try { 20                 //注解類沒有屬性,反射注解類的方法名,內省出數據源類設置參數的方法, 找不到會拋異常,進入下次循環
21                 PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, dataSource.getClass()); 22                 //注解類的方法能得到實際注解的值
23                 Object value = method.invoke(dbInfo, null); 24                 //用數據源的方法將注解類的值注入
25  propertyDescriptor.getWriteMethod().invoke(dataSource, value); 26             } catch(Exception e) { 27                 continue; 28  } 29  } 30         return dataSource; 31  } 32     
33     //工廠模式下的創建DAO
34     public static CommDAO createCommDAO() { 35         CommDAO commDAO = new CommDAO(); 36         try { 37             Field[] fields = commDAO.getClass().getDeclaredFields(); 38             if(fields != null) { 39                 for(Field field : fields) { 40                     field.setAccessible(true); 41                     DBInfo dbInfo = field.getAnnotation(DBInfo.class); 42                     if(dbInfo != null) { 43                         //獲取dao中dataSource的實體類ComboPooledDataSource
44                         DataSource dataSource = (DataSource) field.getType().newInstance(); 45                         dataSource = getDataSourceByDBInfo(dbInfo, dataSource); 46  field.set(commDAO, dataSource); 47  } 48  } 49  } 50         }catch(Exception e) { 51  e.printStackTrace(); 52  } 53         return commDAO; 54  } 55     
56 }

結果(成功):


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM