com.din.OSS 中构造函数的参数 0 需要找不到类型为“java.lang.String”的 bean

Posted

技术标签:

【中文标题】com.din.OSS 中构造函数的参数 0 需要找不到类型为“java.lang.String”的 bean【英文标题】:Parameter 0 of constructor in com.din.OSS required a bean of type 'java.lang.String' that could not be found 【发布时间】:2018-12-06 10:24:59 【问题描述】:

运行应用程序后控制台中显示的错误粘贴在下方

18:46:00.752 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
18:46:00.754 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
18:46:00.754 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/D:/Automation/sts/stsworkspace/BduckApp-1/target/classes/]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m[2m (v2.1.0.BUILD-SNAPSHOT)[0;39m

[2m2018-06-27 18:46:00.946[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.din.BduckApp1Application            [0;39m [2m:[0;39m Starting BduckApp1Application on KOR1064502 with PID 14192 (D:\Automation\sts\stsworkspace\BduckApp-1\target\classes started by rad9kor in D:\Automation\sts\stsworkspace\BduckApp-1)
[2m2018-06-27 18:46:00.947[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.din.BduckApp1Application            [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2018-06-27 18:46:00.991[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@47ebf699: startup date [Wed Jun 27 18:46:00 IST 2018]; root of context hierarchy
[2m2018-06-27 18:46:01.785[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mtrationDelegate$BeanPostProcessorChecker[0;39m [2m:[0;39m Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$6d259a6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2m2018-06-27 18:46:02.216[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2018-06-27 18:46:02.229[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2018-06-27 18:46:02.229[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet Engine: Apache Tomcat/8.5.31
[2m2018-06-27 18:46:02.233[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.a.catalina.core.AprLifecycleListener  [0;39m [2m:[0;39m The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre8/bin/server;C:/Program Files/Java/jre8/bin;C:/Program Files/Java/jre8/lib/amd64;C:\Perl520\Perl64\bin;C:\Perl520\Perl64\site\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\7-Zip;C:\Program Files\TortoiseSVN\bin;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;C:\Program Files\7-Zip;C:\Program Files\Black Duck Software\protexIP\bin;C:\Program Files\mysql\MySQL Utilities 1.6\;C:\Program Files\Black Duck Software\protexIP\bin;C:\Program Files\7-Zip;C:\Program Files (x86)\Toolbase Client;C:\Program Files\Git\cmd;C:\Users\rad9kor\anaconda3;C:\Users\rad9kor\anaconda3\Library\mingw-w64\bin;C:\Users\rad9kor\anaconda3\Library\usr\bin;C:\Users\rad9kor\anaconda3\Library\bin;C:\Users\rad9kor\anaconda3\Scripts;D:\Automation\sts\tool\sts-bundle\sts-3.9.4.RELEASE;;.]
[2m2018-06-27 18:46:02.329[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2018-06-27 18:46:02.329[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.web.context.ContextLoader           [0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1340 ms
[2m2018-06-27 18:46:02.459[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.ServletRegistrationBean [0;39m [2m:[0;39m Servlet dispatcherServlet mapped to [/]
[2m2018-06-27 18:46:02.460[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.ServletRegistrationBean [0;39m [2m:[0;39m Servlet webServlet mapped to [/h2-console/*]
[2m2018-06-27 18:46:02.462[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean  [0;39m [2m:[0;39m Mapping filter: 'characterEncodingFilter' to: [/*]
[2m2018-06-27 18:46:02.463[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean  [0;39m [2m:[0;39m Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
[2m2018-06-27 18:46:02.463[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean  [0;39m [2m:[0;39m Mapping filter: 'httpPutFormContentFilter' to: [/*]
[2m2018-06-27 18:46:02.463[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean  [0;39m [2m:[0;39m Mapping filter: 'requestContextFilter' to: [/*]
[2m2018-06-27 18:46:02.570[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Starting...
[2m2018-06-27 18:46:02.628[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[2m2018-06-27 18:46:02.660[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Building JPA container EntityManagerFactory for persistence unit 'default'
[2m2018-06-27 18:46:02.669[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
[2m2018-06-27 18:46:02.816[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.Version                   [0;39m [2m:[0;39m HHH000412: Hibernate Core 5.3.1.Final
[2m2018-06-27 18:46:02.817[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.cfg.Environment           [0;39m [2m:[0;39m HHH000206: hibernate.properties not found
[2m2018-06-27 18:46:02.869[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.hibernate.annotations.common.Version  [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations 5.0.3.Final
[2m2018-06-27 18:46:02.954[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.dialect.Dialect           [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
[2m2018-06-27 18:46:03.084[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.h.t.schema.internal.SchemaCreatorImpl [0;39m [2m:[0;39m HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@152501c5'
[2m2018-06-27 18:46:03.086[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2018-06-27 18:46:03.099[0;39m [33m WARN[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bduckTrigger': Unsatisfied dependency expressed through field 'oss'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'OSS' defined in file [D:\Automation\sts\stsworkspace\BduckApp-1\target\classes\com\din\OSS.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
[2m2018-06-27 18:46:03.099[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Closing JPA EntityManagerFactory for persistence unit 'default'
[2m2018-06-27 18:46:03.099[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36m.SchemaDropperImpl$DelayedDropActionImpl[0;39m [2m:[0;39m HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
[2m2018-06-27 18:46:03.101[0;39m [33m WARN[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.f.support.DisposableBeanAdapter   [0;39m [2m:[0;39m Invocation of destroy method failed on bean with name 'inMemoryDatabaseShutdownExecutor': org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-197]
[2m2018-06-27 18:46:03.101[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Shutdown initiated...
[2m2018-06-27 18:46:03.103[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Shutdown completed.
[2m2018-06-27 18:46:03.105[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Stopping service [Tomcat]
[2m2018-06-27 18:46:03.114[0;39m [32m INFO[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConditionEvaluationReportLoggingListener[0;39m [2m:[0;39m 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2m2018-06-27 18:46:03.208[0;39m [31mERROR[0;39m [35m14192[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter  [0;39m [2m:[0;39m 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.din.OSS 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.

包下有3个类

com.din

BduckApp1Application.java

BduckTrigger.java

OSS.java

BduckApp1Application.java

package com.din;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
  public class BduckApp1Application 

public static void main(String[] args) 
    SpringApplication.run(BduckApp1Application.class, args);


BduckTrigger.java

package com.din;

import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BduckTrigger 
@Autowired
OSS oss;

Vector<OSS> osses = new Vector<>();
@RequestMapping("/bd")
public Vector<OSS> LogintoProtex() 
    oss = OSS.builder().name("Dinesh").version("1.0").license("apache 2.0").usage("dynamic").comment("approved").build();
    osses.add(oss);
    oss = OSS.builder().name("Nikesh").version("2.0").license("BSD 2.0").usage("static").comment("disapproved").build();
    osses.add(oss);
    return osses;


OSS.java

package com.din;

import org.springframework.stereotype.Component;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@Component
public class OSS 
 public String name;
 public String version;
 public String license;
 public String usage;
 public String comment;

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.din.bd</groupId>
    <artifactId>BduckApp-1</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>BduckApp-1</name>
    <description>Microservice project for Bduck</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

我尝试了在各种 *** 帖子中针对类似错误提到的所有解决方案。所有人都建议,将类移动到与@SpringBootApplication 类相同的文件夹或包含具有basepackage 属性的包。没有一个对我有用。任何帮助都会很棒!

【问题讨论】:

能否检查post是否有类似问题 @soufrk Nope.. 问题是因为 OSS 类混合了 lombok 和 spring 注释 【参考方案1】:

注意:您为 Spring 和 Lombok 配置了 OSS bean。 关于 Lombok,您在类级别声明 @Builder 可能会生成一个构造函数。

问题是从 Spring 4 开始,默认情况下,声明构造函数的 Spring 组件会自动被视为自动装配构造函数。 而您遇到的异常。

查看您实例化 OSS 实例的方式:

oss = OSS.builder().name("Dinesh").version("1.0").license("apache 2.0").usage("dynamic").comment("approved").build();

OSS 不必配置为 Spring bean,但必须是手头创建的普通对象。 你不想创建这个对象的单个实例,你想根据客户端请求实例化它,最后你也不想在客户端之间共享它。 所以只要去掉这个类的@Component注解即可。

【讨论】:

我不得不删除 BduckTrigger.java 中 OSS 对象的 @Autowire 并且它起作用了。谢谢 @Dinesh Ravi 欢迎您。确实,它也是必需的。 如果我只删除 OSS.java 中的 \\@component 并为 oss 实例对象保留 @Autowired,则会引发此错误,->“com.din.BduckTrigger 中的字段 oss 需要一个类型为 ' 的 bean com.din.OSS' 找不到。行动:考虑在您的配置中定义一个类型为 'com.din.OSS' 的 bean。" @Dinesh Ravi 这很有意义:) 如果我想从你提到的“你不想创建这个对象的单个实例,你想根据客户请求实例化它,最后你不也想在客户之间分享”->我的方法应该是什么?

以上是关于com.din.OSS 中构造函数的参数 0 需要找不到类型为“java.lang.String”的 bean的主要内容,如果未能解决你的问题,请参考以下文章

“”中构造函数的参数 0 需要一个“”类型的 bean,但无法找到

Controller 中构造函数的参数 0 需要一个找不到的 Service 类类型的 bean

为啥 C++ 构造函数在继承中需要默认参数?

如何解决此问题:LoginController 中构造函数的参数 0 需要找不到类型为“OktaOAuth2Properties”的 bean

有没有办法 @Autowire 需要构造函数参数的 bean?

在构造函数中默认类的结构字段