Home  >  Article  >  Java  >  How to implement custom annotations in java

How to implement custom annotations in java

coldplay.xixi
coldplay.xixiOriginal
2020-08-20 11:53:495384browse

How to implement custom annotations in java: first create a new java file and customize an annotation; then use meta-annotations to define various items; and finally define the business logic of the annotation.

How to implement custom annotations in java

How to implement custom annotations in java:

1. Customize an annotation (MyValidate) and use the element The annotation defines various uses

How to implement custom annotations in java

2. Define the business logic of the annotation. Only one attribute of the annotation is implemented here: isNotNull()

How to implement custom annotations in java

Then you can use it happily

How to implement custom annotations in java

In actual projects, it is implemented through interceptors or aspects:

1. Define a interface, named BaseCheck. There is an abstract check method in BaseCheck, and the return value of the check method is boolean.

2. Define verification annotations, such as: @NotNull, @Length.

3. According to the above annotations, define corresponding verification classes, such as: NotNullCheck, LengthCheck.

4. NotNullCheck and LengthCheck both need to implement the check method of BaseCheck. You need to write the verification process in the check method.

5. Define a container and put the objects of NotNullCheck and LengthCheck into it when the project starts.

If you use spring, add an annotation @Component directly on NotNullCheck and LengthCheck. , can also achieve the same effect.

6. Define an interceptor or aspect.

7. In this interceptor or aspect, get the request parameters, which is the user object.

8. Through reflection, obtain the class corresponding to the user object. The name of the class must be User.

9. Traverse all Fields in User and check whether each Field contains annotations.

10. Traverse all annotations on the Field.

11. If you find an annotation @NotNull, look for the corresponding check class,

BaseCheck baseCheck = container.get("NotNullCheck")

or BaseCheck baseCheck = (BaseCheck) SpringUtl.getBean("NotNullCheck")

12. If found, that is, baseCheck is not empty, get the actual value of this Field through reflection, use this value as a parameter, and call baseCheck. check method

13, baseCheck.check will report an error if it returns false, and continue until all Fields and all annotations have been traversed.

Related learning recommendations: java Basic tutorial

The above is the detailed content of How to implement custom annotations in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn