Spring 尝试@Autowire@组件时出现异常
当我尝试自动连接组件时,会出现NoSuchBeanDefinition异常Spring 尝试@Autowire@组件时出现异常,spring,spring-boot,Spring,Spring Boot,当我尝试自动连接组件时,会出现NoSuchBeanDefinition异常 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 auto
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)}
这是我尝试自动连线的代码:
@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "/order", produces = "application/json")
public class OrderController {
@Autowired
private OrderRepository repository;
...
}
这是组件:
@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());
}
}
请将您的
@springbootplication
向上移动一个包级别
注释本身包含@ComponentScan
和
如果未定义特定的包,将从声明此注释的类的包中进行扫描
资料来源:
在
34ced93
上的这些更改使用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) {
diff--git a/src/main/java/com/github/robertobatts/restapi/main/RestApiExampleApplication.java b/src/main/java/com/github/robertobatts/restapi/RestApiExampleApplication.java
相似性指数86%
从src/main/java/com/github/robertobatts/restapi/main/RestApiExampleApplication.java重命名
重命名为src/main/java/com/github/robertobatts/restapi/RestApiExampleApplication.java
索引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 @@
-包com.github.robertobatts.restapi.main;
+包com.github.robertobatts.restapi;
导入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
索引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 @@
包com.github.robertobatts.restapi.repository;
+导入javax.annotation.PostConstruct;
+
导入org.springframework.stereotype.Repository;
@@-8,4+10,9@@import com.github.robertobatts.restapi.domain.Order;
公共类OrderRepository在MemoryRepository中扩展{
+@施工后
+专用无效测试(){
+System.out.println(“OrderRepository ready.”);
+ }
+
@凌驾
受保护的无效更新存在(订单原始,订单更新){
OrderController的包是什么?请显示主要的应用程序代码。可能您尚未配置要扫描的包,并且包布局不符合标准的spring引导布局,因此未发现您的bean。如果您的应用程序不在包的根目录中,则必须指定用于扫描的文件夹应该扫描e组件。你能提供项目的树状结构吗?我编辑了添加包结构的帖子查看我的包结构。SpringBootApplication已经在其他e包的基础上升级了,但仍然没有work@robertobatts请从com.github.robertoba移动RestApiExampleApplication
tts.restapi.main
到com.github.robertobatts.restapi
它已经在那里了。我以前发布过github代码,但我忘记提交它。它不起作用anyway@robertobatts您是否也重构了Java文件中的包名?我将自己试一试。@robertobatts我添加了我的工作差异。请试一试。我还将推荐请使用最新版本而不是构建快照,但这可能不是问题所在。
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) {