Autowiring :expected at least 1 bean which qualifies as autowire candidate for this dependency

前端 未结 5 1684
终归单人心
终归单人心 2021-01-12 06:16

Okay, I know that there are many questions asked around the same topic. But I cant seem to make anything work. It also might be the case that I am yet to completely understa

5条回答
  •  暖寄归人
    2021-01-12 06:24

    You need to enable the JPA repositories in your config class, specify the package that contains the repositories as below

    @Configuration
    @EnableJpaRepositories(basePackages = {
        "com.springBoot.usl.repo"
    })
    public class ApplicationConfig {
    
    }
    

    Example of ApplicationConfig:

    @Configuration
    @EnableJpaRepositories(basePackages = {"com.springBoot.usl.repo"})
    @EnableTransactionManagement
    public class ApplicationConfig {
    
        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/xxxx");
            dataSource.setUsername("xxxx");
            dataSource.setPassword("xxxx");
    
            return dataSource;
        }
    
        @Bean
        public EntityManagerFactory entityManagerFactory() {
            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            vendorAdapter.setGenerateDdl(true);
            LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
            factory.setJpaVendorAdapter(vendorAdapter);
            factory.setPackagesToScan("xx.xxxx.xxxx.xxxx.domain");
            factory.setDataSource(dataSource());
            factory.afterPropertiesSet();
            return factory.getObject();
        }
    
        @Bean
        public PlatformTransactionManager transactionManager() {
            JpaTransactionManager txManager = new JpaTransactionManager();
            txManager.setEntityManagerFactory(entityManagerFactory());
            return txManager;
        }
    

提交回复
热议问题