Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @spring boot、JPA和Crudepository的autowire错误_Java_Jpa_Spring Boot - Fatal编程技术网

Java @spring boot、JPA和Crudepository的autowire错误

Java @spring boot、JPA和Crudepository的autowire错误,java,jpa,spring-boot,Java,Jpa,Spring Boot,我尝试将spring boot与JPA一起使用,但出现以下错误: 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.b

我尝试将spring boot与JPA一起使用,但出现以下错误:

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());
    }
}
@ComponentScan
@启用自动配置
公共类公司{
公共静态void main(字符串[]args)引发异常{
SpringApplication.run(Compta.class,args);
}
} 
@实体
@表(name=“ACCOUNT”)
公共类帐户{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@纵队
私有字符串名称;
公共帐户(){
超级();
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
@存储库
公共接口AccountRepository扩展了JpaRepository{
}
@服务
公共类AccountCommand{
@自动连线
私人帐户存储库;
公共帐户创建(){
返回accountRepository.save(新帐户());
}
}

有什么想法吗?

查看堆栈跟踪,您的
Compta
类似乎不在任何包中。这可能会导致问题(请参阅),因此我会先尝试将所有类放在一个包中

还要检查您的
AccountRepository
是否在同一个包中,或者是否在
Compta
的子包中


如果您仍然有问题,也许您可以共享一个完整的项目来复制问题?

如果所有这些类都在同一个包中,那么它就可以工作。我猜他们不是(所以Spring数据不知道在哪里扫描存储库)?回答得好,我把Compta类放在一个包中,一切正常。非常感谢