深入JAVA注解之方法注解
2023-09-11 14:22:18 时间
以获取数据库连接为例,建立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高级用法之:在JNA中将本地方法映射到JAVA代码中
- Java VS .NET:Java与.NET的特点对比 单点登录(SSO)的设计
- Win10 JAVA安装及环境搭建(windows jdk,windows java环境配置)
- CSDN日报191016:Java纯干货分享:史上最全的JAVA工程师面试题汇总
- 2022年目前最新上千道Java面试题,刷完你不进大厂谁进大厂
- memcached java client
- 【Java】【TestNg】
- 【java】+对excel进行操作_1_
- java四种内部类详解
- Java学习-050-AES256 之 java.security.InvalidKeyException: Illegal key size or default parameters 解决方法
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果集 java.lang.OutOfMemoryError: Java heap space
- Java_jdbc 基础笔记之九 数据库连接 (查询Customer对象的方法)
- Java反射基础知识笔记:反射的定义、class类的本质、class类的动态加载、class类的实例如何访问字段/方法/构造方法/继承关系、动态代理的本质
- Java设计模式之创建型:工厂模式详解(简单工厂+工厂方法+抽象工厂)
- JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析
- 一些关于Java的琐碎知识点
- Java 古堡算式
- Java基于Socket文件传输示例
- Java中Integer类的方法
- 一个资深java面试官的“面试心得”
- Java小白入门200例64之Java复制(拷贝)数组的几种方法