如何修复 "Field ... required a bean of type ... that could not be found" 异常 Spring 引导
How to fix "Field ... required a bean of type ... that could not be found" exception Spring Boot
我正在使用来自 javabrains 的 spring 引导教程,在将 CrudRepository
放入项目之前一切都很清楚。下面你可以找到我的主要 class:
package pl.springBootStarter.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CourseApiDataApplication
{
public static void main(String args[])
{
SpringApplication.run(CourseApiDataApplication.class,args);
}
}
服务class:
package pl.springBootStarter.app.topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class TopicService
{
@Autowired
private TopicRepository topicRepository;
private List<Topic> topics = new ArrayList<>(Arrays.asList(
new Topic("spring","spring framework", "spring framework dectription"),
new Topic("sprin","spring framework", "spring framework dectription"),
new Topic("spri","spring framework", "spring framework dectription")));
public List<Topic> getAllTopics()
{
// return topics;
List<Topic> t = new ArrayList<Topic>();
topicRepository.findAll().forEach(t::add);
return t;
}
public Topic getTopic (String id)
{
return topics.stream().filter( t -> t.getId().equals(id)).findFirst().get();
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(Topic topic, String id)
{
topics.set(topics.indexOf(topics.stream().filter(t-> t.getId().equals(id)).findFirst().get()), topic);
}
public void deleteTopic(String id)
{
topics.remove(topics.stream().filter(t -> t.getId().equals(id)).findFirst().get());
}
}
和Repository
接口:
package pl.springBootStarter.app.topic;
import org.springframework.data.repository.CrudRepository;
public interface TopicRepository extends CrudRepository<Topic,String>
{
}
当我 运行 应用程序将 TopicRepository
注入 TopicService
class 中的 topicRepository
字段时出现问题。我收到以下错误:
Error starting ApplicationContext. To display the conditions report re- run your application with 'debug' enabled.
2019-05-01 10:33:52.206 ERROR 6972 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field topicRepository in pl.springBootStarter.app.topic.TopicService required a bean of type 'pl.springBootStarter.app.topic.TopicRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Spring 无法自动装配的原因是什么?
确保 class 被 spring 扫描!
(如果这是问题所在,这可能会有所帮助:
).
您可能希望将 TopicRepository
注释为 @Repository
。
@Repository
public interface TopicRepository extends CrudRepository<Topic,String>
{
}
Spring 无法注入 bean,因为它还没有被创建。
您必须指示 Spring 通过在您的任何配置 classes 或 class 上使用 @EnableJpaRepositories(basePackages={"pl.springBootStarter.app"})
注释来生成声明的存储库接口的实现11=]。那应该可以解决您的问题。
对于通过谷歌搜索通用 bean 错误消息被带到这里,但实际上正在尝试添加 feign 客户端 的任何人他们的 Spring 通过客户端界面上的 @FeignClient
注释启动应用程序,上述解决方案中的 none 将适用于您。
要解决此问题,您需要将 @EnableFeignClients
注释添加到您的应用程序 class,如下所示:
@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {
我收到了一条类似的消息,但我在服务 class 中缺少 @Service 注释。一个简单的错误,发帖以防对其他人有帮助。
我收到了类似的消息。
我的主要包裹是 com.example
其他 classes 的包裹是 com.xyz
所以当我将其他class的包名更改为com.example.topic
即
最后
主包是 com.example
另一个 class 的包裹是 com.example.topic
一个简单的错误,发布以防对其他人有帮助。
我正在使用来自 javabrains 的 spring 引导教程,在将 CrudRepository
放入项目之前一切都很清楚。下面你可以找到我的主要 class:
package pl.springBootStarter.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CourseApiDataApplication
{
public static void main(String args[])
{
SpringApplication.run(CourseApiDataApplication.class,args);
}
}
服务class:
package pl.springBootStarter.app.topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class TopicService
{
@Autowired
private TopicRepository topicRepository;
private List<Topic> topics = new ArrayList<>(Arrays.asList(
new Topic("spring","spring framework", "spring framework dectription"),
new Topic("sprin","spring framework", "spring framework dectription"),
new Topic("spri","spring framework", "spring framework dectription")));
public List<Topic> getAllTopics()
{
// return topics;
List<Topic> t = new ArrayList<Topic>();
topicRepository.findAll().forEach(t::add);
return t;
}
public Topic getTopic (String id)
{
return topics.stream().filter( t -> t.getId().equals(id)).findFirst().get();
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(Topic topic, String id)
{
topics.set(topics.indexOf(topics.stream().filter(t-> t.getId().equals(id)).findFirst().get()), topic);
}
public void deleteTopic(String id)
{
topics.remove(topics.stream().filter(t -> t.getId().equals(id)).findFirst().get());
}
}
和Repository
接口:
package pl.springBootStarter.app.topic;
import org.springframework.data.repository.CrudRepository;
public interface TopicRepository extends CrudRepository<Topic,String>
{
}
当我 运行 应用程序将 TopicRepository
注入 TopicService
class 中的 topicRepository
字段时出现问题。我收到以下错误:
Error starting ApplicationContext. To display the conditions report re- run your application with 'debug' enabled.
2019-05-01 10:33:52.206 ERROR 6972 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field topicRepository in pl.springBootStarter.app.topic.TopicService required a bean of type 'pl.springBootStarter.app.topic.TopicRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Spring 无法自动装配的原因是什么?
确保 class 被 spring 扫描!
(如果这是问题所在,这可能会有所帮助:
您可能希望将 TopicRepository
注释为 @Repository
。
@Repository
public interface TopicRepository extends CrudRepository<Topic,String>
{
}
Spring 无法注入 bean,因为它还没有被创建。
您必须指示 Spring 通过在您的任何配置 classes 或 class 上使用 @EnableJpaRepositories(basePackages={"pl.springBootStarter.app"})
注释来生成声明的存储库接口的实现11=]。那应该可以解决您的问题。
对于通过谷歌搜索通用 bean 错误消息被带到这里,但实际上正在尝试添加 feign 客户端 的任何人他们的 Spring 通过客户端界面上的 @FeignClient
注释启动应用程序,上述解决方案中的 none 将适用于您。
要解决此问题,您需要将 @EnableFeignClients
注释添加到您的应用程序 class,如下所示:
@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {
我收到了一条类似的消息,但我在服务 class 中缺少 @Service 注释。一个简单的错误,发帖以防对其他人有帮助。
我收到了类似的消息。
我的主要包裹是 com.example 其他 classes 的包裹是 com.xyz
所以当我将其他class的包名更改为com.example.topic
即 最后 主包是 com.example 另一个 class 的包裹是 com.example.topic
一个简单的错误,发布以防对其他人有帮助。