2014-05-13 446 views
4

@autowire錯誤我試圖用彈簧啓動JPA和我得到了以下錯誤:與春天開機,JPA和CrudRepository

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountCommand': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.compta.repositories.AccountRepository com.compta.commands.AccountCommand.accountRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.compta.repositories.AccountRepository] 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)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120) 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898) 
at Compta.main(Compta.java:12) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.compta.repositories.AccountRepository com.compta.commands.AccountCommand.accountRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.compta.repositories.AccountRepository] 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)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
... 16 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.compta.repositories.AccountRepository] 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)} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
... 18 more 

我的代碼:

@ComponentScan 
@EnableAutoConfiguration 
public class Compta { 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(Compta.class, args); 
    } 
} 

@Entity 
@Table(name = "ACCOUNT") 
public class Account { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    @Column 
    private String name; 

    public Account() { 
     super(); 
    } 

    public long getId() { 
    return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

@Repository 
public interface AccountRepository extends JpaRepository<Account, Long> { 

} 

@Service 
public class AccountCommand { 

    @Autowired 
    private AccountRepository accountRepository; 

    public Account create() { 
     return accountRepository.save(new Account()); 
    } 
} 

任何想法?

+0

如果所有這些類都在同一個包中,它將起作用。我猜他們不是(所以Spring Data不知道在哪裏掃描你的倉庫)? –

回答

7

看着你的堆棧跟蹤,看起來你的Compta類不在任何包中。這可能會導致問題(請參閱http://docs.spring.io/spring-boot/docs/current/reference//htmlsingle/#using-boot-using-the-default-package),因此我會嘗試將所有類放入包中。

此外,請檢查您的AccountRepository是在同一個包中,還是在Compta的子包中。

如果你仍然有問題,也許你可以分享一個完整的項目,複製問題?

+0

好的答案,我將Compta課程移到了一個包中,一切正常。非常感謝您 – Antoine

+0

這也解決了我同樣的問題。謝謝! –