目录
Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
JDK基本注解
JDK元注解
自定义注解
@Override
重写@SuppressWarnings(value = "unchecked")
压制编辑器警告
@Retention:定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到@Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)
@Target(ElementType.TYPE) //接口、类
@Target(ElementType.FIELD) //属性
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE) //局部变量
@Target(ElementType.ANNOTATION_TYPE) //注解
@Target(ElementType.PACKAGE) //包
注:可以指定多个位置,例如:
@Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用@Inherited:指定被修饰的Annotation将具有继承性
@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.
注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):
标记Annotation:
没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息元数据Annotation:
包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;
使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:
Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型,
而且我们还可以使用default关键字为这个成员变量设定默认值;
假设这是我们写的一个注解类
我们使用它时就会报错
其根本原因是注解类上面的Target,
METHOD 注解只能在方法上使用
TYPE 注解只能在类上使用
FIELD 注解只能在属性上使用
我们改成TYPE就可以啦
不报错啦
如果想用多个
可以写多个tagar
代码:
- //1.只能用于类
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.SOURCE)
-
- //2.只能用于属性
- @Target(ElementType.FIELD)
- @Retention(RetentionPolicy.SOURCE)
-
- //3.只能用于方法
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.SOURCE)
-
- //4.类,属性,方法都可以用
- @Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
- @Retention(RetentionPolicy.SOURCE)
给注解写一个属性
若没有加注解的属性就会报错
写上注解的属性就可以不报错了,value这个属性可以默认不写,其他的属性就不能了
还可以默认注解的属性
这样也不会报错啦
直接运行我们就会报错
原因是注解上的source,将它改为runtime运行时可见
结果为(获取到啦):
测试的代码:
- package com.zking.ssm.annaction.dome;
-
- import com.zking.ssm.annaction.MyAnnaction1;
- import com.zking.ssm.annaction.MyAnnaction2;
- import com.zking.ssm.annaction.RuojuanController;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年10月27日 14:21
- **/
- public class Dome1 {
-
- public static void main(String[] args) {
-
- //获取类上的注解
- MyAnnaction1 annaction = RuojuanController.class.getAnnotation(MyAnnaction1.class);
- System.out.println(annaction.value());
- System.out.println(annaction.desc());
-
- }
-
- }
注解的代码:
- package com.zking.ssm.annaction;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年10月27日 11:14
- **/
-
-
- @Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyAnnaction1 {
-
- //指的是注解中的属性
- public String value() default "可以修饰方法,属性,类";
-
-
- public String desc() default "可以修饰方法,属性,类";
-
-
- }
代码:
- package com.zking.ssm.annaction.dome;
-
- import com.zking.ssm.annaction.MyAnnaction1;
- import com.zking.ssm.annaction.MyAnnaction2;
- import com.zking.ssm.annaction.RuojuanController;
-
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Field;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年10月27日 14:21
- **/
- public class Dome1 {
-
- public static void main(String[] args) throws Exception {
-
-
- //获取属性上的
- Field id = RuojuanController.class.getDeclaredField("id");
- Field name = RuojuanController.class.getDeclaredField("name");
- System.out.println(id.getAnnotation(MyAnnaction1.class).value());
- System.out.println(name.getAnnotation(MyAnnaction1.class).value());
-
-
-
- }
-
- }
效果:
代码:
- package com.zking.ssm.annaction.dome;
-
- import com.zking.ssm.annaction.MyAnnaction1;
- import com.zking.ssm.annaction.MyAnnaction2;
- import com.zking.ssm.annaction.RuojuanController;
-
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年10月27日 14:21
- **/
- public class Dome1 {
-
- public static void main(String[] args) throws Exception {
-
- //获取方法上的
- Method me1 = RuojuanController.class.getDeclaredMethod("test", long.class, String.class);
- System.out.println(me1.getAnnotation(MyAnnaction1.class).value());
-
- }
-
- }
写一个专门用来修饰属性的
代码:
- package com.zking.ssm.annaction;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年10月27日 11:14
- **/
-
-
- @Target({ElementType.PARAMETER})
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyAnnaction2 {
-
- //指的是注解中的属性
- public String value() default "可以修饰方法,属性,类";
-
-
- public String desc() default "可以修饰方法,属性,类";
-
-
- }
获取参数上的标识
代码:
- package com.zking.ssm.annaction.dome;
-
- import com.zking.ssm.annaction.MyAnnaction1;
- import com.zking.ssm.annaction.MyAnnaction2;
- import com.zking.ssm.annaction.RuojuanController;
-
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.lang.reflect.Parameter;
-
- /**
- * @author ruojuan
- * @site www.ruojuan.com
- * @company 玉渊工作室
- * @create 2022年10月27日 14:21
- **/
- public class Dome1 {
-
- public static void main(String[] args) throws Exception {
-
-
- //获取方法上的
- Method me1 = RuojuanController.class.getDeclaredMethod("test", long.class, String.class);
- System.out.println(me1.getAnnotation(MyAnnaction1.class).value());
-
- //获取参数上的标识
- for(Parameter p:me1.getParameters()){
- System.out.println(p.getAnnotation(MyAnnaction2.class).value());
- }
-
- }
-
- }
自定义注解
- package com.zking.annotation.aop;
-
- 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();
- }
applicationContext.xml
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
-
-
- <context:annotation-config/>
-
- <context:component-scan base-package="com.zking"/>
-
- <aop:aspectj-autoproxy />
-
- beans>
应用注解
- package com.zking.annotation.aop;
-
- 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.zking.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());
- }
- }
- package com.zking.annotation.aop;
-
- 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.zking.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());
- }
- }
- package com.zking.annotation.aop;
-
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext.xml"})
- public class BaseTestCase {
-
- }
- package com.zking.annotation.aop;
-
- import org.junit.Test;
- import org.springframework.beans.factory.annotation.Autowired;
-
- public class LogControllerTest extends BaseTestCase {
- @Autowired
- private LogController logController;
-
- @Test
- public void testLogAspect(){
- logController.testLogAspect();
- }
- }
pom.xml
- "1.0" encoding="UTF-8"?>
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.zkinggroupId>
- <artifactId>selenium280artifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>selenium280 Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
-
-
-
- <spring.version>5.0.2.RELEASEspring.version>
-
- <mybatis.version>3.4.5mybatis.version>
-
- <mysql.version>5.1.44mysql.version>
-
- <pagehelper.version>5.1.2pagehelper.version>
-
- <mybatis.spring.version>1.3.1mybatis.spring.version>
-
- <commons.dbcp2.version>2.1.1commons.dbcp2.version>
- <commons.pool2.version>2.4.3commons.pool2.version>
-
- <log4j2.version>2.9.1log4j2.version>
- <log4j2.disruptor.version>3.2.0log4j2.disruptor.version>
- <slf4j.version>1.7.13slf4j.version>
-
- <junit.version>4.12junit.version>
- <servlet.version>4.0.0servlet.version>
- <lombok.version>1.18.2lombok.version>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis.spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-dbcp2artifactId>
- <version>${commons.dbcp2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- <version>${commons.pool2.version}version>
- dependency>
-
-
-
-
-
-
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>${slf4j.version}version>
- dependency>
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>jcl-over-slf4jartifactId>
- <version>${slf4j.version}version>
- <scope>runtimescope>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-slf4j-implartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>${log4j2.version}version>
- <scope>runtimescope>
- dependency>
-
-
- <dependency>
- <groupId>com.lmaxgroupId>
- <artifactId>disruptorartifactId>
- <version>${log4j2.disruptor.version}version>
- dependency>
-
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet.version}version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>${lombok.version}version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>jstlgroupId>
- <artifactId>jstlartifactId>
- <version>1.2version>
- dependency>
- <dependency>
- <groupId>taglibsgroupId>
- <artifactId>standardartifactId>
- <version>1.1.2version>
- dependency>
-
- <dependency>
- <groupId>org.seleniumhq.seleniumgroupId>
- <artifactId>selenium-javaartifactId>
- <version>3.141.59version>
- dependency>
-
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- <version>2.9.0version>
- dependency>
- <dependency>
- <groupId>org.testnggroupId>
- <artifactId>testngartifactId>
- <version>RELEASEversion>
- <scope>compilescope>
- dependency>
-
- dependencies>
-
- <build>
- <finalName>selenium280finalName>
- <pluginManagement>
- <plugins>
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- plugins>
- pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>8source>
- <target>8target>
- configuration>
- plugin>
- plugins>
- build>
- project>