Can't create aspect in Spring Boot

我是研究僧i 提交于 2019-12-23 19:48:30

问题


I just want to achieve one simple thing - to create logging aspect, which should hook on every method to be able print arguments. The aspect looks like as following:

@Aspect
@Component
@Slf4j
public class MyLogger {

    public MyLogger () {}

    @AfterReturning("execution(* my.package..*.*(..))")
    public void logMethodAccessAfter(JoinPoint joinPoint) {
        System.out.println("***** Completed: " + joinPoint.getSignature().getName() + " *****");
    }

    @Before("execution(* my.package..*.*(..))")
    public void logMethodAccessBefore(JoinPoint joinPoint) {
        System.out.println("***** Starting: " + joinPoint.getSignature().getName() + " *****");
    }

}

Thing is, it makes some strange proxy out my beans and the types then do not match. I get following error:

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'downloadController' is expected to be of type 'my.package.controllers.DownloadController' but was actually of type 'com.sun.proxy.$Proxy60'

Download controller is simple @Component which is a JavaFX controller.

@Component
@Slf4j
public class DownloadController implements Initializable {

    @FXML
    private Label speedLabel;

    @FXML
    private Label appNameLabel;

    @FXML
    private ProgressBar downloadProgressBar;

    @FXML
    private Button cancelButton;

If I delete the aspect class, everything runs smoothly. Any hint?

UPDATE: I am setting up the JavaFX autowiring as following:

@Override
public void init() throws Exception {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(XAPStarterApplication.class);
    builder.headless(false);
    springContext = builder.run(XAPStarterApplication.args);
    springContext.getAutowireCapableBeanFactory().autowireBean(this);
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/MainUI.fxml"));
    fxmlLoader.setControllerFactory(springContext::getBean);
    root = fxmlLoader.load();
}

UPDATE: I uploaded the sample project in https://github.com/mejmo/aspect-javafx-issue .

来源:https://stackoverflow.com/questions/45933616/cant-create-aspect-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!