天津建设网站安管人员成绩查询,南宁网站建设索q479185700,建设工程抗震管理条例,定制型和模板型网站目录
一、Java注解
1.1 注解简介
1.2 注解分类
1.3 JDK基本注解
1.4 JDK元注解
1.5 自定义注解
1.5.1 标记注解
1.5.2 元数据注解
1.6 如何自定义注解
二、自定义注解的基本案例
2.1 案例一#xff08;获取类、方法以及属性上的注解#xff09;
2.1.1 Ingerited的…目录
一、Java注解
1.1 注解简介
1.2 注解分类
1.3 JDK基本注解
1.4 JDK元注解
1.5 自定义注解
1.5.1 标记注解
1.5.2 元数据注解
1.6 如何自定义注解
二、自定义注解的基本案例
2.1 案例一获取类、方法以及属性上的注解
2.1.1 Ingerited的使用
2.2 案例二获取类属性上的注解属性值 2.3 案例三获取参数修饰注解对应的属性值
三、AOP自定义注解的应用
3.1 定义自定义注解Mylog
3.2 定义切面类MyLogAspect
3.3 创建一个控制器LogController 一、Java注解
1.1 注解简介 Java注解Annotations是Java语言中的一种元程序meta-programming机制提供了一种在程序中添加元数据信息的方法。注解可以应用于类、方法、变量、参数等程序元素上用于给程序代码添加一些特别的信息以便其他程序或工具能够更好地理解和处理这些程序元素。注解可以带参数也可以没有参数定义注解时需要使用注解interface来声明注解中的参数通过定义成员变量来实现。注解的属性的类型可以是基本类型、字符串、枚举类型、注解类型等。注解在运行时通过反射来获取注解信息可以用于实现动态代理、自动代码生成等功能。Java提供了许多预定义注解如Override、Deprecated、SuppressWarnings等同时也可以自定义注解来满足特定的需求。注解的使用通常需要结合Java反射机制、Java编译器等技术来实现可以使代码更加清晰、简洁、易于维护。 1.2 注解分类
Java注解Annotation按照其作用范围和保留策略可以分为以下几类 1. 元注解Meta-Annotation用于修饰注解的注解如Target、Retention、Documented、Inherited等。 2. 编译时注解Compile-Time Annotation只存在于源代码中在编译时会被编译器处理掉如Override、Deprecated、SuppressWarnings等。 3. 运行时注解Runtime Annotation在程序运行时才会被解析和处理通常用于实现框架或插件的功能如SpringBootConfiguration、ComponentScan、RequestMapping等。 4. 类型注解Type Annotation从Java 8开始提供的注解可以用于指定注解的作用目标类型如NonNull、Nullable等。 5. 单值注解和多值注解按照注解值的个数可分为单值注解和多值注解。其中单值注解只有一个值多值注解可以有多个值如Value、Order等。 1.3 JDK基本注解
JDKJava Development Kit是Java的开发工具包提供了许多注解Annotation来帮助Java开发者编写更加优秀的代码。下面是JDK常用的几种注解 1. Override用于表明当前方法是重写了父类或者实现了接口中的方法。如果不小心写错了方法名或者参数类型编译时会提示错误。 2. Deprecated用于标记某个方法或者类已经过时告诉开发者不要再使用该方法或者类。虽然标记为过时但是还可以继续使用。 3. SuppressWarnings用于抑制警告信息可以选择性地关闭编译器对某些代码的警告提示。例如SuppressWarnings(unchecked)可以关闭编译器对未经检查的转换产生的警告。 4. SafeVarargs用于标记当前方法使用可变参数时不会出现类型不安全的问题。该注解可以解决一些泛型转型的警告问题。 5. FunctionalInterface用于标记某个接口是函数式接口即只有一个抽象方法的接口。使用该注解可以帮助开发者避免不必要的错误同时也可以让编译器进行更加优化的处理。 除了上述注解JDK还有许多其他的注解例如Nullable和NonNull等。这些注解可以提高代码的可读性和可维护性使代码更加健壮和简洁。
1.4 JDK元注解
元注解是用于注解其他注解的注解。在 Java 中有四个元注解分别是Retention、Target、Inherited 和 Documented。 1. Retention用于指定注解的声明周期即注解的保留时间。它有三个取值RetentionPolicy.SOURCE、RetentionPolicy.CLASS、RetentionPolicy.RUNTIME。其中RetentionPolicy.SOURCE 表示该注解只在源码中存在编译成字节码后就不存在了RetentionPolicy.CLASS 表示该注解会被编译到字节码中但在运行时不会被虚拟机保留RetentionPolicy.RUNTIME 表示该注解会保留到运行时并可以通过反射获取到。 2. Target用于指定注解的作用范围即注解可以用在哪些元素上。它有多个取值包括 ElementType.TYPE类、接口、枚举、ElementType.FIELD字段、ElementType.METHOD方法、ElementType.PARAMETER参数、ElementType.CONSTRUCTOR构造方法、ElementType.LOCAL_VARIABLE局部变量、ElementType.ANNOTATION_TYPE注解类型和 ElementType.PACKAGE包。 3. Inherited用于指定注解是否可以被继承。如果一个注解被 Inherited 修饰它的子类会自动继承该注解。 4. Documented用于指定注解是否可以被包含在 JavaDoc 文档中。如果一个注解被 Documented 修饰它会被包含在 JavaDoc 文档中方便开发者查看。 1.5 自定义注解
1.5.1 标记注解
Java中的自定义注解可以有多种类型其中一种是标记注解Marker Annotation。这种注解不包含任何成员变量只是作为一种标记用于标识某些特殊的情况或设置。比较常见的标记注解有以下几种 Deprecated标记某个方法或类已经过时不建议使用通常用于提醒开发者避免使用这些方法或类。 Override标记某个方法是重写了父类的方法用于提醒开发者这个方法与父类方法同名但已经重写需要特别注意。 SuppressWarnings标记某个代码段不需要被编译器警告或报错通常用于消除编译器针对代码的一些警告信息比如去除未使用的变量等。 这些标记注解可以很方便地在代码中进行标识并提供给编译器或其他工具进行处理。同时也方便其他开发者阅读和理解代码的意图。
1.5.2 元数据注解
Java自定义注解中的元数据注解是指用来定义注解的注解也就是说它可以用来描述一个注解的定义。
例如Java中的Target注解就是一个元数据注解它用来指定一个注解可以应用的目标元素如类、方法、字段等。
另一个例子是Retention注解它用来指定一个注解可以在运行时保留多长时间如只在源码中保留、在.class文件中保留、在运行时保留等。
除了Target和Retention之外还有一些其他的元数据注解可以用来描述一个注解的定义如Documented、Inherited、Repeatable等等。这些注解都是在Java语言中提供的可以灵活地用于定义各种风格的自定义注解。
1.6 如何自定义注解 使用interface关键字, 其定义过程与定义接口非常类似, 需要注意的是: Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型, 而且我们还可以使用default关键字为这个成员变量设定默认值 二、自定义注解的基本案例
2.1 案例一获取类、方法以及属性上的注解
前期准备在java包下创建pakage如下
建包目录如下 在demo1下建立一个TranscationModel代码如下
package com.Kissship.annotation.demo1;public enum TranscationModel {Read, Write, ReadWrite;private String name;private Integer id;public void init1(){Read.id 1;Read.name 刘一金;}public void init2(){Write.id 2;Write.name 刘二金;}public void init3(){ReadWrite.id 3;ReadWrite.name 刘三金;}
}
同样是在demo1目录下新建三个MyAnnotation如下
MyAnnotation1代码如下
package com.Kissship.annotation.demo1;import java.lang.annotation.*;/*** MyAnnotation1注解可以用在类、接口、属性、方法上* 注解运行期也保留* 不可继承*/
Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface MyAnnotation1 {String name();
}
MyAnnotation2代码如下
package com.Kissship.annotation.demo1;import java.lang.annotation.*;/*** MyAnnotation2注解可以用在方法上* 注解运行期也保留* 不可继承*/
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
Documented
public interface MyAnnotation2 {TranscationModel model() default TranscationModel.ReadWrite;
}新建MyAnnotation3代码如下
package com.Kissship.annotation.demo1;import java.lang.annotation.*;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00* MyAnnotation3注解可以用在方法上* 注解运行期也保留* 可继承*/
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
Inherited
Documented
public interface MyAnnotation3 {TranscationModel[] models() default TranscationModel.ReadWrite;
}注
2.1.1 Ingerited的使用 Inherited注解是Java中的元注解之一用于控制注解的继承性。当一个类或接口被Inherited注解修饰时它的子类将自动获得该注解。但需要注意的是Inherited只对类和接口有效对方法、字段等无效。普通的类继承是指子类可以继承父类的所有非private的成员变量和方法包括构造器。子类可以使用父类中的方法和属性也可以重写父类的方法在其基础上进行扩展或修改。 Ingerited注解继承与普通类继承的区别 因此Inherited注解继承与普通类继承的区别在于Inherited注解只能继承注解而普通类继承可以继承所有非private的成员变量和方法。 接下来就是测试类demo1界面代码如下
package com.Kissship.annotation.demo1;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
MyAnnotation1(name 刘三金)
public class Demo1 {MyAnnotation1(name 《挪威的森林》)private Integer age;MyAnnotation2(model TranscationModel.Read)public void list() {System.out.println(list);}MyAnnotation3(models {TranscationModel.Read, TranscationModel.Write})public void edit() {System.out.println(edit);}
}Demo1Test代码如下
package com.Kissship.annotation.demo1;import org.junit.Test;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
public class Demo1Test {Testpublic void list() throws Exception {
// 获取类上的注解MyAnnotation1 annotation1 Demo1.class.getAnnotation(MyAnnotation1.class);System.out.println(annotation1.name());// 获取方法上的注解MyAnnotation2 myAnnotation2 Demo1.class.getMethod(list).getAnnotation(MyAnnotation2.class);System.out.println(myAnnotation2.model());// 获取属性上的注解MyAnnotation1 myAnnotation1 Demo1.class.getDeclaredField(age).getAnnotation(MyAnnotation1.class);System.out.println(myAnnotation1.name());}Testpublic void edit() throws Exception {MyAnnotation3 myAnnotation3 Demo1.class.getMethod(edit).getAnnotation(MyAnnotation3.class);for (TranscationModel model : myAnnotation3.models()) {System.out.println(model);}}
}然后就可进行执行测试了案例一测试结果如下
list方法执行结果 edit方法执行结果 2.2 案例二获取类属性上的注解属性值
新建一个demo2包用来演示案例二如下 放入案例代码如下
Demo2
package com.Kissship.annotation.demo2;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
public class Demo2 {TestAnnotation(value 这就是value对应的值_msg1, what 这就是what对应的值_msg1)private static String msg1;TestAnnotation(这就是value对应的值1)private static String msg2;TestAnnotation(value 这就是value对应的值2)private static String msg3;TestAnnotation(what 这就是what对应的值)private static String msg4;
}Demo2Test
package com.Kissship.annotation.demo2;import org.junit.Test;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
public class Demo2Test {Testpublic void test1() throws Exception {TestAnnotation msg1 Demo2.class.getDeclaredField(msg1).getAnnotation(TestAnnotation.class);System.out.println(msg1.value());System.out.println(msg1.what());}Testpublic void test2() throws Exception{TestAnnotation msg2 Demo2.class.getDeclaredField(msg2).getAnnotation(TestAnnotation.class);System.out.println(msg2.value());System.out.println(msg2.what());}Testpublic void test3() throws Exception{TestAnnotation msg3 Demo2.class.getDeclaredField(msg3).getAnnotation(TestAnnotation.class);System.out.println(msg3.value());System.out.println(msg3.what());}Testpublic void test4() throws Exception{TestAnnotation msg4 Demo2.class.getDeclaredField(msg4).getAnnotation(TestAnnotation.class);System.out.println(msg4.value());System.out.println(msg4.what());}
}TestAnnotation
package com.Kissship.annotation.demo2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
//Retention(RetentionPolicy.SOURCE)
Retention(RetentionPolicy.RUNTIME)
Target(ElementType.FIELD)
public interface TestAnnotation {String value() default 默认value值;String what() default 这里是默认的what属性对应的值;
}进行测试如下
Test1方法结果如下 测试Test2方法结果如下 测试Test3方法结果如下 测试Test4方法结果如下 2.3 案例三获取参数修饰注解对应的属性值
同上新建demo3进行案例三演示代码的存放如下 新建自定义注解IsNotNull代码如下
package com.Kissship.annotation.demo3;import java.lang.annotation.*;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00* 非空注解使用在方法的参数上false表示此参数可以为空true不能为空*/
Documented
Target({ElementType.PARAMETER})
Retention(RetentionPolicy.RUNTIME)
public interface IsNotNull {boolean value() default false;
}新建测试类Demo3代码如下
package com.Kissship.annotation.demo3;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00* 获取参数修饰注解对应的属性值*/
public class Demo3 {public void hello1(IsNotNull(true) String name) {System.out.println(hello: name);}public void hello2(IsNotNull String name) {System.out.println(hello: name);}
}新建测试类Demo3Test代码如下
package com.Kissship.annotation.demo3;import org.junit.Test;import java.lang.reflect.Method;
import java.lang.reflect.Parameter;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
public class Demo3Test {Testpublic void hello1() throws Exception {Demo3 demo3 new Demo3();for (Parameter parameter : demo3.getClass().getMethod(hello1, String.class).getParameters()) {IsNotNull annotation parameter.getAnnotation(IsNotNull.class);if(annotation ! null){System.out.println(annotation.value());//true}}}Testpublic void hello2() throws Exception {Demo3 demo3 new Demo3();for (Parameter parameter : demo3.getClass().getMethod(hello2, String.class).getParameters()) {IsNotNull annotation parameter.getAnnotation(IsNotNull.class);if(annotation ! null){System.out.println(annotation.value());//false}}}Testpublic void hello3() throws Exception {
// 模拟浏览器传递到后台的参数 解读requestParamString name zs;Demo3 demo3 new Demo3();Method method demo3.getClass().getMethod(hello1, String.class);for (Parameter parameter : method.getParameters()) {IsNotNull annotation parameter.getAnnotation(IsNotNull.class);if(annotation ! null){System.out.println(annotation.value());//trueif (annotation.value() !.equals(name)){method.invoke(demo3,name);}}}}
}执行hello1方法测试结果如下 执行hello2方法测试结果如下
执行hello3方法测试结果如下 三、AOP自定义注解的应用 AOP面向切面编程是一种编程思想它通过在代码中定义切点和切面来实现对程序行为的控制和扩展。在AOP中切点表示程序中的某个特定位置切面表示在切点处要执行的逻辑。而注解是一种元数据它可以提供额外的信息并且可以在编译期、运行期、甚至是部署期进行处理。 下面演示AOP与自定义注解相结合应用实现日志记录功能如下
3.1 定义自定义注解Mylog
用于标记记录日志的方法如下
package com.Kissship.annotation.aop;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
public interface MyLog {String desc();
}3.2 定义切面类MyLogAspect
在该类中定义增强逻辑例如记录日志等。如下
package com.Kissship.aspect;import com.Kissship.annotation.aop.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;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
Component
Aspect
public class MyLogAspect {private static final Logger logger LoggerFactory.getLogger(MyLogAspect.class);/*** 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的就是目标类*/Pointcut(annotation(com.Kissship.annotation.aop.MyLog))private void MyValid() {}Before(MyValid())public void before(JoinPoint joinPoint) {MethodSignature signature (MethodSignature) joinPoint.getSignature();logger.debug([ signature.getName() : start.....]);System.out.println([ signature.getName() : start.....]);MyLog myLog signature.getMethod().getAnnotation(MyLog.class);logger.debug(【目标对象方法被调用时候产生的日志记录到日志表中】myLog.desc());System.out.println(【目标对象方法被调用时候产生的日志记录到日志表中】 myLog.desc());}
}在上面的切面类中使用Aspect注解标记该类为切面类。Pointcut(annotation(com.CloudJun.annotation.aop.MyLog))注解用于定义切点表示匹配所有标记有Log注解的方法。Before注解表示在目标方法执行前执行增强逻辑。 3.3 创建一个控制器LogController
代码如下
package com.Kissship.web;import com.Kissship.annotation.aop.MyLog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-14-19:00*/
Controller
public class LogController {RequestMapping(/mylog)MyLog(desc 这是结合spring aop知识讲解自定义注解应用的一个案例)public void testLogAspect(HttpServletRequest request){request.getRemoteAddr();//这里是获取请求IP,可以输出或者保存在某个地方及属性request.getRemotePort();//这里是获取请求端口,可以输出或者保存在某个地方及属性System.out.println(来自九院压力怪————刘三金的死亡凝视);}
}进行代码测试结果如下 然后在切面类中( MyLogAspect ) 的 before 方法进行注释加入doAround方法代码如下
Around(MyValid())public Object doAround(ProceedingJoinPoint pjp) throws Throwable {long startTime System.currentTimeMillis();System.out.println(pjp.getTarget());//获取目标方法System.out.println(pjp.getThis());//Object[] args pjp.getArgs();//获取参数System.out.println(Arrays.toString(args));//输出参数Object ob pjp.proceed();//获取方法返回值System.out.println(ob);//输出返回值logger.info(耗时 : (System.currentTimeMillis() - startTime));return ob;}
接着进行测试测试结果如下 最后SpringMVC之自定义注解就到这里祝大家在敲代码的路上一路通畅!
感谢大家的观看 !