找不到字符串类型的 Bean
Bean of type String that could not be found
此 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
public 列表 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);
}
}
将 getTableListBySchema
方法标记为 Autowired
告诉 spring 在这里进行依赖注入。这就是为什么 Spring 查找类型为 Spring
的 bean 以将其自动装配到方法参数的原因。从该方法中删除 Autowired
注释。
作为旁注,如果您正在开发 api。您应该使用 @RestController
而不是 @Controller
此 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 public 列表 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);
}
}
将 getTableListBySchema
方法标记为 Autowired
告诉 spring 在这里进行依赖注入。这就是为什么 Spring 查找类型为 Spring
的 bean 以将其自动装配到方法参数的原因。从该方法中删除 Autowired
注释。
作为旁注,如果您正在开发 api。您应该使用 @RestController
而不是 @Controller