簡體   English   中英

字段需要一個在通用JPA DAO體系結構中找不到的類型的bean

[英]Field required a bean of type that could not be found on Generic JPA DAO architecture

我正在嘗試在春季啟動時為我的項目定義架構

我要做的是創建一個從JpaRepository擴展的通用存儲庫

public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
}

之后,每個EntityDao將從BaseRepository擴展

@Repository    
public interface AuthorityDao extends BaseRepository<Authority, Long> {

        Authority findById(Long idRole);

        Authority findByRoleName(String findByRoleName);

    }

這就是我在存儲庫層上執行此操作的方式。 在服務層,我創建一個名為GenericService的類,該類實現IGenericService並將BaseRepository注入其中:

@Service
public class GenericService<T, D extends Serializable> implements IGenericService<T, D> {

    @Autowired
    @Qualifier("UserDao")
    private BaseRepository<T, D> baseRepository;
// implemented method from IGenericService

}

每個服務將從GenericService擴展:

public class AuthorityService extends GenericService<Authority, Long> implements IAuthorityService {

    @Autowired
    GenericService<Authority, Long> genericService;

運行項目時,出現以下錯誤:


申請開始失敗


描述:
fr.java.service.impl.GenericService中的字段baseRepository需要找不到類型為“ fr.config.daogeneric.BaseRepository”的bean。

行動:
考慮在配置中定義類型為“ fr.config.daogeneric.BaseRepository”的bean。

我怎么解決這個問題?

更新:

@SpringBootApplication
@EntityScan("fr.java.entities")
@ComponentScan("fr.java")
@EnableJpaRepositories("fr.java")
@EnableScheduling
@EnableAsync
@PropertySource({ "classpath:mail.properties", "classpath:ldap.properties" })
@EnableCaching
@RefreshScope
public class MainApplication extends SpringBootServletInitializer {

    private static final Logger log = LoggerFactory.getLogger(MainApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainApplication.class);
    }

    public static void main(String[] args) {
        log.debug("Starting {} application...", "Java-back-end-java");

        SpringApplication.run(MainApplication.class, args);
    }

}

您有這個問題,是因為您將GenericService創建為bean並嘗試注入BaseRepository但是Spring無法做到這一點,因為不清楚哪個類對BaseRepository進行了參數設置。

從我的角度來看,我可以建議您下一步:首先,您不應該將GenericService用作bean,他的所有子代都應該是bean,您應該在子類中刪除對GenericService注入,它們已經擴展了。 您的GenericService應該是抽象的,並且可以有抽象方法getRepository ,該方法將在GenericService內使用,並且存儲庫的注入將在GenericService子類中完成。

所以你應該有這樣的東西:

public abstract class GenericService<T, D extends Serializable> implements IGenericService<T,D> {
    abstract BaseRepository<T, D> getRepository();
}

@Service
public class AuthorityService extends GenericService<Authority, Long> implements IAuthorityService {

    @Autowired
    BaseRepository<Authority, Long> baseRepository;

    public BaseRepository<Authority, Long> getRepository() {
        retrurn baseRepository;
    }
}

暫無
暫無

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

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