注解Annotation
简介
注解Annotation,又称为java标注,从JDK5.0开始被支持。用来修饰类,属性,方法,参数,局部变量等等,在编译,类加载和运行时被读取并做相应处理。
四大元注解
@Target(必须)
标识当前注解在什么地方使用
属性ElementType

@Retention(必须
标识当前注解使用的级别(作用级别)
属性RetentionPolicy保存策略

@Documented(可选)
当前项目导出成java-doc文档时带不带当前注解
@Inherited(可选)
标识当前注解是不是可以被子类直接继承使用
自定义注解实战
自定义类注解
需求
模仿spring框架的@Controller @RestController 在类上使用
代码
注解
package com.aaa.annotation.class1;
import java.lang.annotation.*;
/**
* @FileName: Controller
* @Description: 自定义类注解
* @Author: zhz
* @CreateTime: 2024/11/20 10:45
* @Version: 1.0.0
*/
//@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD}) //标识当前注解在类使用
@Target({ElementType.TYPE}) //标识当前注解在类使用
@Retention(RetentionPolicy.RUNTIME) // 标识当前注解在运行时保留
@Documented
@Inherited
public @interface Controller {
//定义当前注解的属性
//定义name属性 设置默认值为BaseController
String name() default "BaseController";
//定义value属性 没有默认值 没有默认值时,使用该注解时,必须写属性并赋值,否则报错
String value();
}
使用
package com.aaa.annotation.class1;
import java.util.HashMap;
/**
* @FileName: DeptController
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 10:51
* @Version: 1.0.0
*/
@Controller(value = "deptController")
public class DeptController {
//@Controller(value = "testMap") //报错,说明当前注解不能被使用到属性上
private HashMap testMap;
/**
* 测试方法
*/
//@Controller(value = "testMap") //报错,说明当前注解不能被使用到方法上
public void testA(){
}
}
测试
package com.aaa.annotation.class1;
/**
* @FileName: Test
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 10:56
* @Version: 1.0.0
*/
public class Test {
public static void main(String[] args) {
//获取DeptController的class对象
Class<DeptController> deptControllerClass = DeptController.class;
//判断类头上有没有Controller注解
if(deptControllerClass.isAnnotationPresent(Controller.class)){
//获取注解
Controller controllerAnnotation = deptControllerClass.getAnnotation(Controller.class);
//打印属性
System.out.println("注解中属性name的值:"+controllerAnnotation.name()+",注解中属性value的值:"+controllerAnnotation.value());
}
}
}
自定义属性注解
需求
模仿spring框架的@Autowired,在属性上使用
代码
注解
package com.aaa.annotation.field;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @FileName: Autowired
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:19
* @Version: 1.0.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
//定义一个属性
String value() default "baseService";
}
使用
package com.aaa.annotation.field;
/**
* @FileName: UserService
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:21
* @Version: 1.0.0
*/
public interface UserService {
}
package com.aaa.annotation.field;
/**
* @FileName: UserController
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:21
* @Version: 1.0.0
*/
//@Autowired //不能使用在类
public class UserController {
//在属性上使用
// @Autowired(value = "userService")
@Autowired(value = "userService")
private UserService userService111;
}
测试
package com.aaa.annotation.field;
import java.lang.reflect.Field;
/**
* @FileName: Test
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:22
* @Version: 1.0.0
*/
public class Test {
public static void main(String[] args) throws Exception {
//获取class
Class clazz = UserController.class;
//反射获取属性
Field userServiceField = clazz.getDeclaredField("userService111");
//获取注解
//先判断是否存在某一个注解
if(userServiceField.isAnnotationPresent(Autowired.class)){
Autowired autowired = userServiceField.getAnnotation(Autowired.class);
System.out.println(autowired.value());
}else {
System.out.println("没有该注解");
}
}
}
自定义方法注解
需求
模仿mybatis框架的@Select,在方法上使用
代码
注解
package com.aaa.annotation.method;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @FileName: Select
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:33
* @Version: 1.0.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Select {
//定义value属性
String value() default "select * from mysql.user";
}
使用
package com.aaa.annotation.method;
import java.util.List;
import java.util.Map;
/**
* @FileName: DeptMapper
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:35
* @Version: 1.0.0
*/
//@Select
public interface DeptMapper {
// @Select
Integer MAX_VALUE = 1024;
/**
* 分页查询
* @return
*/
@Select(value = "select * from tb_dept")
List<Map> queryPage();
}
测试
package com.aaa.annotation.method;
import java.lang.reflect.Method;
/**
* @FileName: Test
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 11:39
* @Version: 1.0.0
*/
public class Test {
public static void main(String[] args) throws NoSuchMethodException {
// 获取接口的Class对象
Class<DeptMapper> deptMapperClass = DeptMapper.class;
//获取方法
Method queryPageMethods = deptMapperClass.getDeclaredMethod("queryPage");
//判断
if(queryPageMethods.isAnnotationPresent(Select.class)){
Select select = queryPageMethods.getAnnotation(Select.class);
System.out.println(select.value());
}else {
System.out.println("没有注解");
}
}
}
@Documented实战用法
准备工作
locale: zh_CN
command line arguments:
-encoding UTF-8 -charset UTF-8 -windowtitle "ANNOTATION" -link https://docs.oracle.com/javase/8/docs/api
多次导出,确认是否使用@Documented注解,如果没用,导出就没有,用了,导出就有
@Inherited实战用法
代码
注解
package com.aaa.annotation.inherited;
import java.lang.annotation.*;
/**
* @FileName: RestController
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 14:52
* @Version: 1.0.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
//@Inherited
public @interface RestController {
}
使用
package com.aaa.annotation.inherited;
/**
* @FileName: BaseController
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 14:53
* @Version: 1.0.0
*/
@RestController
public class BaseController {
}
package com.aaa.annotation.inherited;
/**
* @FileName: GoodsController
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 14:53
* @Version: 1.0.0
*/
public class GoodsController extends BaseController{
}
测试
package com.aaa.annotation.inherited;
/**
* @FileName: Test
* @Description:
* @Author: zhz
* @CreateTime: 2024/11/20 14:54
* @Version: 1.0.0
*/
public class Test {
public static void main(String[] args) {
//获取class
Class<GoodsController> goodsControllerClass = GoodsController.class;
//判断是否有@RestController注解
System.out.println(goodsControllerClass.isAnnotationPresent(RestController.class));
}
}
浙公网安备 33010602011771号