自定义注解
如何使用自定义注解?
案例1:获取类与方法上的注解值
MyAnnotation1
package com.liao.annotation; import java.lang.annotation.*; /** * MyAnnotation1注解可以用在类、接口、属性、方法上 * 注解运行期也保留 * 不可继承 */ @Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation1 { String name(); }
MyAnnotation2
package com.liao.annotation; import com.liao.model.TranscationModel; import java.lang.annotation.*; /** * MyAnnotation2注解可以用在方法上 * 注解运行期也保留 * 不可继承 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation2 { TranscationModel model() default TranscationModel.ReadWrite; }
MyAnnotation3
package com.liao.annotation; import com.l.model.TranscationModel; import java.lang.annotation.*; /** * MyAnnotation3注解可以用在方法上 * 注解运行期也保留 * 可继承 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface MyAnnotation3 { TranscationModel[] models() default TranscationModel.ReadWrite; }
Demo1测试类 : 获取类与方法上的注解值
package com.liao.annotation.demo; import com.liao.annotation.MyAnnotation1; import com.liao.annotation.MyAnnotation2; import com.liao.annotation.MyAnnotation3; import com.liao.model.TranscationModel; /** * 获取类与方法上的注解值 */ @MyAnnotation1(name = "abc") public class Demo1 { @MyAnnotation1(name = "xyz") 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"); } }
案例二:获取类属性上的注解属性值
TestAnnotation
package com.liao.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface TestAnnotation { String value() default "默认value值"; String what() default "这里是默认的what属性对应的值"; }
测试类
package com.liao.annotation.demo2; import com.liao.annotation.TestAnnotation; /** * 获取类属性上的注解属性值 */ 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; }
测试
package com.liao.annotation.demo2; import com.liao.annotation.TestAnnotation; import org.junit.Test; public class Demo2Test { @Test public void test1() throws Exception { TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class); System.out.println(msg1.value()); System.out.println(msg1.what()); } @Test public void test2() throws Exception{ TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class); System.out.println(msg2.value()); System.out.println(msg2.what()); } @Test public void test3() throws Exception{ TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class); System.out.println(msg3.value()); System.out.println(msg3.what()); } @Test public void test4() throws Exception{ TestAnnotation msg4 = Demo2.class.getDeclaredField("msg4").getAnnotation(TestAnnotation.class); System.out.println(msg4.value()); System.out.println(msg4.what()); } }
示例三:获取参数修饰注解对应的属性值
IsNotNull自定义注解
package com.liao.annotation; import java.lang.annotation.*; /** * 非空注解:使用在方法的参数上,false表示此参数可以为空,true不能为空 */ @Documented @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface IsNotNull { boolean value() default false; }
测试:
package com.liao.annotation.demo3; import com.liao.annotation.IsNotNull; /** * 获取参数修饰注解对应的属性值 */ 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); } }
package com.liao.annotation.demo3; import com.liao.annotation.IsNotNull; import org.junit.Test; import java.lang.reflect.Method; import java.lang.reflect.Parameter; public class Demo3Test { @Test public 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 } } } @Test public 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 } } } @Test public void hello3() throws Exception { // 模拟浏览器传递到后台的参数 解读@requestParam String 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());//true if (annotation.value() && !"".equals(name)){ method.invoke(demo3,name); } } } } }
Aop自定义注解的应用
MyLog自定义注解类
package com.liao.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.liao.aspect; import com.liao.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.liao.annotation.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()); } }
controller层
package com.liao.web; import com.liao.annotation.aop.MyLog; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LogController { @RequestMapping("/mylog") @MyLog(desc = "这是结合spring aop知识,讲解自定义注解应用的一个案例") public void testLogAspect(){ System.out.println("记录日志。。。"); } }