Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot says it requires a certain bean

This is the userService class that requires a bean of type com.example.repository.userRepository that could not be found

package com.example.services;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.modal.User;
import com.example.repository.userRepository;


@Service
@Transactional
public class UserService {

 @Autowired
 private userRepository userRepository;


 public UserService() {
    super();
}

public UserService(userRepository userRepository)
 {
     this.userRepository = userRepository;
 }

 public void saveMyuser(User user) {
     userRepository.save(user);
 }
}

The error message reads :

Consider defining a bean of type 'com.example.repository.userRepository' in your configuration.

This is the repository:

package com.example.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


import com.example.modal.User;


public interface userRepository extends CrudRepository<User,Integer> {

}

this is the application class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class TutorialProjectApplication {

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

}

like image 612
K.Nehe Avatar asked Dec 17 '18 12:12

K.Nehe


People also ask

How do you inject specific beans in a Spring boot?

In Spring Boot, we can use Spring Framework to define our beans and their dependency injection. The @ComponentScan annotation is used to find beans and the corresponding injected with @Autowired annotation. If you followed the Spring Boot typical layout, no need to specify any arguments for @ComponentScan annotation.

How does Spring know which bean to use?

The identifier of the parameter matches the name of one of the beans from the context (which is the same as the name of the method annotated with @Bean that returns its value). In this case, Spring chooses that bean for which the name is the same as the parameter.

What does @bean mean in Spring boot?

@Bean is a method-level annotation and a direct analog of the XML <bean/> element. The annotation supports most of the attributes offered by <bean/> , such as: init-method , destroy-method , autowiring , lazy-init , dependency-check , depends-on and scope .

Is bean id mandatory in Spring?

Sr.No. This attribute is mandatory and specifies the bean class to be used to create the bean. This attribute specifies the bean identifier uniquely. In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s).


1 Answers

Seems like userRepository interface is outside of spring-boot default scanning i.e. package of that repository interface is not same or sub-package of the class annotated with @SpringBootApplication. If so, you need to add @EnableJpaRepositories("com.example.repository") on your main class.

Update: After looking at your updated post, you need to add @EnableJpaRepositories("com.example.repository") to TutorialProjectApplication class

like image 196
Sukhpal Singh Avatar answered Sep 20 '22 21:09

Sukhpal Singh