我正在构建一个Spring-boot应用程序,其中我使用了Spring data jpa功能。
请查看下面的dao层代码。
我将使用DaoProvider类,如下所示:
请查看下面的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
无法起作用。
但是我无法找出问题所在。有人能够找出问题吗?
AdvMain
上添加@EnableJpaRepositories("com.adv.dao")
。 - Al-Mustafa Azhari