2016-03-12 90 views
-1

我对Spring很新,我目前正在跟随一个关于如何将服务和存储库与@autowired一起使用的教程,但是在执行每个步骤之后它似乎没有工作我。从阅读错误我得到我可以看到,使用@Autowired注入bean不适用于我创建的服务类,因为它没有找到具有匹配条件的类。注入@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: {} 
+1

创建一个Bean,这是一种ArtistRepository,然后重试。 – JToddler

+1

您的应用程序环境中是否包含? – nnunes10

+0

@JToddler我有一个存储库类,其结构与我的服务类基本相同,但是使用Repository标记 – Sean

回答

0

确保您ArtistRepository延伸CrudRepository像下面。 SpringBoot将自动查找所有存储库并创建实例。您不需要注释存储库类。

public interface ArtistRepository extends CrudRepository<Artist, Long> 
相关问题