我剛剛使用Spring Boot V1.3.5設置了一個新項目,並且在嘗試將存儲庫自動裝入到服務中時不斷收到NoSuchBeanDefinitionException異常。這很奇怪,因爲我有其他項目設置相同的方式工作正常。將NoSuchBeanDefinitionException存儲庫投入服務
我的應用程序類。
package api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("-------------------");
System.out.println("The API is running.");
System.out.println("-------------------");
}
}
我的服務。
package api.services;
import api.entity.Project;
import api.repository.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProjectService {
@Autowired
private ProjectRepository projectRepository;
/**
* Saves a project entity into the database.
*
* @param project Project
* @return Project
*/
public Project save(Project project) {
return this.projectRepository.save(project);
}
}
我的存儲庫。現在
package api.repository;
import api.entity.Project;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProjectRepository extends CrudRepository<Project, Integer> {
Project findByName(String name);
}
我的服務被自動連接到我的控制器,但春天似乎並不喜歡我出於某種原因庫。
任何人都可以看到什麼是錯的/失蹤?
感謝。
異常消息是:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private api.repository.ProjectRepository api.services.ProjectService.projectRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [api.repository.ProjectRepository] 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)}
你有任何具體的實現你的ProjectRepository?它不能僅實例化一個接口。 – Gandalf
當然,你不能實例化一個接口,但我沒有實例化它,我自動裝配它。我在不同的項目中具有相同的設置,並且工作正常。從文檔「但是這正是Spring Data JPA如此強大的原因:您不必編寫一個存儲庫接口的實現。當您運行應用程序時,Spring Data JPA即時創建實現。」 –
我試着創建一個類似的示例項目,這是可用[這裏](https://github.com/vamsilp/testProjects.git),它爲我工作。請公開項目實體類,因爲我猜測中的方法簽名ProjectRepository可能不正確。 –