找不到类型为String的Bean

问题描述 投票:0回答:1

这个webapp要用数据库,但目前我从一个最简单的存根开始,就卡住了。

我有点无从下手,不知道是哪里出了问题。总的来说,我对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);
    }
}
java spring spring-boot javabeans
1个回答
0
投票

标记 getTableListBySchema 方法为 Autowired 告诉Spring在这里进行依赖注入。这就是为什么Spring要找一个类型为 Spring 来将其自动连接到方法参数中。移除 Autowired 注释。

顺便说一下,如果你正在开发一个api。你应该使用 @RestController@Controller


1
投票

问题出在这段代码上。

@Autowiredpublic List 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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.