Spring Boot Application无法从另一个模块注入Bean

thmspl:

我的spring-boot应用程序中有以下3个模块:

  • web(入口点/主应用程序类带有注释 @SpringBootApplication
  • 坚持不懈
  • 服务

我现在正尝试在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 类:

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模块中的类的方式

@Autowired
private ImageService imageService;

因此,我在互联网上进行搜索,看到有些人遇到类似的问题。然后我得到了提示,我应该在应用程序类scanBasePackagesSpringBootApplication注释中添加所以我这样做:

package com.project.web;

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

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

但它仍然无法正常工作。如果我将用于扫描的特定程序包添加到注解中,com.project.service.images则注入了ImageService作品,但ImageRepository在其中找不到

我究竟做错了什么?

我知道这么多的模块对于这么小的应用程序没有意义,但是我必须这样做,因为这是我的学徒期,我们需要制作多个模块。

arturo.bhn:

通常应该做的就是在您的应用中使用这种结构

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

如果您不遵循该程序包结构,则需要

@EnableJpaRepositories

并注意可能存在相同问题的实体,在这种情况下,请查看:

@EntityScan

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从另一个模块访问Spring Boot Quartz Bean?

Spring-Boot多模块无法从另一个模块读取属性文件

如何从另一个Maven模块启动Spring Boot应用程序?

Spring Boot application.properties扩展了另一个属性文件

如何在 Spring Boot Maven 多模块项目中包含来自另一个模块的资源

Spring将一个bean的属性值注入另一个bean

Spring boot / Application.java如何在另一个包中提取Mongo AbstractMongoConfiguration?

Spring Boot无法连接到部署在另一个kubernetes pod上的postgre数据库

无法在另一个应用程序模块中找到spring bean xml文件

在Spring 3.0中如何将属性从一个bean注入另一个bean?

一个从属Spring Boot项目application.properties不注入默认值

将Spring Boot应用程序添加为对另一个Spring Boot应用程序的依赖

通过另一个Spring Boot Webapp对Spring Boot Webapp进行运行状况检查

Spring Boot CrudRepo定义一个bean

在配置spring boot中定义一个bean名称

在Spring-Boot中从服务器调用另一个Rest API

Spring Boot:用另一个替换 @Configuration 类

Spring Boot和Thymeleaf-到另一个站点的“ a href”进入404错误

传递给另一个线程的 Spring Boot JPA 存储库不起作用

如何在另一个项目中向Spring Boot Jar添加依赖项?

包含来自另一个项目 Spring boot gradle Intellij 的包

如何在Spring Boot Thymeleaf模板引擎中链接另一个html页面?

Spring Boot从请求中获取承载令牌并调用另一个微服务

将Spring Boot应用程序导入另一个项目

Spring Boot将特定参数移交给另一个API

Spring Boot - Mongodb - 基于另一个字段的自动递增字段

Spring Boot将值从控制器更改为另一个

Spring Boot在另一个程序包上找到自动接线

Spring Boot-调用另一个需要证书的Web服务