【问题标题】:Consider defining a bean of type 'service' in your configuration [Spring boot]考虑在您的配置中定义一个“服务”类型的 bean [Spring boot]
【发布时间】:2017-05-30 12:57:13
【问题描述】:

运行主类时出现错误。

错误:

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

【问题讨论】:

  • TopicServiceImplementation有注解@Component吗?如果没有,则必须将其添加到类中,以便 Spring 可以将其识别为 bean。
  • @dunni 在spring boot中,@Component注解不是必需的,它是springboot自动扫描的,不像springmvc
  • 如果类上不加注解,是不会被扫描的,因为你还没有把它标记为bean。如果您不相信我,请阅读文档。
  • @dunni 你是对的。我会接受它作为答案。谢谢

标签: java spring spring-boot spring-data spring-data-jpa


【解决方案1】:

我只是有这个问题,这就是解决方案。首先,创建如下界面:

public interface TransactionService {

}

然后使用类实现方法:

@Service
public class TransactionServiceImpl implements TransactionService {

}

【讨论】:

    【解决方案2】:

    如果您想知道在哪里添加 @Service 注释,那么 确保您已将@Service 注释添加到实现接口的 。这样就解决了这个问题。

    【讨论】:

    • @Service 注释我应用在接口上,这就是为什么我面临同样的问题,在将它应用到实现类之后它对我有用
    【解决方案3】:

    我解决了将这一行 @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);
    	}
    
    }

    【讨论】:

      【解决方案4】:

      即使在执行了所有建议的方法之后,我还是遇到了同样的错误。经过努力,我知道在我的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>
      

      【讨论】:

        【解决方案5】:

        @SpringBootApplication @ComponentScan(basePackages = {"io.testapi"})

        在 springbootapplication 注释下面的主类中,我编写了 componentscan,它对我有用。

        【讨论】:

          【解决方案6】:

          考虑定义一个 bean 类型 'moviecruser.repository.MovieRepository' 在你的 配置。

          如果您没有添加,则会产生此类问题 正确的依赖。我遇到了同样的问题,但是在我找到我的 JPA 之后 依赖项无法正常工作,因此请确保首先 依赖是否正确。

          例如:-

          我使用的依赖:

              <dependency>
                 <groupId>org.springframework.data</groupId>      
                 <artifactId>spring-data-jpa</artifactId>
              </dependency>
          

          描述(得到这个例外):-

          构造函数的参数0 moviecruser.serviceImple.MovieServiceImpl 需要一个 bean 键入“moviecruser.repository.MovieRepository” 找到了。

          行动:

          改变依赖后:-

              <!-- 
              <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]:已启动 MovieCruserApplication 在 10.585 秒内(JVM 运行时间为 11.357)

          【讨论】:

            【解决方案7】:

            我通过在 SpringConfig.java 文件中为我的服务创建一个 bean 解决了这个问题。 请检查以下代码,

            @Configuration 
            public class SpringConfig { 
            
            @Bean
            public TransactionService transactionService() {
                return new TransactionServiceImpl();
            }
            
            }
            

            这个文件的路径如下图所示, Spring boot application folder structure

            【讨论】:

              【解决方案8】:

              请确保您已在 pom.xml 或 gradle 文件中添加了依赖项

              spring-boot-starter-data-jpa

              【讨论】:

                【解决方案9】:

                我通过替换损坏的 jar 文件来解决。

                但是要找到那些损坏的 jar 文件,我必须在三个 IDE 中运行我的应用程序 - 1) Intellij Idea 2)NetBeans 3) Eclipse。

                Netbeans 向我提供了有关损坏 jar 的最大数量的信息。在 Netbeans 和运行中,我使用构建选项(右键单击项目后)来了解有关损坏 jar 的更多信息。

                我花了 15 个多小时才找出这些错误的根本原因。希望对大家有所帮助。

                【讨论】:

                  【解决方案10】:

                  你必须更新你的

                  scanBasePackages = { "com.exm.java" }
                  

                  将路径添加到您的服务(使用 @service 注释后)

                  【讨论】:

                    【解决方案11】:

                    因为TopicService 是一个Service 类,你应该用@Service 注释它,以便Spring 自动为你装配这个bean。像这样:

                    @Service
                    public class TopicServiceImplementation implements TopicService {
                        ...
                    }
                    

                    这会解决你的问题。

                    【讨论】:

                      【解决方案12】:

                      一个类必须具有@Component 注释或其派生(如@Service@Repository 等)才能被组件扫描识别为Spring bean。因此,如果您将@Component 添加到课程中,它应该可以解决您的问题。

                      【讨论】:

                      • 即使我用 @Component 注释了那个类,我也会收到这个错误
                      • 对我来说是一个完美的解决方案 :(.
                      • @TusharBanne 如果您对 bean 的接口使用注解,而不是 bean,它将不起作用
                      【解决方案13】:

                      您正在尝试在其自身中注入一个 bean。这显然行不通。

                      TopicServiceImplementation 实现TopicService。该类尝试自动装配(按字段!)一个`TopicService。所以你本质上是在要求上下文注入自己。

                      您似乎编辑了错误消息的内容:Field topicService in seconds47.restAPI.topics is not a class。如果您需要隐藏敏感信息,请小心,因为这会使其他人更难帮助您。

                      回到实际问题,看起来注入 TopicService 本身就是你的一个小故障。

                      【讨论】:

                      • 你说I am injecting a bean in itself的时候我什么都不懂??我将TopicService 注入控制器类Topics
                      • 我没有编辑错误信息。我刚刚编辑了我发布的问题标题
                      • 问题依然存在。我从实现类中删除了@Autowired private TopicService topicService;
                      猜你喜欢
                      • 2017-03-16
                      • 2017-11-14
                      • 2019-12-05
                      • 2021-04-04
                      • 2022-09-29
                      • 2022-01-05
                      • 2018-08-27
                      • 2019-03-16
                      • 2018-06-22
                      相关资源
                      最近更新 更多