I have the following 3 modules in my spring-boot application:
@SpringBootApplication
I'm now trying to inject a service in the web
module which comes from the service
. In the service I'm injecting the repository which comes from the persistence
module. When I start the application the following error shows up:
***************************
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
class:
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);
}
And that's how I inject the service into my class in the web
module:
@Autowired
private ImageService imageService;
So on I was searching throught the internet and saw some people with similar problems. Then I got the tip that I should add the scanBasePackages
to the SpringBootApplication
annotation at my application class. So I did this:
package com.project.web;
@SpringBootApplication(scanBasePackages = "com.project.service")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
But it's still not working. If I add the specific package for scanning to the annotation com.project.service.images
the injection of the ImageService
works but then it can't find the ImageRepository
in it.
What am I doing wrong?
I know that so many modules doesn't make sense for such a small application but I have to because it's for my apprenticeship and we need to make multiple modules.
What normally should do is to have this structure in your app
app
SpringBootApp.java
app.repositories
Repository.java
app.services
Service.java
If you are not following that package structure, then you need to have
@EnableJpaRepositories
And watch out for your entities which may have the same issue, in that case take a look at:
@EntityScan
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments