Field Required a Bean of Type That Could Not Be Found.' Error Spring Restful API Using Mongodb

Field required a bean of type that could not be found.' error spring restful API using mongodb

Solved it. So by default, all packages that falls under @SpringBootApplication declaration will be scanned.

Assuming my main class ExampleApplication that has @SpringBootApplication declaration is declared inside com.example.something, then all components that falls under com.example.something is scanned while com.example.applicant will not be scanned.

So, there are two ways to do it based on this question. Use

@SpringBootApplication(scanBasePackages={
"com.example.something", "com.example.application"})

That way, the application will scan all the specified components, but I think what if the scale were getting bigger ?

So I use the second approach, by restructuring my packages and it worked ! Now my packages structure became like this.

src/
├── main/
│ └── java/
| ├── com.example/
| | └── Application.java
| ├── com.example.model/
| | └── User.java
| ├── com.example.controller/
| | ├── IndexController.java
| | └── UsersController.java
| └── com.example.service/
| └── UserService.java
└── resources/
└── application.properties

How to fix “Field … required a bean of type … that could not be found” exception Spring Boot

You need to add to your main class a @ComponentScan annotation, telling it to scan the package of the services, otherwise it will not initialize these beans

Spring boot Field required a bean of type that could not be found

You are excluding the autoconfiguration of JPA repositories. Remove the line from application.properties to let Spring make CustomerRepository a bean and configure it.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Required a bean of type that could not be found REST API

Check this line:

@Service
public abstract class CustomerServiceImplementation implements CustomerService {...}

while it should be:

@Service
public class CustomerServiceImplementation implements CustomerService {...}

You have to implement CustomerService in a concrete class, not an abstract. Since, abstract classes can't be instantiated.

Field required a bean of type that could not be found.' error spring restful API using mongodb

Solved it. So by default, all packages that falls under @SpringBootApplication declaration will be scanned.

Assuming my main class ExampleApplication that has @SpringBootApplication declaration is declared inside com.example.something, then all components that falls under com.example.something is scanned while com.example.applicant will not be scanned.

So, there are two ways to do it based on this question. Use

@SpringBootApplication(scanBasePackages={
"com.example.something", "com.example.application"})

That way, the application will scan all the specified components, but I think what if the scale were getting bigger ?

So I use the second approach, by restructuring my packages and it worked ! Now my packages structure became like this.

src/
├── main/
│ └── java/
| ├── com.example/
| | └── Application.java
| ├── com.example.model/
| | └── User.java
| ├── com.example.controller/
| | ├── IndexController.java
| | └── UsersController.java
| └── com.example.service/
| └── UserService.java
└── resources/
└── application.properties

How to fix Field ... required a bean of type ... that could not be found exception Spring Boot

Be sure the class is scanned by spring!

(this may help if that's the problem:
Intellij Springboot problems on startup).


Optionally you may want to annotate TopicRepository as a @Repository.

@Repository
public interface TopicRepository extends CrudRepository<Topic,String>
{
}

See a demo code here: https://github.com/lealceldeiro/repository-demo

NoSuchBeanDefinitionException with reactive mongo repository: required a bean of type that could not be found

I've tried to reproduce the issue and it seems that changing the annotation

@EnableMongoRepositories(basePackages = {
"outer.package.repository"
//"local.package.repository" // temporary solution, should be external
})
public class MyConfig {}

to its reactive equivalent:

@EnableReactiveMongoRepositories(basePackages = {
"outer.package.repository"
//"local.package.repository" // temporary solution, should be external
})
public class MyConfig {}

solved the issue.
More on that in the documentation

MongoDB uses two different drivers for imperative (synchronous/blocking) and reactive (non-blocking) data access. You must create a connection by using the Reactive Streams driver to provide the required infrastructure for Spring Data’s Reactive MongoDB support. Consequently, you must provide a separate configuration for MongoDB’s Reactive Streams driver. Note that your application operates on two different connections if you use reactive and blocking Spring Data MongoDB templates and repositories.



Related Topics



Leave a reply



Submit