前言
java快速开发的一个必备要素。
也就是java注解:
是Java5开始引入的新特征。它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联
java元注解
1、@Retention (一个注解的生命周期)
带参:
RententionPolicy.source(注解仅存在于源码中,在class字节码文件中不包含)
RententionPolicy.class (默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得)
RententionPolicy.runtime(注解会在class字节码文件中存在,在运行时可以通过反射获取到)
2、@Target(一个注解的使用范围)
带参:
ElementType.type (只能在**上面使用该注解)**:类
ElementType.fleld (只能在**上面使用该注解)**:属性
ElementType.method (只能在**上面使用该注解)**:方法
ElementType.parameter(只能在**上面使用该注解)**:方法参数
-------------------以上常用
ElementType.constructor(只能在**上面使用该注解)**: 构造函数
ElementType.local_variavle(只能在**上面使用该注解)**:局部变量
ElementType.annotation_type(只能在**上面使用该注解)**:注解
ElementType.package(只能在**上面使用该注解)**:包
注:可以同时指定多个位置,例如:
@ Target({ ElementType.method, ElementType.type}),也就是此注解可以在方法或类上面使用
3、@inherited (一个注解可以被继承)
4、@Documented(一个注解可以被javadoc工具提取为文档)
java基本注解
1、@Override (jdk1.5更新) 重写
ps:比如说我们的service层有接口类和实现类,实现类就是用来重写接口类的方法,那么就要加上该注解
2、@SuppressWarnings (jdk1.5更新) 抑制器
ps:就是用来把报黄色警告的变为不报警告(黄色警告本来就没有错误)
3、@Deprecated (jdk1.5更新) 过时
ps:可以参考我们写的一些new Date().toLocaleString(),这就有很多过时的。
---------------------------以上常用
4、@SafeVarargs (jdk1.7更新) 表示:专门为抑制“堆污染”警告提供的。
5、@FunctionalInterface (jdk1.8更新) 表示:用来指定某个接口必须是函数式接口,否则就会编译出错。
ps:函数式接口:Java8规定,如果接口中只有一个抽象方法,该接口称为函数式接口。
java自定义注解
通过自己写的一个注解类,用元注解修饰自己写的注解类后,就可以在其他类上使用
简单介绍一下注解可以写些什么
@ 元注解 (上面的元注解来修饰注解)
public @interface myannotation {
String str(); //调用@myannotation(str="数据定义str")
}
@ 元注解 (上面的元注解来修饰注解)
public @interface myannotation {
String str() default "默认"; //调用@myannotation() ,不用写值,也有默认的
}
@ 元注解 (上面的元注解来修饰注解)
public @interface myannotation {
mymodel models(); //调用@myannotation() ,不用写值,也有默认的
}
枚举(只能使用enum类的,不能使用class类的)
获取java注解里的值
demoController.java
package com.liwangwang.demo;
import com.liwangwang.annotation.*;
/**
* @authorliwangwang
* @site www.liwangwang.com
* @company xxx公司
* @create 2019-11-06 18:57
*/
@myannotation(name="myannotation的注解")
public class demoController {
@myannotation1()
public String age ;
public void add(@myannotation2(isNoll = true) String b1,@myannotation2 String b2){
System.out.println("使用参数注解");
}
@mymodel1(mymodel = mymodelenum.name )
public void list(){
System.out.println("用枚举的类");
}
}
demo.java
package com.liwangwang.demo;
import com.liwangwang.annotation.myannotation;
import com.liwangwang.annotation.myannotation1;
import com.liwangwang.annotation.myannotation2;
import com.liwangwang.annotation.mymodel1;
import java.lang.reflect.Parameter;
/**
* @authorliwangwang
* @site www.liwangwang.com
* @company xxx公司
* @create 2019-11-06 19:12
*/
public class demo {
@org.junit.Test
public void test1() throws NoSuchFieldException, NoSuchMethodException {
//类注解的数值
myannotation annotation = demoController.class.getAnnotation(myannotation.class);
System.out.println("demoController类上面注解的值是:"+annotation.name());//注解
//属性注解的数值
myannotation1 age = demoController.class.getField("age").getAnnotation(myannotation1.class);
System.out.println("demoController类里面的age属性上面注解的值是:"+age.name());//默认注解1
//方法注解的数值
mymodel1 list = demoController.class.getMethod("list").getAnnotation(mymodel1.class);
System.out.println("demoController类里面的list方法上面注解的值是:"+list.mymodel());//name
//参数注解的数值
Parameter[] adds = demoController.class.getMethod("add", String.class, String.class).getParameters();
for (Parameter s:adds){
myannotation2 annotation1 = s.getAnnotation(myannotation2.class);
if(annotation1!=null){
System.out.println(annotation1.isNoll());//true ,false
}
}
}
}
Aop结合注解开发案例
LogController控制层
package com.liwangwang.demo;
import com.liwangwang.annotation.MyLog;
import org.springframework.stereotype.Component;
@Component
public class LogController {
@MyLog(desc = "这是结合spring aop知识,讲解自定义注解应用的一个案例")
public void testLogAspect(){
System.out.println("这里随便来点啥");
}
}
Mylog注解
package com.liwangwang.annotation;
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 MyLog {
String desc();
}
MyLogAspect切入面
package com.liwangwang.aspect;
import com.liwangwang.annotation.MyLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyLogAspect {
private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
/**
* 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的,就是目标类
*/
@Pointcut("@annotation(com.liwangwang.annotation.MyLog)")
private void MyValid() {
}
@Before("MyValid()")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
logger.debug("[" + signature.getName() + " : start.....]");
MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);
logger.debug("【监听MyLog这个注解是否使用到了,并且他注解的内容为】:"+myLog.desc());
}
}
测试
import com.liwangwang.demo.LogController;
import org.springframework.beans.factory.annotation.Autowired;
public class Test extends BaseTestCase {
@Autowired
LogController logController;
}
@org.junit.Test
public void tes(){
logController.testLogAspect();
}
}