![](/img/trans.png)
[英]Consider defining a bean of type 'package' in your configuration [Spring-Boot]
[英]Consider defining a bean of type 'service' in your configuration [Spring boot]
运行主类时出现错误。
错误:
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
主题服务接口:
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);
}
控制器:
@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");
}
}
实现类:
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.
}
}
其余的类也被定义。 尽管在主类中声明了componentScan
,但我不知道为什么它会抛出。
主类:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@ComponentScan(basePackages = {"seconds47"})
@EnableJpaRepositories("seconds47.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我有这样的包裹:
seconds47
seconds47.beans
seconds47.config
seconds47.repository
seconds47.restAPI
seconds47.service
一个类必须具有@Component
注释或其派生(如@Service
、 @Repository
等)才能被组件扫描识别为 Spring bean。 因此,如果您将@Component
添加到类中,它应该可以解决您的问题。
由于TopicService
是一个Service
类,你应该用@Service
注释它,以便 Spring 为你自动装配这个 bean。 像这样:
@Service
public class TopicServiceImplementation implements TopicService {
...
}
这将解决您的问题。
我修复了将这一行@ComponentScan(basePackages = {"com.example.DemoApplication"})
添加到主类文件的问题,就在类名中
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.DemoApplication"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
我通过在 SpringConfig.java 文件中为我的服务创建一个 bean 解决了这个问题。 请检查以下代码,
@Configuration
public class SpringConfig {
@Bean
public TransactionService transactionService() {
return new TransactionServiceImpl();
}
}
此文件的路径如下图所示, Spring Boot 应用程序文件夹结构
您正在尝试自己注入一个 bean。 这显然行不通。
TopicServiceImplementation
实现TopicService
。 该类尝试自动装配(按字段!)一个`TopicService。 所以你本质上是在要求上下文注入自己。
看起来您已经编辑了错误消息的内容: Field topicService in seconds47.restAPI.topics
is not a class。 如果您需要隐藏敏感信息,请小心,因为这会使其他人更难帮助您。
回到实际问题上,看起来注入TopicService
本身就是一个小故障。
考虑在您的配置中定义“moviecruser.repository.MovieRepository”类型的 bean。
如果您没有添加正确的依赖项,则会产生此类问题。 我遇到了同样的问题,但是在我发现我的 JPA 依赖项无法正常工作之后,因此请确保第一个依赖项是否正确。
例如:-
我使用的依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
描述(得到这个例外): -
moviecruser.serviceImple.MovieServiceImpl 中构造函数的参数 0 需要找不到类型为“moviecruser.repository.MovieRepository”的 bean。
行动:
更改依赖项后:-
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
回复:-
2019-09-06 23:08:23.202 信息 7780 -
[main]moviecruser.MovieCruserApplication]:在 10.585 秒内启动 MovieCruserApplication(JVM 运行时间为 11.357)
你必须更新你的
scanBasePackages = { "com.exm.java" }
将路径添加到您的服务(使用 @service 对其进行注释之后)
我通过替换损坏的 jar 文件来解决。
但是要找到那些损坏的 jar 文件,我必须在三个 IDE 中运行我的应用程序——1)Intellij Idea 2)NetBeans 3)Eclipse。
Netbeans 为我提供了有关损坏 jar 的最大数量的信息。 在 Netbeans 和运行中,我使用构建选项(右键单击项目后)来了解有关损坏 jar 的更多信息。
我花了 15 个多小时才找出这些错误的根本原因。 希望它可以帮助任何人。
如果您想知道在哪里添加@Service
注释,请确保您已将@Service
注释添加到实现接口的类中。 那将解决这个问题。
@SpringBootApplication @ComponentScan(basePackages = {"io.testapi"})
在 springbootapplication 注释下面的主类中,我编写了 componentscan,它对我有用。
即使在完成所有建议的方法之后,我也遇到了同样的错误。 经过努力,我知道在我的pom.xml中添加了hibernate的maven依赖,当我删除它时,应用程序启动成功。
我删除了这个依赖:
<dependency> <groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
请确保您在 pom.xml 或 gradle 文件中添加了依赖项
spring-boot-starter-数据-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 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);
}
控制器:
@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");
}
}
实现类:
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.
}
}
其余的类也已定义。 我不知道为什么在主类中声明了componentScan
却抛出了它。
主班:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@ComponentScan(basePackages = {"seconds47"})
@EnableJpaRepositories("seconds47.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我有这样的包裹:
seconds47
seconds47.beans
seconds47.config
seconds47.repository
seconds47.restAPI
seconds47.service
我只是有这个问题,这就是解决方案。 首先,创建如下界面:
public interface TransactionService {
}
然后使用类实现方法:
@Service
public class TransactionServiceImpl implements TransactionService {
}
我曾经遇到过类似的问题。 您可以使用@Component
注释服务类,并且 Main 处的@SpringBootApplication
应该带有@SpringBootApplication(ScanBasePackages="com.seconds47")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.