1. 程式人生 > >深入JAVA註解之屬性註解

深入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 }

結果(成功):