Guide to use
Guide to use
Arez Annotations is a Java library for building a responsive system. It provides some annotations and tools that can simplify the development of asynchronous event processing, status management and UI update.This article will introduce the basic concepts and usage methods of the AREZ Annitations framework, and provide some Java code examples.
1. Introduction to dependencies
Before using Arez Annotations, you need to introduce corresponding dependencies in the project construction tool.You can add the following dependencies in Maven or Gradle configuration files:
Maven:
<dependency>
<groupId>org.realityforge.arez</groupId>
<artifactId>arez-annotations</artifactId>
<version>1.0.0</version>
</dependency>
Gradle:
implementation 'org.realityforge.arez:arez-annotations:1.0.0'
Second, define a responsive component
Using the ANNOTATIONS framework, you first need to define a response component.The response component is an ordinary Java class that is marked with the `@Arezcomponent` annotation.In this category, you can define the observation attributes, calculation attributes and asynchronous operations that need to be tracked.
The following is an example:
import arez.annotations.ArezComponent;
import arez.annotations.Observable;
@ArezComponent
public class Counter {
private int count;
@Observable
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void increment() {
setCount(getCount() + 1);
}
}
In the above example, the `counter` class is marked as a response component. It has an observed attribute` Count`, a setter method to update the `count`, and there is also a method` increment` to add `to add`Count` value.
3. Use responsive components
Once a response component is defined, you can use it in other Java classes.Where to use a response component, operate through an instance of the component.
public class Main {
public static void main(String[] args) {
Counter counter = new Counter();
// Monitor the change of the count property
Arez.context().observer(() -> System.out.println("count changed: " + counter.getCount()));
counter.increment (); // Output: count change: 1
counter.increment (); // Output: count change: 2
}
}
In the above example, we created an instance of `Country`, and define a observer to monitor the changes of the attribute of the` conscontext (). Observer` method.Each time the `counter.increment () method is called, the latest` Count` value is output.
Fourth, enable Arez Annotations compiler plug -in
In order to enable Arez Annotations, the Arez Annitations compiler plug -in must be enabled in the project.This can be implemented by the corresponding configuration in the construction tool of the project.
For Maven project, add the following plug -in configuration to the `pom.xml`:
<build>
<plugins>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>3.3.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.realityforge.arez</groupId>
<artifactId>arez-annotations-processor</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
For the Gradle project, add the following plug -in configuration to `build.gradle`:
groovy
plugins {
id 'java'
id 'maven'
}
dependencies {
annotationProcessor 'org.realityforge.arez:arez-annotations-processor:1.0.0'
implementation 'org.realityforge.arez:arez-annotations:1.0.0'
}
5. Further learning
The above is the basic method of use of the ANNOTATIONS framework.If you want to understand more functions and usage of the Arez Annotations framework, you can refer to the official documentation and example code.
Official document: https://arez.github.io/arez/
Example code: https://github.com/arez/arez