簡體   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