jmix-framework / jmix

Jmix framework
https://www.jmix.io
Apache License 2.0
585 stars 122 forks source link

Spring AOP and view controller #2173

Open Fedoseew opened 1 year ago

Fedoseew commented 1 year ago

General description of the problem:

At the moment, if we want to use a simple boxed approach to writing aspects, then we will face the problem that writing aspects that will belong to the view controller class is impossible. Since when creating a proxy object, it will not have an @ViewController annotation and the screen will not open. This occurs because the @ViewController annotation is not annotated with the @Inherited annotation.

Technical details:

  1. First of all we need to create simple aspect like this:
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

@Aspect @Component public class ExampleAspect {

private static final Logger log = LoggerFactory.getLogger(ExampleAspect.class);

@Before(value = "execution(* com.company.demo.view.user.UserListView.onInit(..))")
public void onInitInLogView(JoinPoint joinPoint) {
    log.info("onInit AOP");
}

}


2. In the second step, we annotate any public method in the view controller with an com.company.example.ExampleAnnotation
3. As a result, we will get a view creation error when we try to go to the view associated with controller.

## **Solution:**
Add `@Inherited` annotation to `@ViewController` annotation.
ntha79 commented 2 weeks ago

When we making jmix application for customers like banks, always need track every actions of users on application screens. For example: What and when user open screen, when they close screen,... AOP is much more simple for this task. Please take care about this function.

glebfox commented 2 weeks ago

Simply adding the @Inherited annotation to the @ViewController annotation helps partially. If the target method uses fields annotated with @ViewComponent, they are not initialized. Thus, we need to teach our injectors to take AOP into account.

To simply track opening and closing of views, global application events can be used:

Also, we have view monitoring: https://github.com/jmix-framework/jmix/issues/1704#issuecomment-1943207017