简体   繁体   中英

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 . 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:


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.

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. I did this by manually deleting my:

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? Check if you sign the repository with a @Repository annotation.

  1. Download project from git gs-accessing-data-mysql-master
  2. Extract the file to your disk
  3. Open Eclipse
  4. Click File > Import > Existing Maven Projects > Next
  5. Click Browse and select the gs-accessing-data-mysql-master folder that is on your disk and Next
  6. Select both pom.xml and Finish
  7. Open gs-mysql-data project that has been imported
  8. Right click on Application.java and run as Java Application or Spring Boot Application

Make sure that UserRepository interface extends from CrudRepository , like this:

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> {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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