当前位置: 首页 > 知识库问答 >
问题:

错误:字段需要的bean类型找不到。考虑定义一个bean

凌炜
2023-03-14

在spring Boot1.5.9上创建spring boot应用程序时,我收到以下错误消息。有谁能帮忙吗?提前道谢。

  .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.9.RELEASE)

    2018-01-26 22:16:01.534  INFO 7176 --- [           main] com.myapp.MyAppApplication               : Starting MyAppApplication on DESKTOP-29H8ALN with PID 7176 (F:\My_SpringBoot_WorkSpace_20_01_2018\MyApp\MyApp\target\classes started by Tanmoy in F:\My_SpringBoot_WorkSpace_20_01_2018\MyApp\MyApp)
    2018-01-26 22:16:01.534  INFO 7176 --- [           main] com.myapp.MyAppApplication               : No active profile set, falling back to default profiles: default
    2018-01-26 22:16:01.590  INFO 7176 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@61009542: startup date [Fri Jan 26 22:16:01 IST 2018]; root of context hierarchy
    2018-01-26 22:16:02.889  INFO 7176 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9001 (http)
    2018-01-26 22:16:02.903  INFO 7176 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2018-01-26 22:16:02.904  INFO 7176 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
    2018-01-26 22:16:03.075  INFO 7176 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2018-01-26 22:16:03.075  INFO 7176 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1490 ms
    2018-01-26 22:16:03.238  INFO 7176 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2018-01-26 22:16:03.242  INFO 7176 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2018-01-26 22:16:03.243  INFO 7176 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2018-01-26 22:16:03.243  INFO 7176 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2018-01-26 22:16:03.243  INFO 7176 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2018-01-26 22:16:03.277  WARN 7176 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController': Unsatisfied dependency expressed through field 'customerService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerService': Unsatisfied dependency expressed through field 'customersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myapp.repository.CustomersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    2018-01-26 22:16:03.278  INFO 7176 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    2018-01-26 22:16:03.311  INFO 7176 --- [           main] utoConfigurationReportLoggingInitializer : 

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    2018-01-26 22:16:03.396 ERROR 7176 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

    ***************************
    APPLICATION FAILED TO START
    ***************************

    Description:

    Field customersRepository in com.myapp.service.CustomerService required a bean of type 'com.myapp.repository.CustomersRepository' that could not be found.


    Action:

    Consider defining a bean of type 'com.myapp.repository.CustomersRepository' in your configuration.

Oracle 11g数据库。

请找到我在pom.xml中使用的以下类:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tanmoy</groupId>
    <artifactId>MyApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MyApp</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>12.1.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Java代码-应用程序:

package com.myapp;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.myapp.entity.Customers;
import com.myapp.repository.CustomersRepository;
import com.myapp.service.CustomerService;

@SpringBootApplication
public class MyAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyAppApplication.class, args);

    }

    @Bean
    public CommandLineRunner demo(CustomersRepository repository) {

        return (args) -> {

            // fetch all customers

            for (Customers customer : repository.findAll()) {

            }

        };

    }

}

Java代码存储库:

package com.myapp.repository;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.myapp.entity.Customers;

@Repository
public interface CustomersRepository extends CrudRepository<Customers, Integer> {

    @Query("select * from customers")
    public List<Customers> getAllCustomerts();

}

Java代码服务:

package com.myapp.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myapp.repository.CustomersRepository;

@Service
public class CustomerService {

    @Autowired
    CustomersRepository customersRepository;

    public void getAllCustomers() {
        System.out.println(customersRepository.findAll());
    }

}

共有1个答案

蒋茂材
2023-03-14

customersrepository中删除@repository批注。

接口crudrepository已经注册在Spring的容器中,并且已经可以注入它(也可以注入它的子接口)。

 类似资料:
  • 我在尝试运行我的应用程序时遇到以下错误: com.alon.service.EmployeeServiceImpl中得字段edao需要类型为“com.alon.Repository.EmployeeRepository”得bean,但找不到该bean. 注入点有以下注释: null EmployeeServiceImpl: 我的应用程序:

  • 我是一名spring boot学习者,所以我一直在尝试创建一些基本的spring boot应用程序。我试图运行开发的应用程序时出错。 我的错误是[[https://i.stack.imgur.com/oyQDi.png][1]][1] java: ItemDetails.java:[软件包名称:io.ajithan.springbootstarter.model] ItemResponse.jav

  • 结构没有问题。spring boot可以扫描UserMapper,但不能扫描UserService。我试着给我的UserService@Mapper组件,然后它就可以被扫描了。但我不知道如何使用其他方法进行扫描。我尝试了@服务,但不起作用。

  • 客户服务实施 客户服务界面 客户映射器 主类 错误- 应用程序启动失败 说明: com中的字段映射器。应用服务实施。CustomerServiceImpl需要“com”类型的bean。应用映射器。找不到CustomerMapper“”。 措施: 考虑定义“com”类型的bean。应用映射器。配置中的CustomerMapper。

  • 我有一个Spring Boot项目,其中Flyway与JPA一起使用。 该项目运行完全正常。由于一些要求,我不得不改变Flyway加载的顺序,以便在Hibernate后加载。我搜索并找到了以下解决方案 。我按照上述链接中的答案创建了。现在,当我运行该应用程序时,它会给我以下错误, 现在我对Spring Boot相当陌生,因此无法理解此错误,并且在网上找不到与此错误相关的任何内容。

  • 我使用KOTLIN/SPRING BOOT(2.5.4)/MAVEN/MARIA DB我遇到了这样的错误,所以我想我必须找到make bean。但是我找不到我错过的东西。你能帮我吗...? 我的项目是Kotlin com。实例测试0831└ 控制器└ 用户控制器└ 实体└ 使用者└ 存储库└ 用户存储库└ 服务└ 用户服务└ UserServiceImpl 程序包com。实例test0831.co