Introduction to AOP (programming) features in GIN (GWT Injection) framework
Gin (GWT Injection) is a lightweight framework for building a Java front -end application based on Google Web Toolkit (GWT).It provides a series of functions and features, one of which is to facing cut -oriented programming (AOP).This article will introduce the AOP features in the Gin framework and provide the corresponding Java code example.
Facial programming is a software development technology. By defining the cut surface (Aspect) and separating them from the main business logic in the application, the modularization of horizontal cutting attention points is achieved.The cut surface can be considered a functional module that spans multiple objects, such as log records, performance statistics, security control, etc.AOP can be separated from the main business logic through the main business logic, which can make the code clearer and modular.
To use AOP in the Gin framework, you first need to define a cut surface.The cut surface is an object that contains notifications and pointcut.The notification is the functional logic provided by the cut surface, and the cut point determines when to call the notification of the cut surface during the execution of the application.
The following is a simple example of cutting surface:
import com.google.gwt.inject.client.GinModule;
import com.google.inject.matcher.Matchers;
import com.google.inject.Provides;
public class LoggingAspect implements GinModule {
@Override
public void configure(Binder binder) {
// Perform the corresponding configuration here
}
@Provides
public Logger getLogger() {
return new Logger();
}
@Provides
public LoggingInterceptor getLoggingInterceptor(Logger logger) {
return new LoggingInterceptor(logger);
}
@Override
public void install(com.google.gwt.inject.client.GinBinder binder) {
binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Loggable.class), getLoggingInterceptor(logger));
}
}
The `Loggingaspect` class in the above code example is a cut surface, which is related to the cut surface in the` Configure` method, and bind the cut surface to the class that needs to be applied through the `Install` method.Through the `bindInterceptor` method, we can specify the cut point, and select the method of cutting the surface function when you need to be called.
In addition, we can define a custom annotation `loggable` to mark the method of using the cut surface.In the cut surface, all the class matches, and the method of marking the annotation of the `Loggable` will use the cutting function.
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Loggable {
}
Use the sample of cutting surface:
public class UserService {
@Loggable
public void addUser(User user) {
// Add user logic
// The notification in Loggingaspect will be triggered here
}
}
In the above examples, the `adduser` method is marked by the annotation of`@loggable`, which means that this method will trigger notifications configured in the `loggingaspect`.
By using the AOP characteristics provided by the GIN framework, we can easily separate the universal functional logic and business logic to improve the readability and modularity of the code, so as to better manage and maintain our application.
Summary: The GIN framework is a GWT -based Java front -end application framework. The AOP feature allows us to use cutting programming to modify the common functional logic.By defining cutting surfaces, cutting points, and notifications, and using custom annotation markers to apply cutting surface, we can easily separate the universal functional logic and business logic to improve the readability and maintenance of the code.
It is hoped that this article will help understand the AOP characteristics in the Gin framework and show how to apply AOP in the Gin framework through the example code.