我是春天的新手,我目前正在学习如何使用服务和存储库以及@autowired,但是在完成每个步骤之后它似乎并不适合我。从阅读错误我得到我可以看到使用@Autowired注入bean并不适用于我创建的服务类,因为它没有找到具有匹配条件的类。
这是我的服务界面。
public interface ArtistService {
void save(Artist artist);
Artist get(int id);
void remove(Artist artist);
List<Artist> findAll();
}
这是我的类实现我的服务接口
@Service
public class ArtistServiceImpl implements ArtistService {
ArtistRepository artistRepository;
@Autowired
public ArtistServiceImpl(ArtistRepository artistRepository) {
this.artistRepository = artistRepository;
}
(实现了接口方法,但是为了保持这种紧凑而省略了)
我的启动应用
@SpringBootApplication
public class JdbcTutorialApplication implements CommandLineRunner {
@Autowired
ArtistService artistService;
@Autowired
ArtistRepository artistRepository;
}
@Autowired也没有为我想要添加的存储库工作,但它基本上与此完全相同,但它是一个存储库。
以下是我得到的错误的第一行
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTutorialApplication': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: ie.cit.oossp.service.ArtistService ie.cit.oossp.JdbcTutorialApplication.artistService; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'artistServiceImpl' defined in file [C:\Users\Sean\Documents\College\3rd Year\OOSSP\STSWorkspace\JdbcTutorial\target\classes\ie\cit\oossp\service\ArtistServiceImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [ic.cit.oossp.repository.ArtistRepository]: No qualifying bean of type [ic.cit.oossp.repository.ArtistRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ic.cit.oossp.repository.ArtistRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
答案 0 :(得分:0)
确保ArtistRepository
扩展CrudRepository
,如下所示。 SpringBoot将自动查找所有存储库并创建实例。您不需要注释存储库类。
public interface ArtistRepository extends CrudRepository<Artist, Long>