繁体   English   中英

Spring无法运行MySQL访问数据

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

我是使用Spring的新手,我将在这里查看 access-data-with-MySQL教程。 我正在使用Eclipse,当我下载项目的所有代码,然后将项目作为Spring Boot App运行时,我收到错误:


应用程序未能启动


描述:

hello.MainController中的字段userRepository需要一个无法找到的类型为“hello.UserRepository”的bean。

行动:

考虑在配置中定义类型为“hello.UserRepository”的bean。

同样,这是直接来自教程的代码。 有人知道发生了什么事吗?

谢谢!

全程日志

  . ____ _ __ _ _ /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\ ( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\ \\\\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\\__, | / / / / =========|_|==============|___/=/_/_/_/ :: 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. 

我能够通过清除我的maven缓存来解决这个问题。 我这样做是通过手动删除我的:

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

文件夹,然后进行全新安装。 问题是什么,它是如何发展的,以及改变它以解决它我/我们可能永远不会知道的。 但是,事情似乎再次奏效。

感谢所有帮助过的人!

您是否注册了类型为UserRepository的存储库? 检查是否使用@Repository注释对存储库进行签名。

  1. 从git gs-acces-data-mysql-master下载项目
  2. 将文件解压缩到磁盘
  3. 打开Eclipse
  4. 单击文件>导入>现有Maven项目>下一步
  5. 单击“浏览”并选择磁盘上的gs-accessing-data-mysql-master文件夹,然后单击“下一步”
  6. 选择pom.xml和Finish
  7. 打开已导入的gs-mysql-data项目
  8. 右键单击Application.java并运行Java Application或Spring Boot Application

确保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