java - 找不到 String 类型的 Bean
问题描述
这个 web 应用程序将与数据库一起使用,但现在我从一个最简单的存根开始并卡住了。
我有点不知道出了什么问题。总的来说,我对 Spring 或如何理解和跟踪此类错误不太熟悉。我在这里唯一理解的是它与方法参数的类型无关。我已经用谷歌搜索了这个错误,在此处查看了有关此类错误的一些答案,但未能找到正确的解决方案。
所以,我得到这个:
:: Spring Boot :: (v2.2.5.RELEASE)
2020-05-22 15:44:36.132 INFO 17992 --- [ main] c.rinkashikachi.SpringReactApplication : Starting SpringReactApplication v0.0.1-SNAPSHOT on DESKTOP-3BPPMPQ with PID 17992 (D:\Projects\J
ava\zni\target\zni-0.0.1-SNAPSHOT.jar started by 15rin in D:\Projects\Java\zni)
2020-05-22 15:44:36.135 INFO 17992 --- [ main] c.rinkashikachi.SpringReactApplication : No active profile set, falling back to default profiles: default
2020-05-22 15:44:37.108 INFO 17992 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-22 15:44:37.116 INFO 17992 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-22 15:44:37.116 INFO 17992 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-05-22 15:44:37.166 INFO 17992 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-22 15:44:37.166 INFO 17992 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 999 ms
2020-05-22 15:44:37.241 WARN 17992 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springfram
ework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'utilController' defined in URL [jar:file:/D:/Projects/Java/zni/target/zni-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/co
m/rinkashikachi/controllers/UtilController.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyExcep
tion: Error creating bean with name 'databaseMetaDataService': Unsatisfied dependency expressed through method 'getTableListBySchema' parameter 0; nested exception is org.springframework.beans.fact
ory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-05-22 15:44:37.244 INFO 17992 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-05-22 15:44:37.252 INFO 17992 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-22 15:44:37.326 ERROR 17992 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method getTableListBySchema in com.rinkashikachi.service.DatabaseMetaDataService required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
这就是方法所在。
package com.rinkashikachi.service;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.rinkashikachi.service.repositories.ColNameEntity;
import com.rinkashikachi.service.repositories.TableNameEntity;
import java.util.ArrayList;
import java.util.List;
@Service("databaseMetaDataService")
public class DatabaseMetaDataService {
@Autowired
public List<TableNameEntity> getTableListBySchema(String schema) {
// Stub
List<TableNameEntity> names = new ArrayList<>(3);
switch(schema) {
case "ADDITIONAL":
names.add(new TableNameEntity(1L, "ADDITIONAL1"));
names.add(new TableNameEntity(2L, "ADDITIONAL2"));
names.add(new TableNameEntity(3L, "ADDITIONAL3"));
break;
case "BOOKKEEPING":
names.add(new TableNameEntity(1L, "BOOKKEEPING1"));
names.add(new TableNameEntity(2L, "BOOKKEEPING2"));
names.add(new TableNameEntity(3L, "BOOKKEEPING3"));
break;
}
return names;
}
}
这就是我使用它的地方:
package com.rinkashikachi.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.rinkashikachi.service.DatabaseMetaDataService;
import com.rinkashikachi.service.repositories.TableNameEntity;
@Controller
@RequestMapping(value="/api")
public class UtilController {
private final DatabaseMetaDataService databaseMetaDataService;
@Autowired
public UtilController(DatabaseMetaDataService databaseMetaDataService) {
this.databaseMetaDataService = databaseMetaDataService;
}
@GetMapping(value="/tech")
public ResponseEntity<List<String>> getTechData(
@RequestParam(value="schema") String schema,
@RequestParam(value="table", required = false) String table,
@RequestParam(value="column", required = false) String column) {
List<TableNameEntity> entityList = databaseMetaDataService.getTableListBySchema(schema);
List<String> tables = new ArrayList<>(entityList.size());
for (TableNameEntity entity : entityList) {
tables.add(entity.toString());
System.out.println(entity);
}
return !tables.isEmpty()
? new ResponseEntity<>(tables, HttpStatus.OK)
: new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
}
解决方案
问题在于这段代码:
@Autowired 公共列表 getTableListBySchema(字符串模式){
你能试一下吗:
package com.rinkashikachi.service;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.rinkashikachi.service.repositories.ColNameEntity;
import com.rinkashikachi.service.repositories.TableNameEntity;
import java.util.ArrayList;
import java.util.List;
@Service("databaseMetaDataService")
public class DatabaseMetaDataService {
public List<TableNameEntity> getTableListBySchema(String schema) {
// Stub
List<TableNameEntity> names = new ArrayList<>(3);
switch(schema) {
case "ADDITIONAL":
names.add(new TableNameEntity(1L, "ADDITIONAL1"));
names.add(new TableNameEntity(2L, "ADDITIONAL2"));
names.add(new TableNameEntity(3L, "ADDITIONAL3"));
break;
case "BOOKKEEPING":
names.add(new TableNameEntity(1L, "BOOKKEEPING1"));
names.add(new TableNameEntity(2L, "BOOKKEEPING2"));
names.add(new TableNameEntity(3L, "BOOKKEEPING3"));
break;
}
return names;
}
}
package com.rinkashikachi.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.rinkashikachi.service.DatabaseMetaDataService;
import com.rinkashikachi.service.repositories.TableNameEntity;
@Controller
@RequestMapping(value="/api")
public class UtilController {
@Autowired
private DatabaseMetaDataService databaseMetaDataService;
@GetMapping(value="/tech")
public ResponseEntity<List<String>> getTechData(
@RequestParam(value="schema") String schema,
@RequestParam(value="table", required = false) String table,
@RequestParam(value="column", required = false) String column) {
List<TableNameEntity> entityList = databaseMetaDataService.getTableListBySchema(schema);
List<String> tables = new ArrayList<>(entityList.size());
for (TableNameEntity entity : entityList) {
tables.add(entity.toString());
System.out.println(entity);
}
return !tables.isEmpty()
? new ResponseEntity<>(tables, HttpStatus.OK)
: new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
}
推荐阅读
- javascript - antd table如何固定第一行
- python - 如果条件无法正常工作且计数器未更新
- python - Python 字典 has_key
- spring-boot - 如何模拟 ResponseEntity方法
- java - 允许变量影响循环中的语句数
- java - 对Java中的偶数索引位置求和
- android - 如何使用存储库设计清除 android mvvm 中的 rxJava 一次性用品?
- android - 将 Unity 模块集成到 Android 模块时避免版本冲突
- azure - 在 Office 365 中安装应用程序时出错:AADSTS650051:应用程序“CLIENT_ID”正在请求无效或过期的权限
- java - 当我完成推送我的项目时,它会报告“java.lang.NoSuchMethodError: org.springframework.beans.factory.support.RootBeanDefinition.setSingleton(Z)V”