首页 > 解决方案 > 在哪里应用 ApplicationContextAware

问题描述

我有一个服务类,它是一个 spring bean,我想在一个不是 spring bean 的类(A 类)中使用这个服务类。

我应该在哪里实现 ApplicationContextAware ?

以下是我的代码

   @Service("sharedListsService")
   public class  SharedListsService
   {

   }

   public class A
   {
      // I want to call my service class methods here
   }

标签: javaspring

解决方案


ApplicationContextAware仅适用于 spring bean。它将应用程序上下文注入到 bean 中。这就是为什么您不能直接使用它来将 SharedListsService 的实例放入“A”。您需要一个 bean,可能是“A”的工厂来为您接线。


推荐阅读