【发布时间】:2017-09-11 21:49:58
【问题描述】:
我使用 Spring,但从未尝试过 Spring Boot。我想尝试访问数据 jpa 教程 - https://spring.io/guides/gs/accessing-data-jpa/。我已经从 git 克隆了存储库,并按照教程中的描述使用 Maven 运行它: 混帐克隆https://github.com/spring-guides/gs-accessing-data-jpa.git mvnw spring-boot:run
运行这个,我得到一个错误 - Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.CrudRepository
为了解决这个问题,我添加了一个依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
这有帮助,但是,我遇到了另一个错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method demo in hello.Application required a bean of type 'hello.CustomerRepository' that could not be found.
Action:
Consider defining a bean of type 'hello.CustomerRepository' in your configuration.
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo' defined in hello.Application: Unsatisfied dependency expressed through method 'demo' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at hello.Application.main(Application.java:17)
... 6 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 24 more
我很惊讶 Spring 的官方教程不起作用。你试过这个教程吗?你有什么建议为什么它不起作用?是坏了还是我的机器出了什么问题?由于我对 Spring Boot 知之甚少,我不太确定问题可能出在哪里。
【问题讨论】:
-
如果您按照教程操作但没有成功,请在 repo 上提出问题。和github.com/spring-guides/gs-accessing-data-jpa/issues/20一样吗?
-
假设你可以从here开始使用Spring boot
-
git clone https://github.com/spring-guides/gs-accessing-data-jpa spj,然后是cd spj/complete,然后是mvn spring-boot:run,对我来说非常适合。您如何尝试运行示例? -
感谢您的建议 - 我已经更改了 Spring Data 的版本并完全删除了我的 Maven 存储库。在此之后,教程开始工作。我的 Maven 存储库一定以某种方式损坏了。我只是没有意识到,这可能是问题所在。
标签: spring spring-boot spring-data spring-data-jpa