簡體   English   中英

Spring 引導應用程序無法從另一個模塊注入 Bean

[英]Spring Boot Application can not inject Bean from another module

我的 spring-boot 應用程序中有以下 3 個模塊:

  • web(入口點/主要應用程序 class 用@SpringBootApplication注釋
  • 堅持
  • 服務

我現在正在嘗試在來自 service 的web模塊中注入service 在服務中,我正在注入來自persistence模塊的存儲庫。 當我啟動應用程序時,出現以下錯誤:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.project.service.images.ImageService required a bean of type 'com.project.persistence.repositories.ImageRepository' that could not be found.


Action:

Consider defining a bean of type 'com.project.persistence.repositories.ImageRepository' in your configuration.

圖像服務ImageService

package com.project.service.images;

import com.project.common.entities.Image;
import com.project.persistence.repositories.ImageRepository;
import com.project.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityNotFoundException;
import java.util.Date;
import java.util.List;

@Component
public class ImageService extends AbstractService {

    private final ImageRepository imageRepository;

    @Autowired
    public ImageService(ImageRepository imageRepository) {
        this.imageRepository = imageRepository;
    }

    public Image getImage(Long id) {
        return imageRepository.findById(id).orElseThrow(EntityNotFoundException::new);
    }

    public List<Image> getAll() {
        return imageRepository.findAll();
    }

    public List<Image> getAll(Date from) {
        return imageRepository.findByDateRange(from, null);
    }

    public List<Image> getAll(Date from, Date to) {
        return imageRepository.findByDateRange(from, to);
    }

    public List<Image> getAllForDay(Date day) {
        return imageRepository.findAll();
    }
}

ImageRepository class:

package com.project.persistence.repositories;

import com.project.common.entities.Image;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface ImageRepository extends JpaRepository<Image, Long> {

    @Query("SELECT i FROM Image i WHERE i.created > :from AND i.created < :to")
    public List<Image> findByDateRange(@Param("from") Date from, @Param("to") Date to);
}

這就是我將服務注入web模塊中的 class 的方式:

@Autowired
private ImageService imageService;

所以我在網上搜索,看到一些人有類似的問題。 然后我得到提示,我應該將scanBasePackages添加到我的應用程序 class 的SpringBootApplication注釋中。 所以我這樣做了:

package com.project.web;

@SpringBootApplication(scanBasePackages = "com.project.service")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

但它仍然無法正常工作。 如果我將用於掃描的特定 package 添加到注釋com.project.service.images中,則ImageService的注入有效,但隨后無法在其中找到ImageRepository

我究竟做錯了什么?

我知道這么多模塊對於這么小的應用程序沒有意義,但我必須這樣做,因為這是我的學徒期,我們需要制作多個模塊。

只需嘗試將 scanBasePackages 更改為“com.project”。 存儲庫位於不同的 package 中。

例如:

@SpringBootApplication(scanBasePackages = "com.project")

通常應該做的是在您的應用程序中具有此結構

app
   SpringBootApp.java
   app.repositories
       Repository.java
   app.services
       Service.java

如果您沒有遵循 package 結構,那么您需要

@EnableJpaRepositories

並注意可能有相同問題的實體,在這種情況下,請查看:

@EntityScan

Spring 無法掃描您的存儲庫 class,因為它位於不同的 package 中。

根據您在評論中的回復,您的應用程序 class 在

com.project.web

,所以默認情況下 Spring 會掃描這個包和子包下的所有類。 因此,您需要將所有 spring 組件放在應用程序所在的同一包/子 package 下。

創建一個配置 class,並在該位置定義所有 bean,在這種情況下,您需要 ImageRepository 的 bean,類似於...

@Configuration
@ComponentScan
public class Config {
    @Bean
    public ImageRepository getImageRepository() {
        // return the image repository object
    }
}

暫無
暫無

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

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