注解讲解
@Target(ElementType.TYPE) //表示在类上使用
@Target(ElementType.FIELD) //表示只能在属性上使用
@Target({ElementType.METHOD, ElementType.TYPE}) //表示既能在方法也能在类上使用
@Target(ElementType.PARAMETER) //表示只能在属性上使用
@Retention(RetentionPolicy.RUNTIME) //表示能在运行中使用
自定义注解使用场景
1、可以做权限拦截(结合aop或者拦截器)
2、可以做日志记录(结合aop或者拦截器)
3、表示说明(比如:常见的注解@Override就表示该方法被重写了)
4、可以自己写依赖注入
5、等…
case
引入示例1说明:自定义注解定义及说明(只能在类上使用)
package com.bopai.annotation;
import java.lang.annotation.*;
/**
* @interface 表示这个类是 注解
*/
@Target(ElementType.TYPE) //表示在类上使用
@Retention(RetentionPolicy.RUNTIME) //表示能在运行中使用
@Documented
public @interface BPController {
String value() default ""; //注解上写的值 ,default表示默认的值是什么
}
使用示例1:
package com.bopai.annotation;
@BPController("自定义注解")
public class Test {
}
引入示例2说明:自定义注解定义及说明(只能在属性上使用)
package com.bopai.annotation;
@BPController("自定义注解")
@BPRequestMapping("test")
public class Test {
@BPAutowired
private TestService testService;
@BPRequestMapping("/test.json")
public void testMain(@BPRequestParam("name") String name){
}
}
本文详细解析了自定义注解的使用场景与实现方式,包括权限拦截、日志记录等功能,并通过具体案例展示了如何在类和属性上应用自定义注解。
1064

被折叠的 条评论
为什么被折叠?



