Parameter 0 of constructor in com.din.OSS required a bean of type 'java.lang.String' that could not be found
Asked Answered
B

1

2

Error which is shown in console after running the application is pasted below

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.

There are 3 classes under package

com.din

BduckApp1Application.java <- SpringBoot Application

BduckTrigger.java <- RestController

OSS.java <-Data class

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>

I tried all the solution that is mentioned for the similar error in the various stackoverflow post. All suggests, to move the classes to the same folder as @SpringBootApplication class or include the package with the attribute of it basepackage. None worked for me. Any help would be great!

Bander answered 27/6, 2018 at 13:35 Comment(2)
Can you check if the post has a similar problem or notZionism
@Zionism Nope.. the problem is because of lombok and spring annotations mixed for OSS classBander
E
7

Beware : you configured your OSS bean both for Spring and Lombok.
And about Lombok you declared at the class level @Builder that probably has as consequence to generate a constructor.

And the problem is that from Spring 4,by default, a Spring component that declares a constructor is automatically considered as an autowired constructor.
Whereas the exception you encounter.

Seeing the way you instantiated the OSS instance :

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

the OSS doesn't have to be configured a Spring bean but have to be a plain object created at hand.
You don't want to create a single instance of this object, you want instantiate it according to the client request and at last you don't want share it between clients either.
So just remove the @Component annotation of this class.

Eleanore answered 27/6, 2018 at 13:51 Comment(6)
And I had to remove @Autowire for OSS object in BduckTrigger.java and it worked. ThanksBander
@Dinesh Ravi You are welcome. Indeed, it was also required.Eleanore
If I remove only \\@component in OSS.java and keep @Autowired for oss instance object this error is thrown, -> "Field oss in com.din.BduckTrigger required a bean of type 'com.din.OSS' that could not be found.Action: Consider defining a bean of type 'com.din.OSS' in your configuration."Bander
@Dinesh Ravi And it makes sense :)Eleanore
If i want to do all this from what you mentioned "You don't want to create a single instance of this object, you want instantiate it according to the client request and at last you don't want share it between clients either" -> what should be my approach?Bander
You actually did what it was required : not to make the OSS class a bean spring and so of course not inject it but create it only in the scope of the method as you actually did. So it is fine now !Eleanore

© 2022 - 2024 — McMap. All rights reserved.