In project development, annotations are used everywhere. The use of annotations simplifies the code and reduces the programmer's workload. This course leads students to have a comprehensive understanding of Java annotations, including why annotations are used, common annotations in Java, classification of annotations and how to customize annotations. Finally, a practical case is used to demonstrate the application of annotations in actual projects.
Course playback address: //m.sbmmt.com/course/293.html
The teacher’s teaching style:
The teacher’s lectures are simple, clear, layer-by-layer analysis, interlocking, rigorous argumentation, rigorous structure, and use the logical power of thinking to attract students’ attention Strength, use reason to control the classroom teaching process. By listening to the teacher's lectures, students not only learn knowledge, but also receive thinking training, and are also influenced and influenced by the teacher's rigorous academic attitude
The more difficult point in this video is the annotation in the JDK:
Understand annotations
For Java developers, when writing code, in addition to the source program, we will also use Javadoc tags to describe classes, methods or member variables Make comments so that you can use the Javadoc tool to generate Javadoc documentation that matches the source code. These Javadoc tags such as @param and @return are annotation tags, which provide third-party tools with annotation information describing the program code. Friends who have used Xdoclet will be more impressed by this. Struts and Hibernate both provide Xdoclet tags, which can be used to quickly generate configuration files corresponding to the program code.
JDK5.0 annotations can be seen as the extension and development of Javadoc tags and Xdoclet tags. In JDK5.0, we can customize these tags and obtain the annotations marked in the class through the reflection mechanism of the Java language to complete specific functions.
Annotations are ancillary information of the code. They follow a basic principle: annotations cannot directly interfere with the running of the program code. No matter whether annotations are added or deleted, the code can run normally. The Java language interpreter ignores these annotations, and third-party tools are responsible for processing the annotations. Third-party tools can use annotations in the code to indirectly control the running of the program code. They read the annotation information through the Java reflection mechanism and change the logic of the target program based on this information. This is what Spring AOP does to support @AspectJ. Methods.
A simple annotation class
Usually, third-party tools are not only responsible for processing specific annotations, but also provide the definitions of these annotations, so we usually only need to focus on how to Just use annotations. But defining an annotation class itself is not difficult. Java provides a syntax for defining annotations. Next, we immediately start writing a simple annotation class, as shown in Code Listing 7-1:
Code Listing 7-1 NeedTest Annotation Class
package com.baobaotao.aspectj.anno; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) //①声明注解的保留期限 @Target(ElementType.METHOD)//②声明可以使用该注解的目标类型 public @interface NeedTest {//③定义注解 boolean value() default true;//④声明注解成员 }
The above is the detailed content of Recommended video tutorial materials that comprehensively analyze Java annotations. For more information, please follow other related articles on the PHP Chinese website!