【问题标题】:Autowired service null in AspectJ aspect on a Spring Boot / Swing standalone appSpring Boot / Swing 独立应用程序上 AspectJ 方面的自动装配服务 null
【发布时间】:2019-08-25 10:33:38
【问题描述】:

我目前有一个在 Spring Boot 上运行的 Swing 应用程序,使用以下代码启动:

SpringApplication application = new SpringApplication(Something.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);

我已经为 aspectjweaver 和 spring-instrument 启用了 java 代理,并启用了加载时编织,因为我需要针对私有方法。我的方面是用于日志记录,因此我需要自动连接一个日志记录服务,该服务将条目写入数据库中的日志表。方面类使用@Aspect 和@Component 进行注释。我正在尝试使用 @Autowired 注释自动连接服务,但是当任何切入点执行时该值为 null。我认为这与实例化这些方面的 Spring 上下文之外的某种其他机制有关,但我无法弄清楚如何正确地自动装配我所需的服务。我尝试实现 ApplicationContextAware 接口,但它也不起作用。

有没有人有在 Swing 应用程序中将服务自动装配到 AspectJ 方面的经验?我有另一个在 Spring Boot 和 Spring MVC 上运行的项目,它开箱即用。

谢谢

【问题讨论】:

    标签: java swing spring-boot aspectj autowired


    【解决方案1】:

    我的问题部分与多线程有关,但我最缺少的是:

    @Bean
    public MessagingLogger messagingLogger(LogEntryService logEntryService) 
    {
        MessagingLogger aspect = Aspects.aspectOf(MessagingLogger.class);
    
        aspect.setLogEntryService(logEntryService);
    
        return aspect;
    }
    

    这就是我能够将我的服务“注入”到我的方面的方式。我的问题的第二部分结果是我在多线程设置中不够小心(这个应用程序中有很多后台进程),我试图使用方面,而 Spring 甚至还没有完成创建所有服务。

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2020-04-29
      • 2016-11-20
      • 2016-07-04
      相关资源
      最近更新 更多