深入JAVA注解之方法注解
生活随笔
收集整理的這篇文章主要介紹了
深入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 }測試結果:成功
?
總結
以上是生活随笔為你收集整理的深入JAVA注解之方法注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MySQL经典案例分析】关于数据行溢出
- 下一篇: 三个定理