Java注解类型的详细介绍与实例分析
1、自定义注解定义注解使用关键字:@interface//#1定义注解public@interfaceMyAnno1{}2、元注解用于修饰注解的注解。JDK提供的5种元注解:(1)@Target:用于确定被修饰的自定义注解使用位置(2)@Retention:用于确定被修饰的自定义注解生命周期(3)@Inherited:表示该注解具有继承性(了解)(4)@Documented:使用javadoc生成api文档时,是否包含此注解(了解)(5)@Repeatable:注解在同一个位置,只能出现一次。使用@Rep
1、自定义注解
定义注解使用关键字: @interface
// #1 定义注解
public @interface MyAnno1{
}2、元注解
用于修饰注解的注解。
JDK提供的5种元注解:
(1)@Target:用于确定被修饰的自定义注解使用位置
(2)@Retention:用于确定被修饰的自定义注解生命周期
(3)@Inherited:表示该注解具有继承性(了解)
(4)@Documented:使用 javadoc 生成 api 文档时,是否包含此注解 (了解)
(5)@Repeatable:注解在同一个位置,只能出现一次。使用@Repeatable,可以在同一个地方使用多次了。
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import anno.JDBCConfig;
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
public class DBUtil {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException, NoSuchMethodException, SecurityException {
JDBCConfig config = DBUtil.class.getAnnotation(JDBCConfig.class);
System.out.println(config);
String ip = config.ip();
int port = config.port();
String database = config.database();
String encoding = config.encoding();
String loginName = config.loginName();
String password = config.password();
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException {
Connection c = getConnection();
System.out.println(c);
}
}
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















