我的spring-boot应用程序中有以下3个模块:
@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;
因此,我在互联网上进行搜索,看到有些人遇到类似的问题。然后我得到了提示,我应该在应用程序类scanBasePackages
的SpringBootApplication
注释中添加。所以我这样做:
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
在其中找不到。
我究竟做错了什么?
我知道这么多的模块对于这么小的应用程序没有意义,但是我必须这样做,因为这是我的学徒期,我们需要制作多个模块。
通常应该做的就是在您的应用中使用这种结构
app
SpringBootApp.java
app.repositories
Repository.java
app.services
Service.java
如果您不遵循该程序包结构,则需要
@EnableJpaRepositories
并注意可能存在相同问题的实体,在这种情况下,请查看:
@EntityScan
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句