Java 考虑定义一个类型的豆和*x27;服务';在您的配置中[Spring boot]
我运行主类时出错 错误:Java 考虑定义一个类型的豆和*x27;服务';在您的配置中[Spring boot],java,spring,spring-boot,spring-data,spring-data-jpa,Java,Spring,Spring Boot,Spring Data,Spring Data Jpa,我运行主类时出错 错误: Action: Consider defining a bean of type 'seconds47.service.TopicService' in your configuration. Description: Field topicService in seconds47.restAPI.topics required a bean of type 'seconds47.service.TopicService' that could not be foun
Action:
Consider defining a bean of type 'seconds47.service.TopicService' in your configuration.
Description:
Field topicService in seconds47.restAPI.topics required a bean of type 'seconds47.service.TopicService' that could not be found
TopicService接口:
public interface TopicService {
TopicBean findById(long id);
TopicBean findByName(String name);
void saveTopic(TopicBean topicBean);
void updateTopic(TopicBean topicBean);
void deleteTopicById(long id);
List<TopicBean> findAllTopics();
void deleteAllTopics();
public boolean isTopicExist(TopicBean topicBean);
}
实现类:
public class TopicServiceImplementation implements TopicService {
@Autowired
private TopicService topicService;
@Autowired
private TopicRepository topicRepository;
@Override
public TopicBean findById(long id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public TopicBean findByName(String name) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void saveTopic(TopicBean topicBean) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void updateTopic(TopicBean topicBean) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void deleteTopicById(long id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List<TopicBean> findAllTopics() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void deleteAllTopics() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isTopicExist(TopicBean topicBean) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
我的包裹是这样的:
seconds47
seconds47.beans
seconds47.config
seconds47.repository
seconds47.restAPI
seconds47.service
您正在尝试将bean注入自身。这显然行不通
TopicService实施
实施TopicService
。该类尝试自动连接(按字段!)一个“TopicService”。所以你本质上要求上下文注入它自己
看起来您已经编辑了错误消息的内容:字段topicService in seconds47.restAPI.topics
不是类。如果您需要隐藏敏感信息,请小心,因为这会使其他人更难帮助您
回到实际问题上来,注入
TopicService
本身似乎是您的一个小故障 类必须具有@组件
注释或其派生(如@服务
,@存储库
等),才能通过组件扫描识别为SpringBean。因此,如果您将@Component
添加到类中,它应该可以解决您的问题。由于TopicService
是一个服务
类,您应该使用@Service
对其进行注释,以便Spring为您自动连接这个bean。像这样:
@Service
public class TopicServiceImplementation implements TopicService {
...
}
这将解决您的问题。您必须更新您的
scanBasePackages = { "com.exm.java" }
要将路径添加到您的服务中(在用@service注释它之后)我通过替换损坏的jar文件解决了这个问题 但要找到那些损坏的jar文件,我必须在三个IDE中运行我的应用程序—1)Intellij Idea 2)NetBeans 3)Eclipse Netbeans为我提供了损坏jar的最大数量的信息。在Netbeans和run中,我使用build选项(在右键单击project之后)来了解更多关于损坏的jar的信息
我花了15个多小时才找到这些错误的根本原因。希望它对任何人都有帮助。请确保您已在pom.xml或gradle文件中添加了依赖项
SpringBootStarter数据jpa我通过在SpringConfig.java文件中为我的服务创建一个bean解决了这个问题。 请检查下面的代码
@Configuration
public class SpringConfig {
@Bean
public TransactionService transactionService() {
return new TransactionServiceImpl();
}
}
此文件的路径如下图所示,
考虑定义类型为的bean
“moviecruser.repository.MovieRepository”在您的
配置
如果未添加,将生成此类问题
正确的依赖关系。这与我面临的问题相同,但在我找到我的JPA之后
依赖项无法正常工作,因此请确保
依赖关系是否正确
例如:-
我使用的依赖关系:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
org.springframework.data
spring数据jpa
描述(获取此异常):-
中构造函数的参数0
moviecruser.serviceImple.MovieServiceImpl需要一个
键入“moviecruser.repository.MovieRepository”,无法
找到了
行动:
更改依赖项后:-
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
@springboot应用程序
@ComponentScan(basePackages={“io.testapi”})
在SpringBootApplicationAnnotation下面的主类中,我编写了componentscan,它对我很有用 即使在完成了所有建议的方法之后,我也会遇到同样的错误。经过努力,我知道hibernate的maven依赖项被添加到我的pom.xml中,当我删除它时,应用程序成功启动
我删除了此依赖项:
<dependency> <groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
org.hibernate.javax.persistence
hibernate-jpa-2.0-api
1.0.1.最终版本
我修复了将这一行@ComponentScan(basePackages={“com.example.DemoApplication”})
添加到主类文件(仅从类名开始)的问题
package com.example.demo;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.annotation.ComponentScan;
@SpringBoot应用程序
@ComponentScan(basePackages={“com.example.DemoApplication”})
公共类演示应用程序{
公共静态void main(字符串[]args){
run(DemoApplication.class,args);
}
}
如果您想知道在哪里添加@Service
注释,那么
确保已将@Service
注释添加到实现接口的类中。这将解决此问题。删除注释配置,如服务、存储库、组件
@Component
@Service
我只是有这个问题,这就是解决办法。首先,创建如下界面:
public interface TransactionService {
}
然后使用类实现这些方法:
@Service
public class TransactionServiceImpl implements TransactionService {
}
当你说我正在注入一个bean时,我一点也不明白??我正在将TopicService
这是一个接口注入控制器类Topics
我没有编辑错误消息。我刚刚编辑了我发布的问题标题。问题仍然存在。我删除了@Autowired-private-TopicService-TopicService代码>来自实现类类类TopicServiceImplementation
是否具有注释@组件
?如果没有,您必须将其添加到类中,以便Spring可以将其识别为bean。@dunni在Spring boot中,@Component
注释不是必需的。它由springboot自动扫描,与springmvcIf不同,您没有在类中添加任何注释,它不会被扫描,因为您没有将其标记为bean。如果你不相信我,请阅读文档。@dunni你是对的。我会接受这个答案。谢谢,即使我已经用@Component为该类添加了注释,我也会遇到这个错误。这对我来说是一个完美的解决方案:(。请尝试提供更多信息,说明删除这些注释会做什么,以及为什么在使用这些注释时会出现问题。@我在接口上应用了服务注释,这就是为什么我在应用之后会遇到同样的问题
public interface TransactionService {
}
@Service
public class TransactionServiceImpl implements TransactionService {
}