jmix icon indicating copy to clipboard operation
jmix copied to clipboard

Spring AOP and view controller

Open Fedoseew opened this issue 1 year ago • 2 comments

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");
    }
}
  1. In the second step, we annotate any public method in the view controller with an com.company.example.ExampleAnnotation
  2. 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.

Fedoseew avatar Aug 28 '23 10:08 Fedoseew