Java 找不到字符串类型的Bean
这个webapp将与数据库一起工作,但现在我已经从一个最简单的存根开始了,并且卡住了 我有点不知道怎么了。总的来说,我不太熟悉Spring,也不太熟悉如何理解和跟踪这些错误。这里我唯一能理解的是,它与方法参数的类型无关。我在谷歌上搜索了这个错误,在这里寻找了一些关于这种错误的答案,但是没有找到正确的解决方案 所以,我得到了这个:Java 找不到字符串类型的Bean,java,spring,spring-boot,javabeans,Java,Spring,Spring Boot,Javabeans,这个webapp将与数据库一起工作,但现在我已经从一个最简单的存根开始了,并且卡住了 我有点不知道怎么了。总的来说,我不太熟悉Spring,也不太熟悉如何理解和跟踪这些错误。这里我唯一能理解的是,它与方法参数的类型无关。我在谷歌上搜索了这个错误,在这里寻找了一些关于这种错误的答案,但是没有找到正确的解决方案 所以,我得到了这个: :: Spring Boot :: (v2.2.5.RELEASE) 2020-05-22 15:44:36.132 INFO 17992 ---
:: 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.service;
导入org.springframework.stereotype.Service;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.rinkashikachi.service.repositories.ColNameEntity;
导入com.rinkashikachi.service.repositories.TableNameEntity;
导入java.util.ArrayList;
导入java.util.List;
@服务(“databaseMetaDataService”)
公共类DatabaseMetaDataService{
@自动连线
公共列表getTableListBySchema(字符串模式){
//存根
列表名称=新的ArrayList(3);
开关(模式){
“附加”一案:
名称。添加(新的TableNameEntity(1L,“附加1”);
name.add(新的TableNameEntity(2L,“ADDITIONAL2”);
名称。添加(新的TableNameEntity(3L,“附加3”);
打破
“簿记”一案:
名称。添加(新的TableNameEntity(1L,“簿记1”);
名称。添加(新的TableNameEntity(2L,“簿记2”);
添加(新的TableNameEntity(3L,“簿记3”);
打破
}
返回姓名;
}
}
这就是我使用它的地方:
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);
}
}
package com.rinkashikachi.controllers;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.rinkashikachi.service.DatabaseMetaDataService;
导入com.rinkashikachi.service.repositories.TableNameEntity;
@控制器
@请求映射(value=“/api”)
公共类实用控制器{
私有最终数据库MetadataService DatabaseMetaDataService;
@自动连线
公共UtilController(DatabaseMetaDataService DatabaseMetaDataService){
this.databaseMetaDataService=databaseMetaDataService;
}
@GetMapping(value=“/tech”)
公共响应获取数据(
@RequestParam(value=“schema”)字符串模式,
@RequestParam(value=“table”,required=false)字符串表,
@RequestParam(value=“column”,required=false)字符串(column){
List entityList=databaseMetaDataService.getTableListBySchema(模式);
列表表=新的ArrayList(entityList.size());
for(TableName实体:entityList){
tables.add(entity.toString());
系统输出打印项次(实体);
}
return!tables.isEmpty()
?新的响应性(表,HttpStatus.OK)
:新响应属性(null,HttpStatus.BAD_请求);
}
}
此代码存在问题:
@自动连线
公共列表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.service;
导入org.springframework.stereotype.Service;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.rinkashikachi.service.repositories.ColNameEntity;
导入com.rinkashikachi.service.repositories.TableNameEntity;
导入java.util.ArrayList;
导入java.util.List;
@服务(“databaseMetaDataService”)
公共类DatabaseMetaDataService{
公共列表getTableListBySchema(字符串模式){
//存根
列表名称=新的ArrayList(3);
开关(模式){
“附加”一案:
名称。添加(新的TableNameEntity(1L,“附加1”);
name.add(新的TableNameEntity(2L,“ADDITIONAL2”);
名称。添加(新的TableNameEntity(3L,“附加3”);
打破
“簿记”一案:
名称。添加(新的TableNameEntity(1L,“簿记1”);
名称。添加(新的TableNameEntity(2L,“簿记2”);
添加(新的TableNameEntity(3L,“簿记3”);
打破
}
返回姓名;
}
}
package com.rinkashikachi.controllers;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.rinkashikachi.service.DatabaseMetaDataService;
导入com.rinkashikachi.service.repositories.TableNameEntity;
@控制器
@请求映射(value=“/api”)
公共类实用控制器{
@自动连线
私有数据库元数据服务数据库元数据服务;
@GetMapping(value=“/tech”)
公共响应获取数据(
@RequestParam(value=“schema”)字符串模式,
@RequestParam(value=“table”,required=false)字符串表,
@RequestParam(value=“column”,required=false)字符串(column){
List entityList=databaseMetaDataService.getTableListBySchema(模式);
列表表=新的ArrayList(entityList.size());
for(TableName实体:entityList){
tables.add(entity.toString());
系统输出打印项次(实体);
}
return!tables.isEmpty()
?新响应(表,HttpSta)
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);
}
}