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!
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.
For your request logger, remove the configuration and component scan first
Since you've placed a @Configuration, i assume you will need a config class so you can create one like this:
Take note of your base package here; as it will be used for:
Finally in your Main class
Lastly check your dependencies.
Remove both
from RequestLoggerImpl
And remove
@EnableJpaRepositories(basePackages="com.repository")
from main class. And move main class to directly undercom
packageAnd I assume, you have
spring-boot-starter-data-jpa
dependency added