【发布时间】:2018-08-27 00:56:30
【问题描述】:
我正在构建一个使用 Spring data jpa 功能的 Spring-boot 应用程序。
请在下面找到我的 dao 层代码
package com.adv.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerDao extends JpaRepository<Customer, String> {
}
我正在使用如下 DaoProvider 类:
package com.adv.dao;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class DaoProvider implements Serializable {
private static final long serialVersionUID = 1L;
@Autowired
private CustomerDao customerDao;
public CustomerDao getCustomerDao() {
return customerDao;
}
}
我的spring boot主类定义如下:
@SpringBootApplication
@ComponentScan(basePackages="com.adv")
public class AdvMain extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AdvMain.class);
}
public static void main(String[] args) {
SpringApplication.run(AdvMain.class, args);
}
}
现在在运行时出现以下异常:
Field customerDao in com.adv.dao.DaoProvider required a bean of type 'com.adv.dao.CustomerDao' that could not be found.
我猜CustomerDao 接口上的@Repository 注释不起作用。
但我无法找出问题所在。谁能找出问题所在?
【问题讨论】:
-
尝试按照Can't Autowire @Repository annotated interface in Spring Boot的建议在
AdvMain上添加@EnableJpaRepositories("com.adv.dao") -
ok.thanks. 添加@EnableJpaRepositories("com.adv.dao") 后它工作了
标签: java spring spring-boot spring-data-jpa