SpringBoot CRUD Repository fail to Autowire

2019-09-06 10:42发布

I'm working with SpringBoot and JPA. I'm getting an error with an @Autowired that could not be completed.

This is my main class:

package com;

@SpringBootApplication
@EnableJpaRepositories(basePackages="com.repository")
public class InitBatch implements CommandLineRunner {

    @Autowired
    private Batch batch;

    @Autowired
    private CareDao careDAO;


    @Override
    public void run(String... args) throws Exception {
        careDAO.setMessageSource(messageSource());      
        batch.processFiles();   
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(InitBatch.class, args).close();
        System.out.println("Finish");
    }


    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

        messageSource.setBasename("file:/config/instances");
        messageSource.setCacheSeconds(100);
        return messageSource;
    }   
}

This my class that fails:

package com.logger.impl;

@Configuration
@ComponentScan({"com.repository"})

@Component
public class RequestLoggerImpl implements RequestLogger {

    @Autowired
    private RequestLogDao requestLogDao;

}

This is the RequestLogDao class:

package com.repository;
public interface RequestLogDao extends CrudRepository<RequestLog, Integer> {

}

This is the error message:

Error creating bean with name 'requestLoggerImpl': Injection of autowired dependencies failed;

Could not autowire field: private com.repository.RequestLogDao com.logger.impl.RequestLoggerImpl.requestLogDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.repository.RequestLogDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I just can't find out why it can't autowire. I have tried adding @EnableJpaRepositories in my main class, but this did not work. Any suggestions? Thanks in advance!

3条回答
萌系小妹纸
2楼-- · 2019-09-06 10:46

Do you have a concrete class that implements RequestLogDao? I don't see one.

You need to have one (a Java class), and you need to make an instance of it available in the application context.

查看更多
在下西门庆
3楼-- · 2019-09-06 10:54

For your request logger, remove the configuration and component scan first

package com.logger.impl;

@Component
public class RequestLoggerImpl implements RequestLogger {

    @Autowired
    private RequestLogDao requestLogDao;

}

Since you've placed a @Configuration, i assume you will need a config class so you can create one like this:

@Configuration
@EnableJpaRepositories(basePackages = {"com.repository"}
public class MyConfiguration {
    // possibly your Bean declarations here
    // like dataSource, transactionManager etc.. related to your jpa repo
    // as needed
}

Take note of your base package here; as it will be used for:

package com.repository;

@Repository
public interface RequestLogDao extends CrudRepository<RequestLog, Integer> {
    // ...
}

Finally in your Main class

@SpringBootApplication // scan base packages for autowiring as needed
public class InitBatch implements CommandLineRunner {
    // ...
}

Lastly check your dependencies.

查看更多
Evening l夕情丶
4楼-- · 2019-09-06 11:04

Remove both

@Configuration
@ComponentScan({"com.repository"}) 

from RequestLoggerImpl

And remove @EnableJpaRepositories(basePackages="com.repository") from main class. And move main class to directly under com package

And I assume, you have spring-boot-starter-data-jpa dependency added

查看更多
登录 后发表回答