注解详解
注释与注解
注释:解释代码 面向程序员
注解:对代码级别的说明 面向程序
概念
概念:它是JDK1.5及以后版本引入的一个特性 与类 接口 枚举 是在同一层次
它可以声明在包 类 字段 方法 局部变量 方法参数等的前面 用来对这些元素进行说明 注释
作用
作用:一个用来检测 一个是用来替代配置文件 简化配置文件的书写
常见的注解
常见的注解:
@Override 用来检测这个方法 是不是重写父类的方法
@FunctionalInterface 用来检测该接口是不是函数式接口
@Deprecated 标注过时
@WebServlet(value="/path") 用来替代配置文件
@SuppressWarnings(value = "all") 抑制黄色警告
自定义注解
自定义注解:@interfac
注解的使用 @注解名
注解本质上是一个接口
接口里面可以定义抽象方法
注解中定义的抽象方法 叫做注解的属性
一旦在注解中 定义了属性 那么在使用注解时 必须给注解的所有属性赋值
定义属性可以给默认值
public @interface MyZhuJie {
String username();
String password();
int num() default 1;
}
public class Student {
@MyZhuJie(username = "fff",password = "jjj")
private String name;
}
注解支持的数据类型
注解的属性支持以下数据类型
所有的基本数据类型 和String类型
所有的基本数据类型 和String类型的数组类型
Class
注解类型:枚举类型 Enum
特殊属性 value()
如果只有一个属性 value 可以省略
如果有多个属性 不可以省略
数组类型的注解赋值
{}里面进行赋值
如果只有一个值 可以省略{}
public @interface TeShu {
String aa();
String value();
String[] arr();
int[] hu();
}
public @interface SanWa {
String san();
int num();
int[] cc();
double c();
enum EE{};
}
enum EE{}
@TeShu(aa = "f",value = "a",arr = {"ff","koko"},hu = 333)
public class ShuZu {
}
元注解
概念
限定自定义注解的注解
@Target 限定自定义注解 可以用在什么上面
@Retention 限定自定义注解保留到什么时候
举例
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Frr {
}
@Frr
public class Frrr {
private String anme;
@Frr
public void sh(){}
}
案例
JDBCUtils
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JDBC {
//四个属性
String driverClass();
String url();
String username () default "root";
String password () default "131427";
}
import com.alibaba.druid.util.JdbcUtils;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
//获取连接对象
@JDBC(driverClass = "com.mysql.jdbc.Driver", url = "jdbc:mysql://localhost:3309/loginxx", username = "root", password = "131427")
public static Connection getConnection() {
Method method = null;
try {
method = JDBCUtils.class.getMethod("getConnection");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Connection connection =null;
//判断该方法上是否加了注解
if(method.isAnnotationPresent(JDBC.class)){
JDBC annotation = method.getAnnotation(JDBC.class);
String s = annotation.driverClass();
String url = annotation.url();
String username = annotation.username();
String password = annotation.password();
try {
Class.forName(s);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, username, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return connection;
}
}
import java.sql.Connection;
public class TestUtils {
public static void main(String[] args) {
Connection connection = JDBCUtils.getConnection();
System.out.println(connection);
}
}
运行结果:
com.mysql.jdbc.JDBC4Connection@587d1d39
web的注解
一个Servlet 可以有多个映射路径
一个映射路径只能对应一个Servlet