考虑在配置中定义'service'类型的bean [Spring boot]

kit*_*ttu 22 java spring spring-data spring-data-jpa 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
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

控制器:

@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");
    }
}
Run Code Online (Sandbox Code Playgroud)

实施班:

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

其余的类也定义了.尽管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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的包裹是这样的:

seconds47
seconds47.beans
seconds47.config
seconds47.repository
seconds47.restAPI
seconds47.service
Run Code Online (Sandbox Code Playgroud)

dun*_*nni 38

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

  • 即使我使用@Component注释了该类,我也会收到该错误 (15认同)

Lui*_*íaz 13

我解决了将这一行添加到主类文件中的问题@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);
    }

}
Run Code Online (Sandbox Code Playgroud)


Ahm*_*ila 6

既然TopicService是一个Service类,那么您应该用对其进行注释@Service,以便Spring为您自动装配该bean。像这样:

@Service
public class TopicServiceImplementation implements TopicService {
    ...
}
Run Code Online (Sandbox Code Playgroud)

这样可以解决您的问题。


小智 6

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

@Configuration 
public class SpringConfig { 

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

}
Run Code Online (Sandbox Code Playgroud)

这个文件的路径如下图所示, Spring boot应用程序文件夹结构


小智 5

考虑在配置中定义“moviecruser.repository.MovieRepository”类型的 bean。

如果您没有添加正确的依赖项,就会产生此类问题。这与我遇到的问题相同,但在我发现我的 JPA 依赖项无法正常工作之后,因此请确保第一个依赖项是否正确。

例如:-

我使用的依赖项:

    <dependency>
       <groupId>org.springframework.data</groupId>      
       <artifactId>spring-data-jpa</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

描述(出现此异常):-

moviecruser.serviceImple.MovieServiceImpl 中构造函数的参数 0 需要类型为“moviecruser.repository.MovieRepository”的 bean,但无法找到。

行动:

更改依赖关系后:-

    <!-- 
    <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

回复:-

2019-09-06 23:08:23.202 INFO 7780 -
[main]moviecruser.MovieCruserApplication]:在 10.585 秒内启动 MovieCruserApplication (JVM 运行了 11.357)