Reputation: 967
I get a NoSuchBeanDefinitionException when I try to autowire a component
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.github.robertobatts.restapi.repository.OrderRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
This is the code where I try to autowire:
@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "/order", produces = "application/json")
public class OrderController {
@Autowired
private OrderRepository repository;
...
}
and this is the component:
@Component
public class OrderRepository extends InMemoryRepository<Order> {
@Override
protected void updateIfExists(Order original, Order updated) {
original.setDescription(updated.getDescription());
original.setCostInCents(updated.getCostInCents());
original.setComplete(updated.isComplete());
}
}
All the objects used by OrderRepository are standard java objects with no spring annotations.
Here is the structure of the packages:
restapi
SpringBootApplication.java
controller
OrderController.java
repository
OrderRepository.java
Upvotes: 0
Views: 78
Reputation: 7521
Please move your @SpringBootApplication
annotated class one package level up.
The annotation itself contains @ComponentScan
and ...
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.
These changes on 34ced93
gave me the expected stdout line using mvn clean spring-boot:run
.
diff --git a/src/main/java/com/github/robertobatts/restapi/main/RestApiExampleApplication.java b/src/main/java/com/github/robertobatts/restapi/RestApiExampleApplication.java
similarity index 86%
rename from src/main/java/com/github/robertobatts/restapi/main/RestApiExampleApplication.java
rename to src/main/java/com/github/robertobatts/restapi/RestApiExampleApplication.java
index bc01db5..affb928 100644
--- a/src/main/java/com/github/robertobatts/restapi/main/RestApiExampleApplication.java
+++ b/src/main/java/com/github/robertobatts/restapi/RestApiExampleApplication.java
@@ -1,3 +1,3 @@
-package com.github.robertobatts.restapi.main;
+package com.github.robertobatts.restapi;
import org.springframework.boot.SpringApplication;
diff --git a/src/main/java/com/github/robertobatts/restapi/repository/OrderRepository.java b/src/main/java/com/github/robertobatts/restapi/repository/OrderRepository.java
index b356e74..554610a 100644
--- a/src/main/java/com/github/robertobatts/restapi/repository/OrderRepository.java
+++ b/src/main/java/com/github/robertobatts/restapi/repository/OrderRepository.java
@@ -1,4 +1,6 @@
package com.github.robertobatts.restapi.repository;
+import javax.annotation.PostConstruct;
+
import org.springframework.stereotype.Repository;
@@ -8,4 +10,9 @@ import com.github.robertobatts.restapi.domain.Order;
public class OrderRepository extends InMemoryRepository<Order> {
+ @PostConstruct
+ private void test() {
+ System.out.println("OrderRepository ready.");
+ }
+
@Override
protected void updateIfExists(Order original, Order updated) {
Upvotes: 1