Consider defining a bean of type in your configuration

In this post, we will see how to fix the ‘Consider defining a bean of type in your configuration’ exception. You may encounter this exception while the server starts up if you are trying to use @autowire annotation with some class/interface and spring has not created a bean for that class/interface. In most cases, if our project is divided into different modules then spring doesn’t find some classes and It doesn’t create a bean for that in that case, we face this error.

Spring Data JPA Interview Questions and Answers
How to write custom method in the repository in Spring Data JPA

Consider we have below package structure.

Consider defining a bean of type in your configuration

How to fix ‘Consider defining a bean of type in your configuration’ error.

1. We may not have been using @Service, @Repository, or @Component annotation with appropriate classes. We need to use @Service annotation with ServiceImpl class(not with interface) and @Repository annotation with Repository interface.

StudentService.java

public interface StudentService {

    public Student save(Student student);
    public Student update(Student student);
    public Student get(Long id);
    public void delete(Student student);
}

StudentServiceimpl.java – Wee need to use @Service annotation with serviceimpl class.

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;
   
   //some more code
}

Define repository interface using @Repository annotations.

@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {

}

2. If we are using Spring Data JPA, we need to use @EnableJpaRepositoriesannotation and provide the package name using ‘basePackages’ attribute, so that spring will be able to find the repository interface. If we don’t use @EnableJpaRepositories annotation Spring will not be able to find repositories classes.

package com.javatute.repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {

}

Use @EnableJpaRepository annotation properly

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class JpaConfig {

}

Note – We can also use this use with the main class where we have defined main() method.

@SpringBootApplication
@ComponentScan(basePackages = "com.javatute.*")
@EntityScan("com.javatute.*")
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

If we don’t use @EnableJpaRepositories annotation and don’t provide ‘basePackages’ attribute, spring will not be able to find repositories and it will throw “Consider defining a bean of type ‘com.javatute.repository.StudentRepository’ in your configuration error”.

Note – @EntityScan annotation is used to scan packages where we have entities. If we have entities in different packages and we don’t use @EntityScan annotation, we will end up with Not a managed type error.

3. Make sure we are using @ComponentScan annotation properly.

@SpringBootApplication
@ComponentScan(basePackages = "com.javatute.*")
@EntityScan("com.javatute.*")
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

4. If you have a separate configuration file, don’t forget to use the @Configuration annotation.

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class JpaConfig {

}

5. We can define bean using @Bean annotation manually and use that. In this approach, you can create a bean of any class. For example we are going to create studentService bean.

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class JpaConfig {

    @Bean("studentService")
    public StudentService studentService(){
        return new StudentServiceImpl();
    }

}

Now, even if we don’t use @Service annotation with our serviceimpl class, our application should be up and running.

Note – This is not recommended. Suppose we have 100 of serviceimpl then we need to define 100 bean using @Bean.

6. Make sure we have proper spring data jpa dependency in pom.xml.

    <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

7. Use @Configuration annotation with your configuration file.

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class JpaConfig {

}

Let’s see the complete working example that will not throw “Consider defining a bean of type in your configuration error”.

Define entity Student.java

package com.javatute.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "roll_number")
    private String rollNumber;

    @Column(name = "university")
    private String university;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(String rollNumber) {
        this.rollNumber = rollNumber;
    }

    public String getUniversity() {
        return university;
    }

    public void setUniversity(String university) {
        this.university = university;
    }

}

Define repository interface

package com.javatute.repository;

import com.javatute.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.io.Serializable;

@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {

}

StudentService.java

package com.javatute.service;

import com.javatute.entity.Student;
import org.springframework.stereotype.Component;

@Component
public interface StudentService {

    public Student save(Student student);

    public Student update(Student student);

    public Student get(Long id);

    public void delete(Student student);
}

JpaConfig.java

package com.javatute.config;

import com.javatute.service.StudentService;
import com.javatute.serviceimpl.StudentServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class JpaConfig {

}

StudentServiceImpl.java

package com.javatute.serviceimpl;

import com.javatute.entity.Student;
import com.javatute.repository.StudentRepository;
import com.javatute.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Transactional
    public Student save(Student student) {
        Student createResponse = studentRepository.save(student);
        return createResponse;
    }

    @Transactional
    public Student update(Student student) {
        Student updateResponse = studentRepository.save(student);
        return updateResponse;
    }

    @Transactional
    public Student get(Long id) {
        Optional<Student> response = studentRepository.findById(id);
        Student getResponse = response.get();
        return getResponse;
    }

    @Transactional
    public void delete(Student student) {
        studentRepository.delete(student);
    }
}

Define Controller class

package com.javatute.controller;

import com.javatute.entity.Student;
import com.javatute.repository.StudentRepository;
import com.javatute.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentRepository studentRepository;

    @PostMapping("/create")
    public Student createStudent1(@RequestBody Student student) {
        Student createResponse = studentService.save(student);
        return createResponse;
    }

	@PutMapping("/update")
	public Student updateStudent(@RequestBody Student student) {
		Student updateResponse = studentService.update(student);
		return updateResponse;
	}

	@GetMapping("/{id}")
	public Student getStudent(@PathVariable Long id) {
		Student getReponse = studentService.get(id);
		return getReponse;
	}

	@DeleteMapping("/delete")
	public String deleteStudent(@RequestBody Student student) {
		studentService.delete(student);
		return "Record deleted succesfully";
	}


}

SpringMain.java

package com.javatute.main;

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
@ComponentScan(basePackages = "com.javatute.*")
@EntityScan("com.javatute.*")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

With the above code changes our application should be up and running.

Consider defining a bean of type in your configuration

That’s all about Consider defining a bean of type in your configuration error.

Summary – Let’s summarise the points.

  1. Use @Service and @Repository annotation with appropriate classes.
  2. Use @EnableJpaRepositories(basePackages = “com.javatute.repository”) annotation properly with configuration file.
  3. Use @ComponentScan(basePackages = “com.javatute.*”) annotation.
  4. If the still issue persists, try to create a bean manually using the @Bean annotation in the configuration file.
  5. Make sure we have proper spring data jpa dependency.

Download code from Git.

Other Spring Data JPA and Hibernate example.