Home > database >  Spring configuring bean with component scan - Field userRepository in service.UserService required a
Spring configuring bean with component scan - Field userRepository in service.UserService required a

Time:12-12

I have checked many examples but they are generally about marking the classes with @Repository or @Service etc...For example enter image description here

The output is:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.practice.multiDatabase.service.UserService required a bean of type 'com.practice.multiDatabase.repository.UserRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.practice.multiDatabase.repository.UserRepository' in your configuration.

Extra notes: When I add this to main method

@SpringBootApplication(scanBasePackages = {
        "com.practice.multiDatabase.repository",
        "com.practice.multiDatabase.controller",
        "com.practice.multiDatabase.service",
        //"com.practice.multiDatabase.config"
        //, "com.practice.multiDatabase.model"
})
public class MultiDatabaseApplication {

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

}

The program is working but probably it is ignoring the config file because I have explicitly write the controller-sevice-repository and if I uncomment the config folder as well then still getting same error.

CodePudding user response:

I think you miss another important annotation for your repository to be created.

@EntityScan({"com.practice.multiDatabase.model"})

Then your entity model could be found and your repository could be successfully created!

CodePudding user response:

Omg. I have found it. I have just added

basePackages = {"com.practice.multiDatabase.repository"}

to config and it is working now.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.practice.multiDatabase.repository"},
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager")
public class UserDbConfig {
  • Related