Java 考虑在您的配置中定义一个“服务”类型的 bean [Spring boot]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41663652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consider defining a bean of type 'service' in your configuration [Spring boot]
提问by kittu
I get error when I run the main class.
运行主类时出现错误。
Error:
错误:
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 interface:
主题服务接口:
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);
}
controller:
控制器:
@RestController
public class topics {
@Autowired
private TopicService topicService;
@RequestMapping(path = "/new_topic2", method = RequestMethod.GET)
public void new_topic() throws Exception {
System.out.println("new topic JAVA2");
}
}
Implementation class:
实现类:
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.
}
}
Rest of the classes are defined too. I don't know why its throwing despite declaring componentScan
in main class.
其余的类也被定义。尽管componentScan
在主类中声明,我不知道为什么它会抛出。
Main class:
主要类:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@ComponentScan(basePackages = {"seconds47"})
@EnableJpaRepositories("seconds47.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I have my packages like this:
我有这样的包裹:
seconds47
seconds47.beans
seconds47.config
seconds47.repository
seconds47.restAPI
seconds47.service
采纳答案by dunni
A class must have the @Component
annotation or a derivation of that (like @Service
, @Repository
etc.) to be recognized as a Spring bean by the component scanning. So if you add @Component
to the class, it should solve your problem.
一个类必须具有@Component
注解或该推导(如@Service
,@Repository
等)被识别为一个Spring bean由组件扫描。因此,如果您添加@Component
到课程中,它应该可以解决您的问题。
回答by Stephane Nicoll
You are trying to inject a bean in itself. That's obviously not going to work.
您正试图在自身中注入一个 bean。这显然行不通。
TopicServiceImplementation
implements TopicService
. That class attempts to autowire (by field!) a `TopicService. So you're essentially asking the context to inject itself.
TopicServiceImplementation
实施TopicService
. 该类尝试自动装配(按字段!)一个`TopicService。因此,您实际上是在要求上下文注入自身。
It looks like you've edited the content of the error message: Field topicService in seconds47.restAPI.topics
is not a class. Please be careful if you need to hide sensitive information as it makes it much harder for others to help you.
看起来您已经编辑了错误消息的内容:Field topicService in seconds47.restAPI.topics
不是类。如果您需要隐藏敏感信息,请小心,因为这会使其他人更难帮助您。
Back on the actual issue, it looks like injecting TopicService
in itself is a glitch on your side.
回到实际问题,看起来注入TopicService
本身是你这边的一个小故障。
回答by Ahmed Tawila
Since TopicService
is a Service
class, you should annotate it with @Service
, so that Spring autowires this bean for you. Like so:
既然TopicService
是一个Service
类,你应该用 注释它@Service
,以便 Spring 为你自动装配这个 bean。像这样:
@Service
public class TopicServiceImplementation implements TopicService {
...
}
This will solve your problem.
这将解决您的问题。
回答by yahya Harrathi
You have to update your
你必须更新你的
scanBasePackages = { "com.exm.java" }
to add the path to your service (after annotating it with @service )
将路径添加到您的服务(使用 @service 对其进行注释后)
回答by Tarun
I resolved by replacing the corrupted jar files.
我通过替换损坏的 jar 文件来解决。
But to find those corrupted jar files, I have to run my application in three IDE- 1) Intellij Idea 2)NetBeans 3) Eclipse.
但是要找到那些损坏的 jar 文件,我必须在三个 IDE 中运行我的应用程序 - 1) Intellij Idea 2)NetBeans 3) Eclipse。
Netbeans given me information for maximum number of corrupted jar. In Netbeans along with the run, I use the build option(after right clicking on project) to know more about corrupted jars.
Netbeans 为我提供了有关损坏 jar 的最大数量的信息。在 Netbeans 和运行中,我使用构建选项(右键单击项目后)来了解有关损坏的 jar 的更多信息。
It took me more than 15 hours to find out the root cause for these errors. Hope it help anyone.
我花了超过 15 个小时才找出这些错误的根本原因。希望它可以帮助任何人。
回答by Kumar Kathir
Please make sure that you have added the dependency in pom.xml or gradle file
请确保您已在 pom.xml 或 gradle 文件中添加了依赖项
spring-boot-starter-data-jpa
spring-boot-starter-data-jpa
回答by Jeberdson Abraham
I solved this issue by creating a bean for my service in SpringConfig.java file. Please check the below code,
我通过在 SpringConfig.java 文件中为我的服务创建一个 bean 解决了这个问题。请检查以下代码,
@Configuration
public class SpringConfig {
@Bean
public TransactionService transactionService() {
return new TransactionServiceImpl();
}
}
The path of this file is shown in the below image, Spring boot application folder structure
这个文件的路径如下图所示, Spring boot应用程序文件夹结构
回答by Arun Tiwari
Consider defining a bean of type 'moviecruser.repository.MovieRepository' in your configuration.
考虑在您的配置中定义一个类型为“moviecruser.repository.MovieRepository”的 bean。
This type of issue will generate if you did not add correct dependency. Its the same issue I faced but after I found my JPA dependency is not working correctly, so make sure that first dependency is correct or not.
如果您没有添加正确的依赖项,就会产生这种类型的问题。它与我面临的问题相同,但在我发现我的 JPA 依赖项无法正常工作后,因此请确保第一个依赖项是否正确。
For example:-
例如:-
The dependency I used:
我使用的依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
Description (got this exception):-
描述(得到这个例外):-
Parameter 0 of constructor in moviecruser.serviceImple.MovieServiceImpl required a bean of type 'moviecruser.repository.MovieRepository' that could not be found.
moviecruser.serviceImple.MovieServiceImpl 中构造函数的参数 0 需要一个无法找到的类型为“moviecruser.repository.MovieRepository”的 bean。
Action:
行动:
After change dependency:-
更改依赖项后:-
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Response:-
回复:-
2019-09-06 23:08:23.202 INFO 7780 -
[main]moviecruser.MovieCruserApplication]:Started MovieCruserApplication in 10.585 seconds (JVM running for 11.357)
2019-09-06 23:08:23.202 INFO 7780 -
[main]moviecruser.MovieCruserApplication]:在 10.585 秒内启动 MovieCruserApplication(JVM 运行 11.357)
回答by Ramu Smartguy
@SpringBootApplication @ComponentScan(basePackages = {"io.testapi"})
@SpringBootApplication @ComponentScan(basePackages = {"io.testapi"})
In the main class below springbootapplication annotation i have written componentscan and it worked for me.
在 springbootapplication 注释下面的主类中,我编写了 componentsscan 并且它对我有用。
回答by Vatsal Mehta
Even after doing all the method suggested, i was getting the same error. After trying hard, i got to know that hibernate's maven dependency was added in my pom.xml, as i removed it, application started successfully.
即使在完成了所有建议的方法之后,我还是遇到了同样的错误。经过努力,我知道在我的 pom.xml 中添加了 hibernate 的 maven 依赖项,当我删除它时,应用程序成功启动。
I removed this dependency:
我删除了这个依赖:
<dependency> <groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>