简体   繁体   English

Spring无法运行MySQL访问数据

[英]Spring Accessing data with MySQL Won't Run

I'm new to using Spring, and I'm going through the accessing-data-with-MySQL tutorial here . 我是使用Spring的新手,我将在这里查看 access-data-with-MySQL教程。 I'm using Eclipse, and when I download all the code for the project, and then run the project as a Spring Boot App, I get the error: 我正在使用Eclipse,当我下载项目的所有代码,然后将项目作为Spring Boot App运行时,我收到错误:


APPLICATION FAILED TO START 应用程序未能启动


Description: 描述:

Field userRepository in hello.MainController required a bean of type 'hello.UserRepository' that could not be found. hello.MainController中的字段userRepository需要一个无法找到的类型为“hello.UserRepository”的bean。

Action: 行动:

Consider defining a bean of type 'hello.UserRepository' in your configuration. 考虑在配置中定义类型为“hello.UserRepository”的bean。

Again, this is the code directly from the tutorial. 同样,这是直接来自教程的代码。 Anybody have any idea what's going on? 有人知道发生了什么事吗?

Thanks! 谢谢!

Full run log 全程日志

  . ____ _ __ _ _ /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\ ( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\ \\\\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.6.RELEASE) 2017-08-26 21:58:28.365 INFO 2308 --- [ main] hello.Application : Starting Application on DESKTOP-F9F09K7 with PID 2308 (C:\\Users\\dylan\\Desktop\\gs-accessing-data-mysql-master\\complete\\target\\classes started by dylan in C:\\Users\\dylan\\Desktop\\gs-accessing-data-mysql-master\\complete) 2017-08-26 21:58:28.367 INFO 2308 --- [ main] hello.Application : No active profile set, falling back to default profiles: default 2017-08-26 21:58:28.411 INFO 2308 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6b53e23f: startup date [Sat Aug 26 21:58:28 PDT 2017]; root of context hierarchy 2017-08-26 21:58:29.454 INFO 2308 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-08-26 21:58:29.465 INFO 2308 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-08-26 21:58:29.466 INFO 2308 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2017-08-26 21:58:29.542 INFO 2308 --- [ost-startStop-1] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-08-26 21:58:29.542 INFO 2308 --- [ost-startStop-1] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 1135 ms 2017-08-26 21:58:29.655 INFO 2308 --- [ost-startStop-1] osbwservlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-08-26 21:58:29.659 INFO 2308 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-08-26 21:58:29.667 INFO 2308 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-08-26 21:58:29.667 INFO 2308 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-08-26 21:58:29.667 INFO 2308 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-08-26 21:58:29.701 WARN 2308 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2017-08-26 21:58:29.703 INFO 2308 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-08-26 21:58:29.721 INFO 2308 --- [ main] utoConfigurationReportLoggingInitializer : Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-08-26 21:58:29.804 ERROR 2308 --- [ main] osbdLoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field userRepository in hello.MainController required a bean of type 'hello.UserRepository' that could not be found. Action: Consider defining a bean of type 'hello.UserRepository' in your configuration. 

I was able to fix this problem by clearing my maven cache. 我能够通过清除我的maven缓存来解决这个问题。 I did this by manually deleting my: 我这样做是通过手动删除我的:

C:\\Users\\username\\ .m2 C:\\ Users \\ username \\ .m2

folder and then doing a clean install. 文件夹,然后进行全新安装。 What the problem was, how it developed, and what changed to fix it I/we may never know. 问题是什么,它是如何发展的,以及改变它以解决它我/我们可能永远不会知道的。 But, things seem to be working again. 但是,事情似乎再次奏效。

Thanks to all who helped! 感谢所有帮助过的人!

Have you registered a repository with type UserRepository? 您是否注册了类型为UserRepository的存储库? Check if you sign the repository with a @Repository annotation. 检查是否使用@Repository注释对存储库进行签名。

  1. Download project from git gs-accessing-data-mysql-master 从git gs-acces-data-mysql-master下载项目
  2. Extract the file to your disk 将文件解压缩到磁盘
  3. Open Eclipse 打开Eclipse
  4. Click File > Import > Existing Maven Projects > Next 单击文件>导入>现有Maven项目>下一步
  5. Click Browse and select the gs-accessing-data-mysql-master folder that is on your disk and Next 单击“浏览”并选择磁盘上的gs-accessing-data-mysql-master文件夹,然后单击“下一步”
  6. Select both pom.xml and Finish 选择pom.xml和Finish
  7. Open gs-mysql-data project that has been imported 打开已导入的gs-mysql-data项目
  8. Right click on Application.java and run as Java Application or Spring Boot Application 右键单击Application.java并运行Java Application或Spring Boot Application

Make sure that UserRepository interface extends from CrudRepository , like this: 确保UserRepository接口从CrudRepository扩展,如下所示:

package hello;

import org.springframework.data.repository.CrudRepository;

import hello.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository<User, Long> {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM