The technical principles of the Sundrio :: Annotations :: Builder framework in the java class library
The technical principles of Sundrio :: Annotations :: Builder framework
Sundrio :: Annotations :: Builder is a widely used framework in the Java library. Its technical principles are based on the Java annotation and constructor mode.The purpose of the framework is to provide developers with a convenient way to generate an example of annotations and set the various attributes of the annotation.
In Java, the annotation is a metadata data that can be used to declare and constraint the class, methods, parameters, etc. in the restraint program.The annotation can be accessed by reflection at runtime, and corresponding treatment is performed according to the definition of the annotation.However, because the annotation itself does not provide a constructor or attribute setting method, it is not intuitive to create and set the attribute value of the annotation.
Sundrio :: Annotations :: Builder framework solves this problem by introducing the construct mode.The constructor mode is a creation design model of creating complex objects. By separating the creation and configuration process of the object, the code is more concise and easy to read.In this case, the Sundrio :: Annotations :: Builder framework provides a constructor class that encapsulates the instance of the annotation and provides a set of convenient methods for setting the annotation attributes.
Using Sundrio :: Annotations :: Builder framework, we first need to add related dependencies to the project.Then, where we need to use this framework, we can create an instance of the builder and use the method provided by it to set the annotation of various attributes.Finally, by calling the build () method of the builder, we can obtain the final generating example.
Below is a simple sample code for Sundrio :: Annotations :: Builder framework to create an annotation instance called @exampleannotation and set two attributes:
// Add Maven dependence
<dependency>
<groupId>io.sundr</groupId>
<artifactId>annotations-builder</artifactId>
<version>1.3.9</version>
</dependency>
// Introduce the relevant class
import io.sundr.builder.annotations.ExternalBuilderInterface;
import io.sundr.builder.annotations.ExternalBuildables;
import io.sundr.builder.annotations.ExternalBuilder;
// Definition example Note
@ExternalBuilderInterface
@ExternalBuildables(
editableEnabled = true,
builderPackage = "com.example.annotation.builders"
)
@ExternalBuilder
public @interface ExampleAnnotation {
String name();
int version() default 1;
}
// Use the constructor to create an annotation example
ExampleAnnotation annotation = new ExampleAnnotationBuilder()
.withName("Test")
.withVersion(2)
.build();
In the above examples, we first introduced Sundrio :: Annitations :: Builder framework by adding Maven dependencies.Then, we defined an annotation named @Exampleannotion, and used@ExternalBuilderInterface,@ExternalBuildables, and @ExternalBuilder to configure the framework.Finally, we use ExampleanNotationBuilder to create an annotation instance and set up two properties: name and version.
Through the Sundrio :: Annotations :: Builder framework, developers can more conveniently create and configure the examples of Java annotations.The technical principle of the framework is to combine the constructive mode with the Java annotation, providing a more intuitive and concise way to generate annotations.