Field Required a Bean Which Could Not Be Found in Springboot

'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

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

Field required a bean which could not be found in springboot

The problem was with pom.xml only. I just replaced my pom with the following and everything worked.

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>anonReporting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>anonReporting</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.aerospike</groupId>
<artifactId>spring-data-aerospike</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

Field required a bean of type that could not be found

It doesn't make any sense and is actual dangerous in this case, to autowire the ItemResponse.

By default beans in Spring are singletons, so there is now a single instance of the ItemResponse. Now imagine 50 concurrent threads changing the single instance of ItemResponse, what output do you think each thread will have?

In your case the only proper solution is to remove the autowired field, and simply construct a new ItemResponse inside the addSingleItem method and return that. That way there is no shared state.

@Service
public class StoreService {

private List<ItemDetails> itemDetailsList = new ArrayList<>(Arrays.asList(
new ItemDetails(1,"5Rs GOODDAY Biscuit",5.00,"SWEET"),
new ItemDetails(2,"10Rs GOODAY Biscuit,",10.00,"SALTY"),
new ItemDetails(3,"25Rs GOODAY Biscuit",25.00,"CREAMY")
));

public List<ItemDetails> getItemDetails()
{
return itemDetailsList;
}
public ItemDetails getSingleItem(Integer id) {
return itemDetailsList.stream().filter(n->n.getItemId().equals(id)).findFirst().get();
}

public ItemResponse addSingleItem(ItemDetails itemDetails) {
itemDetailsList.add(itemDetails);
ItemResponse itemResponse = new ItemResponse();
itemResponse.setMessage("Item added successfully");
return itemResponse;
}
}


Related Topics



Leave a reply



Submit