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