Spring Boot Application can not inject Bean from another module

thmspl :

I have the following 3 modules in my spring-boot application:

  • web (Entry point / Main Application class annotated with @SpringBootApplication
  • persistence
  • service

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.

arturo.bhn :

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to launch Spring Boot app from another Maven module?

Spring Boot - inject map from application.yml

Inject nestjs service from another module

How can I inject dependency from a library in Spring Boot 2?

Spring 3: Inject Default Bean Unless Another Bean Present

How do I inject property from one bean into another in Spring 3.0?

How to import Spring application context from another maven module?

How to access Spring Boot Quartz Bean from another module?

How to Inject Dependencies into a Servlet Filter with Spring Boot Filter Registration Bean?

How to add a bean dependency from another module in Spring?

How to inject a map from application.yml into a field in Spring Boot?

How to inject Spring boot Environment bean to Custom Spring xml bean?

Spring - how to inject components from another module into a SpringBoot application

Spring Boot: Inject Bean into HttpServlet

Can't inject multiple ObjectMapper beans in Spring Boot application

Spring inject property values from a bean into another bean

How to access in memory h2 database of one spring boot application from another spring boot application

Inject with Spring Boot depending on properties from application.yml

How to inject RequestScope bean in spring boot wicket application

Spring - Inject bean from a factory class

How to inject dataSource default bean in Spring Boot integration test

unable to find spring bean xml file in another module of application

How Can One Include Resources From Another Module In A Spring Boot Maven Multi Module Project

Spring Boot Inject Bean Map with Generic Type

how can I consume rest api of one module from other module in same application using spring boot application without module dependency

Can I start another application(startup Redis Cache server) from inside of another application(Spring boot or Micronaut application)?

Spring Boot inject List of Maps from application.yml

how can application yaml value inject at runtime in spring boot?

Inject custom properties folder in Spring Boot Application