我得到以下错误:

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

Description:

Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.


Action:

Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.

我以前从未见过这个错误,但奇怪的是@Autowire不工作。项目结构如下:

申请人接口

public interface Applicant {

    TApplicant findBySSN(String ssn) throws ServletException;

    void deleteByssn(String ssn) throws ServletException;

    void createApplicant(TApplicant tApplicant) throws ServletException;

    void updateApplicant(TApplicant tApplicant) throws ServletException;

    List<TApplicant> getAllApplicants() throws ServletException;
}

ApplicantImpl

@Service
@Transactional
public class ApplicantImpl implements Applicant {

private static Log log = LogFactory.getLog(ApplicantImpl.class);

    private TApplicantRepository applicantRepo;

@Override
    public List<TApplicant> getAllApplicants() throws ServletException {

        List<TApplicant> applicantList = applicantRepo.findAll();

        return applicantList;
    }
}

现在我应该能够自动连线申请人,并能够访问,但在这种情况下,当我在@RestController中调用它时,它是不工作的:

@RestController
public class RequestController extends LoggingAware {

    private Applicant applicant;

    @Autowired
    public void setApplicant(Applicant applicant){
        this.applicant = applicant;
    }

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String helloWorld() {

        try {
            List<TApplicant> applicantList = applicant.getAllApplicants();

            for (TApplicant tApplicant : applicantList){
                System.out.println("Name: "+tApplicant.getIndivName()+" SSN "+tApplicant.getIndSsn());
            }

            return "home";
        }
        catch (ServletException e) {
            e.printStackTrace();
        }

        return "error";
    }

}

------------------------ 更新1 -----------------------

我添加了

@SpringBootApplication
@ComponentScan("module-service")
public class WebServiceApplication extends SpringBootServletInitializer {

    @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebServiceApplication.class);
    }

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

}

错误消失了,但什么也没发生。然而,当我在添加@ComponentScan()之前注释掉所有与RestController中的申请人相关的内容时,我能够返回一个字符串的UI,从而意味着我的RestController正在工作,现在它被跳过了。我丑陋的白色标签错误页现在。

--------------------- 更新2 ------------------------------

我添加了它所抱怨的bean的基本包。错误读取:

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

Description:

Parameter 0 of method setApplicantRepo in com.service.applicant.ApplicantImpl required a bean of type 'com.delivery.service.request.repository.TApplicantRepository' that could not be found.


Action:

Consider defining a bean of type 'com.delivery.request.request.repository.TApplicantRepository' in your configuration.

我添加了@ComponentScan

@SpringBootApplication
@ComponentScan({"com.delivery.service","com.delivery.request"})
public class WebServiceApplication extends SpringBootServletInitializer {

    @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebServiceApplication.class);
    }

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

}

---------------------------- 更新3 ----------------------

添加:

@SpringBootApplication
@ComponentScan("com")
public class WebServiceApplication extends SpringBootServletInitializer {

仍然在抱怨我的ApplicantImpl类@自动连接我的repo TApplicantRepository到它。


你的申请人类似乎没有被扫描。默认情况下,所有以根目录开头的包都将被扫描,你在其中放置了@SpringBootApplication类。

假设你的主类“WebServiceApplication”在“com.service”中。Something”,然后是属于“com.service”的所有组件。“某些东西”被扫描,而“com.service.”“申请人”将不会被扫描。

你可以重新构造你的包,使“WebServiceApplication”置于根包之下,而所有其他组件都成为根包的一部分。或者你可以包含@SpringBootApplication(scanBasePackages={"com.service.something","com.service.application"})等,这样"ALL"组件就会在spring容器中被扫描和初始化。

根据评论进行更新

如果你有多个由maven/gradle管理的模块,spring所需要的就是要扫描的包。你告诉春天扫描“com”。Module1”,还有另一个模块,其根包名为“com”。Module2”,这些组件不会被扫描。您甚至可以告诉spring扫描“com”,然后它将扫描“com.module1.”和“com.module2.”中的所有组件。


这可能是因为项目被分解成不同的模块。

@SpringBootApplication
@ComponentScan({"com.delivery.request"})
@EntityScan("com.delivery.domain")
@EnableJpaRepositories("com.delivery.repository")
public class WebServiceApplication extends SpringBootServletInitializer {

如果您正在使用Lombok,并且为字段添加了@RequiredArgsConstructor和@NonNull,但有些字段不会被注入到构造函数中,也会发生这种情况。这只是得到相同错误的一种可能。

参数0需要一个MissingBeanName类型的bean,但是找不到

在我的情况下,错误告诉我什么控制器的问题,删除@NonNull后,应用程序启动良好


基本上,当你在“另一个包”中有你的类应用程序时,就会发生这种情况。例如:

com.server
 - Applicacion.class (<--this class have @ComponentScan)
com.server.config
 - MongoConfig.class 
com.server.repository
 - UserRepository

我在Application.class中解决了这个问题

@SpringBootApplication
@ComponentScan ({"com.server", "com.server.config"})
@EnableMongoRepositories ("com.server.repository") // this fix the problem

另一种不那么优雅的方法是:将所有配置类放在同一个包中。


@SpringBootApplication
@MapperScan("com.developer.project.mapper")

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

我认为你可以通过使用@Repository注释你的存储库来简化它,然后Spring框架就会自动启用它。


有一个机会…… 您可能会在各自的实现类中遗漏@Service、@Repository或@Component注释。


我在网上寻找答案,但似乎没有一个合适的解决方案: 在一开始,一切都很顺利:

@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GroupService {
    private Repository repository;
    private Service service;
}

然后我试图添加一个映射来缓存一些东西,它变成了这样:

@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GroupService {
    private Repository repository;
    private Service service;
    Map<String, String> testMap;
}

繁荣!

Description:

Parameter 4 of constructor in *.GroupService required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

我删除了@AllArgsConstructor(onConstructor = @__(@Autowired)),并为每个存储库和服务添加@Autowired,除了Map<String, String>。就像以前一样。

@Slf4j
@Service
public class SecurityGroupService {
    @Autowired
    private Repository repository;
    @Autowired
    private Service service;
    Map<String, String> testMap;
}

希望这对你有所帮助。


如果@Service类被标记为抽象,就会发生这种情况。


在我的案例中,这两种选择都有效。

in //@ComponentScan ({"myapp", "myapp.resources","myapp.services"}) 还包括在列表中包含Application.class的包,或者 简单地添加@EnableAutoConfiguration;它会自动识别所有的春豆。


就我而言,我犯了一个严重的错误。我把@Service放到了服务接口上。

为了解决这个问题,我把@Service放在了service文件的实现中,它为我工作了。


如果bean位于@Autowired所在的同一个包中,那么它将永远不会引起这样的问题。但是,默认情况下,bean不能从不同的包中访问。 要修复此问题,请执行以下步骤:

在你的主类中导入以下内容: 进口org.springframework.context.annotation.ComponentScan; 在主类上添加注释:

@ComponentScan(basePackages = {"your.company.domain.package"})
public class SpringExampleApplication {

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

在应用程序中添加以下注释后,它为我工作:

@ComponentScan ({com.seic.deliveryautomation.mapper "})

我得到以下错误:

"构造函数中的参数1需要一个类型映射器的bean,但找不到:


在我的情况下,这个错误出现是因为我的导入是错误的,例如,使用spring,导入自动出现:

import org.jvnet.hk2.annotations.Service;

但我需要:

import org.springframework.stereotype.Service;

我有一个案例,我需要将RestTemplate注入到服务类中。但是,服务类不能拾取RestTemplate。我所做的是在与主应用程序相同的包下创建一个包装器类,并将包装器标记为Component,并在服务类中自动装配此组件。问题解决了。希望它也适用于你


如果您的类依赖是由Spring管理的,那么如果我们忘记在POJO类中添加默认/空参数构造函数,则可能会发生此问题。


@Configuration注释将解决这个错误


如果您意外地在两个不同的类中定义了相同的bean,也会得到这个错误。我就是这样。错误信息具有误导性。当我去掉多余的豆子时,问题就解决了。


我在Spring Boot 2的Maven多模块项目中遇到了熟悉的问题。这个问题与子Maven模块中包的命名有关。

@SpringBootApplication封装了很多组件,比如@ComponentScan, @EnableAutoConfiguration, jpa-repositories, json-serialization等等。他把@ComponentScan放在了com.*******中。空间方案。这部分的包com.*******。所有模块的空间必须是公用的。

解决方法:

您应该重命名所有模块包。换句话说,您必须在所有Maven模块的所有包中都有相同的父部分。例如- com.*******.space 此外,您必须将您的入口点移动到这个包- com.*******.space


我的错误在于,我把以下内容包括进去了:

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

而不是:

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

我认为,您在RequestController中遗漏了@Bean注释

在文件中添加Bean,这解决了我的问题 当我从tutorialspoint学习Spring Boot时,我得到了这个解决方案

private Applicant applicant;

@Bean 
public Applicant applicant() { 
    return new Applicant(); 
}

添加Spring Boot Data JPA Starter依赖为我解决了这个问题。

Maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

Gradle

compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.6.RELEASE'

或者你可以直接到这里


它可能会帮助某些人。我遇到了同样的问题,同样的错误信息,一切都一样。我尝试了其他答案的解决方案,没有帮助,直到我意识到我使用的bean与实际自动连接的bean具有相同的名称。它发生在重构过程中,因此我不得不重命名类,结果是积极的。干杯


我也遇到过同样的问题。Mongo DB存储库是由Spring引导标识的,但它并没有为扩展Mongo存储库的存储库接口创建Bean。

在我的案例中,问题是maven pom中“spring + mango”的版本规范不正确。我已经改变了神器的组id,这一切都像魔法一样有效。不需要注释,因为spring boot会处理所有事情。

在我解决问题的过程中,我在网上到处搜索解决方案,并意识到这个问题实际上是与项目配置相关的,任何面临这个问题的人都应该首先检查他们的项目设置,并从spring中启用调试,以获得更多关于失败的详细信息,并密切关注在这个过程中的确切位置,创建失败了。


如果你使用接口,你可以扩展CrudRepository<申请人,长> @Repository注释。


当你使用@EnableMongoRepositories(YOUR_MONGO_REPOSITORIES_PACKAGE),然后重命名包名或将其移动到另一个地方时,问题也会出现。

在多模块maven项目和spring引导中经常遇到这种情况


有可能在实现接口之前尝试@autowired接口。

示例解决方案:

    **HomeController.java**
    class HomeController{

      @Autowired
      UserService userService;
    .....
    }
----------------------------------------------------------------------
    **UserService.java** 
    public interface UserService {
        User findByUsername(String username);
    .....
    }
-----------------------------------------------------------------------
     **UserServiceImpl.java**
     @Service
     public class UserServiceImpl implements UserService{

         public User findByUsername(String username) {
           return userDao.findByUsername(username);
         }
        ....
      }

<i>This is not italic</i>, and [this is not a link](https://example.com)

从线程运行方法中删除@Service这样的注释类型配置。

@Service, @Component

尝试如下所示配置项目结构:

把所有的repo, service,包放在主包的子包中:

package com.leisure.moviemax;  //Parent package
        
@SpringBootApplication
@PropertySource(value={"classpath:conf.properties"})
    
public class MoviemaxApplication implements CommandLineRunner {
        
package com.leisure.moviemax.repo; //child package

@Repository
public interface UsrRepository extends JpaRepository<UserEntity,String> {

提醒一下,spring不会扫描整个世界,它使用的是定向扫描,即springbootapplication存储在包下的所有东西。 因此,这个错误“考虑在你的配置[Spring-Boot]中定义一个'package'类型的bean”可能会出现,因为你在不同的springbootapplication包中有服务接口。


检查基本包名称。如果包具有不同的模块,这些模块没有以基本包名作为前缀。


将Springbootapplication(application.java)文件移动到另一个包中解决了我的问题。保持它与控制器和存储库分离。


重要的是:

对于那些通过谷歌通用bean错误消息而来到这里,但实际上试图通过客户端接口上的@FeignClient注释将一个虚拟客户端添加到Spring Boot应用程序的人来说,上述解决方案都不适合您。

为了解决这个问题,你需要在你的Application类中添加@EnableFeignClients注释,如下所示:

@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {

旁注:在@SpringBootApplication下面添加@ComponentScan(…)是多余的,你的IDE应该这样标记它(至少IntelliJ IDEA是这样做的)。


修复如下错误:

考虑在您的配置中定义一个'package'类型的bean 不是托管类型

我碰巧发现LocalContainerEntityManagerFactoryBean类没有setpackagestscan方法。然后我继续使用@ entitscan注释,它不能正常工作。

后来,我可以找到方法setPackagesToScan(),但在另一个模块,所以问题来自于依赖没有这个方法,因为它是一个旧版本。

这个方法可以在更新版本的spring-data-jpa或spring-orm依赖项中找到:

来自:

implementation("org.springframework", "spring-orm", "2.5.1") 

To:

implementation("org.springframework", "spring-orm", "5.2.9.RELEASE")

或:

implementation("org.springframework.data", "spring-data-jpa", "2.3.4.RELEASE")

此外,没有必要添加除的注释之外的其他注释 @SprintBootApplication。

@SpringBootApplication
open class MoebiusApplication : SpringBootServletInitializer()

@Bean
open fun entityManagerFactory() : LocalContainerEntityManagerFactoryBean {
    val em = LocalContainerEntityManagerFactoryBean()
    em.dataSource = dataSource()
    em.setPackagesToScan("app.mobius.domain.entity")
    ... 
}

GL


对我来说,在pom.xml上进行了干净的安装

右键单击pom.xml 扩展运行为 选择Maven构建 将目标设置为clean install命令 应用>运行>关闭


对我来说-使用Spring Boot与MongoDB,以下是问题:

在我的POM.xml我有:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-starter-data-mongodb</artifactId>
</dependency>

但我需要以下信息:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

(短:增加“spring-boot-…”而不是“spring-…”)


@Service应该从org.springframework.stereotype.Service导入

您可能是从org.jvnet.hk2.annotations.Service导入它

在CrudRepository中,检查实体类@Id是否使用import javax.persistence.Id;,而不是import org.springframework.data.annotation.Id;


在我的例子中,我们的项目有一个Configuration类,所以我像这样添加了我的类

@Configuration
public class DependencyConfiguration {

    @Bean
    public ActivityService activityService(
            @Value("${send.money.ms.activity.url}") final String activityHistoryUrl,
            final HttpRestService httpRestService
    ) {
        return new ActivityServiceImpl(activityHistoryUrl, httpRestService);
    }

.......................

然后微服务就正常启动了。

PS:我遇到了这个问题,即使我需要的库已经正确导入,并且可以在导入的外部库中看到。


当您未能使用@Entity Annotation注释与bean关联的Entity类时,也会弹出此错误消息。

我的ComponentScan工作得很好,但是@repository接口弹出了这个:

@Repository
public interface ExpenseReportAuditRepository extends 
     PagingAndSortingRepository<ExpenseReportAudit, Integer> {

因为我没有将@Entity注释添加到ExpenseReportAudit

@Entity // <--- Adding this fixed the issue.
public class ExpenseReportAudit {
  .....

如果您正在使用Eclipse并尝试了所有可能的方法,但仍然存在错误,那么请尝试使用Maven更新项目。您可以这样做:右键单击您的项目-> Maven ->更新项目。

它帮助我解决了我的错误。如果项目没有更新pom文件中声明的所有repo, Eclipse有时会显示此错误。


我在使用MongoDB时遇到了同样的问题,因为@Repository类的basePackages名称不正确

@Configuration
@EnableMongoRepositories(basePackages = {"org.mycompany.repository.mongo.primary"}, mongoTemplateRef = "getMongoTemplatePrimary")

我遇到了类似的问题,完全相同的错误信息。 但我的错误只发生在我试图安装和运行在我的本地docker。

事实证明,这个问题取决于个人特征。

我有两个实现接口的类,其中一个具有生产Profile @Profile(“prod”) 第二个是“测试环境”概要文件。 @Profile(”!本地& !prod”)

然而,当我试图在本地(在Docker上)运行时,没有活动配置文件。

我创建了一个第三类,实现了一个本地配置文件@Profile(“本地”)的接口,这解决了我的问题


在我的例子中,我刚刚向一些类添加了@Component注释(以确保每个类都有配置)。

@组件,@服务或@存储库几乎是熟悉的。Baeldung链接参考


在我的例子中,这个问题是由错误的spring.autoconfigure.exclude属性值引起的。我有这个属性是因为我的Spring Boot项目最初没有连接到数据库,但后来我添加了我的第一个JpaRepository类,并试图在@RestController类中使用@Autowired注释声明它的一个实例。

线条在应用中。导致问题的属性:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

一旦我删除或注释掉这一行,我的Spring Boot服务就可以正常运行了。


对我来说,这是因为在使用lombok @RequiredArgsConstructor注释的类中使用了@Value。 只是把它改成了构造函数注入。


有同样的错误,这是一个错误的用户名,密码和驱动程序与Bean完全无关的应用程序属性的问题。


我有同样的问题,我得到这个错误的redis库像这样:

@Repository
public interface AppTokenRepository extends CrudRepository<AppToken, String> {
}


@RedisHash("APP_TOKEN")
public class AppToken implements Serializable {
    private String id;
    private String accessToken;
}

我通过在spring引导应用程序类上添加@EnableRedisRepositories解决了我的问题。


@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


我也收到了类似的错误:

Consider defining a bean of type 'A_REPOSITORY_INTERFACE' in your configuration.

然后,根据Akashe的解决方案,我将@ enablejparepository添加到我的主类中。在那之后,我收到了以下错误:

Consider defining a bean of type 'entityManagerFactory' in your configuration.

接下来,我浏览了这里所有的回复,谷歌了很多,阅读了很多其他资源,但没有奏效。

最后,我很幸运地在博客/网站(javatute.com)上找到了解决方案。我只是效仿它。

就像许多人在这里建议的那样,我添加了@ComponentScan(“YOUR_BASE_PACKAGE.*”)和@ entitscan(“YOUR_BASE_PACKAGE.*”)到我的主应用程序类中,然后添加了一个配置包并创建了一个JpaConfig类,如:

package YOUR_BASE_PACKAGE.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories(basePackages = "YOUR_BASE_PACKAGE")
public class JpaConfig {

}

我关注的博客:

考虑在您的配置中定义一个bean类型

这让我想到:

在类路径资源中创建名称为entityManagerFactory的bean时出错:init方法调用失败

最后是:

Hibernate/JPA中使用Spring Boot和Oracle的多对多映射


我通过将此配置添加到Application.java文件来修复它。


如果您有两个名称相同的bean,也会发生这种情况。

例如,如果你有一个工厂方法monkey创建了monkey,而你错误地将另一个工厂方法(创建Car)也命名为monkey。

有问题的代码:

    @Bean
    public Car monkey() {
        return new Car();
    }

应该是:

    @Bean
    public Car car() {
        return new Car();
    }

参考:

默认情况下,bean名称将与方法名称相同…

春季文档1

通过属性名自动装配。春天找一颗同样的豆子 名称作为需要自动连接的属性。例如,如果a Bean定义被设置为按名称自动装配,并且它包含一个master 属性(也就是说,它有一个setMaster(..)方法),Spring寻找一个 Bean定义名为master,并使用它来设置属性。

春季文档2


对于没有找到Bean的类使用@Component。 和使用

@ComponentScan(“< Use_The_Root_Directory_Of_Main_Class >”)

. 当组件分布在许多目录中时,就会发生这种情况。

@Configuration
@SpringBootApplication
@ComponentScan({"com.example.demo"})
public class Application {
}