深入JAVA注解之方法注解


以获取数据库连接为例,建立maven项目

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>org.guangsoft</groupId>
 6     <artifactId>annotation</artifactId>
 7     <version>1.0</version>
 8     <packaging>war</packaging>
 9     <name>annotation</name>
10     <dependencies>
11         <dependency>
12             <groupId>com.mchange</groupId>
13             <artifactId>mchange-commons-java</artifactId>
14             <version>0.2.9</version>
15         </dependency>
16         <dependency>
17             <groupId>com.mchange</groupId>
18             <artifactId>c3p0</artifactId>
19             <version>0.9.5</version>
20         </dependency>
21         <dependency>
22             <groupId>mysql</groupId>
23             <artifactId>mysql-connector-java</artifactId>
24             <version>8.0.11</version>
25         </dependency>
26     </dependencies>
27 </project>

建立注解类

 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(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     private ComboPooledDataSource dataSource; 10     
11  @DBInfo 12     public void setDataSource(ComboPooledDataSource dataSource) { 13         this.dataSource = dataSource; 14  } 15     
16     public ComboPooledDataSource getDataSource() { 17         return dataSource; 18  } 19     
20 }

建立工厂类

 1 package org.guangsoft.annotation.utils;  2 
 3 import java.beans.BeanInfo;  4 import java.beans.Introspector;  5 import java.beans.PropertyDescriptor;  6 import java.lang.reflect.Method;  7 
 8 import javax.sql.DataSource;  9 
10 import org.guangsoft.annotation.dao.CommDAO; 11 import org.guangsoft.annotation.entity.DBInfo; 12 
13 public class JDBCUtil2 { 14     
15     //将注解注入到数据源类
16     private static DataSource getDataSourceByDBInfo (DBInfo dbInfo, DataSource dataSource) { 17         Method[] methods = DBInfo.class.getMethods(); 18         for(Method method : methods) { 19             String name = method.getName(); 20             try { 21                 //注解类没有属性,反射注解类的方法名,内省出数据源类设置参数的方法, 找不到会抛异常,进入下次循环
22                 PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, dataSource.getClass()); 23                 //注解类的方法能得到实际注解的值
24                 Object value = method.invoke(dbInfo, null); 25                 //用数据源的方法将注解类的值注入
26  propertyDescriptor.getWriteMethod().invoke(dataSource, value); 27             } catch(Exception e) { 28                 continue; 29  } 30  } 31         return dataSource; 32  } 33     
34     //工厂模式下的创建DAO
35     public static CommDAO createCommDAO() { 36         CommDAO commDAO = new CommDAO(); 37         try { 38             BeanInfo beanInfo = Introspector.getBeanInfo(commDAO.getClass(), Object.class); 39             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 40             if(propertyDescriptors != null) { 41                 for(PropertyDescriptor propertyDescriptor : propertyDescriptors) { 42                     Method setMethod = propertyDescriptor.getWriteMethod(); 43                     DBInfo dbInfo = setMethod.getAnnotation(DBInfo.class); 44                     if(dbInfo != null) { 45                         //获取dao中dataSource的实体类ComboPooledDataSource
46                         Class dataSourceClass = propertyDescriptor.getPropertyType(); 47                         DataSource dataSource = (DataSource) dataSourceClass.newInstance(); 48                         dataSource = getDataSourceByDBInfo(dbInfo, dataSource); 49  setMethod.invoke(commDAO, dataSource); 50  } 51  } 52  } 53         }catch(Exception e) { 54  e.printStackTrace(); 55  } 56         return commDAO; 57  } 58     
59 }

建立测试类

 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.JDBCUtil2;  9 
10 public class CommService { 11     
12     public static void main(String args[]) throws Exception { 13         CommDAO commDAO = JDBCUtil2.createCommDAO(); 14         DataSource dataSource = commDAO.getDataSource(); 15         Connection connection = dataSource.getConnection(); 16  System.out.println(connection); 17  } 18     
19 }

测试结果:成功

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM