简体   繁体   中英

Spring NoSuchBeanDefinitionException: No Qualifying Bean of Type [@Repository class] available

I see a lot of posts surrounding this error message, but none with a solution that's worked for me.

I'm writing a Spring application that has required me to configure multiple data sources. I got all of those working, but now I need to import a module my teammates built that uses an additional data source and several repositories. They've been using this module in their code and it seems to be working for them, but when I try to run my code I get the following error (some details obscured for work reasons):

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.company.teammodule.repositories.StatsRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

//I can't paste code, so there may be typos...

Spring Application Class:

package com.company.mymodule;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages={
    "com.company.mymodule",
    "com.company.mymodule.utils",
    "com.company.mymodule.configuration",
    "com.company.teammodule.repositories",
    //I've been fiddling with packages here; I've added every package from the teammatemodule app and still get the error described})
@public class MyModuleApplication
{
    public static void main(String[] args) { SpringApplication.run(MyModuleApplication.class, args); }
}

My class that uses the teammatemodule:

package com.company.mymodule.utils;

@Component
public class TeammateModuleUtil {
    @Autowired
    private TeammateModuleConfiguration config; //This class contains an object of another class which references the problem repository
    
    @Value("${applicationName})
    private final String applicationName;
    
    public TeammateModuleUtil()
    {
        try {
            config.setApplicationName(applicationName);
            config.loadConfiguration();
        }
        catch (Exception e) {
            //Error handling
        }
    }
}

The TeammateModuleConfiguration class:

package com.company.teammatemodule

@Component
public class TeammateModuleConfiguration extends ConfigurationBase {
    @Autowired
    TeammateModuleServiceData data; //This class contains a reference to the problem repository
    @Autowired
    Utilities util;
    @Autowired
    ConfigurationRepository configurationRepository;

    public void loadConfiguration() throws Exception {
        try {
            this.alConfigure = this.configurationRepository.findByConfigureIdApplicationName(this.applicationName);
        } catch (Exception e) {
            //Error handling
        }
    }
}

Here's the class that has the problem reference:

package com.company.teammatemodule;

@Component
public class TeammateModuleServiceData {
    @Autowired
    StatsRepository statsRepository //This is the repo the code can't find a bean for
    @Autowired
    MessageAuthorizationRepository messageAuthorizationRepository;
    @Autowired
    LogMessagesRepository logMessagesRepository;
    @Autowired
    Utilities util;

    //Class methods
}

And here's the repository class for good measure:

package com.company.teammatemodule.repositories;

@Repository
public interface StatsRepository extends JpaRepository<Stats, StatsId> {
    @Procedure(
        procedureName = "schema.log_service_stats",
        outputParameterName = "o_stats_id"
    )
    String logServiceStats(@Param("i_request_payload") String var1, @Param("i_response_payload") String var2, @Param("i_operation_name") String var3, @Param("i_stats_id") String var4);
    
    @Procedure(procedureName = "schema.update_service_stats)
    void updateServiceStats(@Param("i_request_payload") String var1, @Param("i_response_payload") String var2, @Param("i_operation_name") String var3, @Param("i_stats_id) String var4);
}

The above repository is the one that can't be found when I try to launch the application. What I've checked and tried: The main application has the @SpringApplication annotation, and all other involved classes have either @Component , @RestController , or @Repository , so I don't think it's an issue of some component not being labeled a bean... None of the other repositories in teammatemodule are giving me a problem, but I'm guessing StatsRepository is just the first repository to be referenced The only properties in my properties file are applicationName and data sources unrelated to the teammatemodule. I'm not listing any packages or exclusions there. I've specified all packages in @ComponentScan ( basepackages = {//packages} ) as well as @SpringApplication( scanBasePackages = {//packages} ) I've tried excluding spring's autoconfigure by using the following annotations in my SpringAplication class:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })

So I don't think it's a missing @Component or other bean annotation, and to the best of my ability I've specified the package in which the repository bean is located and overridden Spring's built-in autoconfiguration tools... But the app still can't find the bean.

Thanks for your help!

看起来这可能只是一个简单的拼写错误: com.company.teammodule.repositories <- 组件扫描 com.company.teammatemodule.repositories <- 在 Repository 类中指定的包

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM