簡體   English   中英

存儲庫對象未在服務類中自動裝配

[英]Repository object not being Autowired in Service class

我有一個Repository ,我通過它從Elasticsearch獲取數據。 當我發送GEt請求時它工作正常。 我需要來自Elasticsearch 的數據而不發送GET請求,我已經為其編寫了一個Service並使用@AutowiredService類中注釋了該存儲庫。
存儲庫

@Repository
public interface RecipeDtoRepository extends 
ElasticsearchRepository<RecipeDto, Integer> {}

控制器

@RestController
@RequestMapping("/repo")
public class RecipeRepoController {
@Autowired
public RecipeDtoRepository recipeDtoRepository;

@GetMapping("/all")
public String getRecipes() {
    List<RecipeDto> recipe_list = new ArrayList<>();
    recipeDtoRepository.findAll().forEach(recipe_list::add);
    return recipe_list.toString();
}

服務

@Service
@Component
public class ClusterService implements ApplicationContextAware {
  @Autowired
public RecipeDtoRepository recipeDtoRepository;
List<RecipeDto> recipe_list = new ArrayList<>();
   new ClusterService(). applicationContext.getBean(RecipeRepoController.class).recipeDtoRepository.findAll().forEach(recipe_list::add);
}
@Override
public void setApplicationContext(ApplicationContext 
applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
}

使用GET請求調用控制器中的方法時出現問題,它工作正常但是當我使用Service類調用它時,它拋出NullPointerException根據我的知識,它不應該是。 我讀過一篇文章,建議在Service類中獲取Bean ,您可以看到我已經使用ApplicationContext完成了,但它拋出了該異常。 我在不使用ApplicationContext情況下嘗試過它,在這種情況下它也會拋出NullPointerException 我過去兩天一直在研究它,但無法找到解決方案。

@Service
@Configurable
public class ClusterService {
@Autowired
public  RecipeDtoRepository recipeDtoRepository;

private  String getRecipes() {
    List<RecipeDto> recipe_list = new ArrayList<>();
    recipeDtoRepository.findAll().forEach(recipe_list::add);
    return recipe_list.toString();
}


public static void main(String[] a) {

    ClusterService clusterService = new ClusterService();
    clusterService.getRecipes();

}}

刪除 contextAware 並且不要從控制器中調用存儲庫的實例,因為您已經注入了它,但只需執行此操作

public void addThings(){
this.recipeDtoRepository.findAll().forEach(recipe_list::add)
}

如果您使用構造型注釋對您的類進行了注釋,並且您正試圖在主類中獲取 bean,請嘗試此操作

     ApplicationContext context= 
           SpringApplication.run(Main.class,args);
                ClusterService clusterService=context.getBean(ClusterService.class);
clusterService.getRecipes();

假設 OP 想要保留一個列表並在整個生命周期中使用它。 這是我的建議

@Service
public class ClusterService {
    @Autowired
    public RecipeDtoRepository recipeDtoRepository;

    private List<RecipeDto> recipeList = null;

    @PostContruct
    public void setup() {
       recipeList = new ArrayList<>();
       recipeDtoRepository.findAll().forEach(recipeList::add);
    }

    public getReceipeList() {
      return recipeList;
    }

}

@RestController
@RequestMapping("/repo")
public class RecipeRepoController {
    @Autowired
    ClusterService clusterService

    @GetMapping("/all")
    public String getRecipes() {
        return clusterService.getReceipeList().toString();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM