问题描述
我正在使用 SpringBoot 和 JPA.我收到一个无法完成的 @Autowired
错误.
I'm working with SpringBoot and JPA. I'm getting an error with an @Autowired
that could not be completed.
这是我的主要课程:
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;
}
}
这是我失败的课程:
package com.logger.impl;
@Configuration
@ComponentScan({"com.repository"})
@Component
public class RequestLoggerImpl implements RequestLogger {
@Autowired
private RequestLogDao requestLogDao;
}
这是 RequestLogDao
类:
package com.repository;
public interface RequestLogDao extends CrudRepository<RequestLog, Integer> {
}
这是错误信息:
创建名为requestLoggerImpl"的 bean 时出错:注入自动装配的依赖项失败;
无法自动装配字段:private com.repository.RequestLogDao com.logger.impl.RequestLoggerImpl.requestLogDao;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到类型为 [com.repository.RequestLogDao] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
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)}
我就是不知道为什么它不能自动装配.我曾尝试在我的主类中添加 @EnableJpaRepositories
,但这不起作用.有什么建议吗?提前致谢!
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!
推荐答案
对于你的请求记录器,首先移除配置和组件扫描
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;
}
既然你已经放置了一个@Configuration,我假设你需要一个配置类,这样你就可以像这样创建一个:
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> {
// ...
}
终于在你的主课上
@SpringBootApplication // scan base packages for autowiring as needed
public class InitBatch implements CommandLineRunner {
// ...
}
最后检查您的依赖项.
这篇关于SpringBoot CRUD 存储库无法自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!