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

32

87

I am working on spring batch with spring boot 2.X application, actually its existing code i am checked out from git. While running the application it fails due to below error only for me and same code is working for others.

s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inputItemReader' defined in file [C:\Users\XYZ\git\main\batch\CBatchProcessing\target\classes\com\main\batchprocessing\batch\reader\InputItemReader.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**: {}


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-16 23:23:37.411 ERROR 2384 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

**Parameter 0 of constructor in com.main.batchprocessing.batch.reader.InputItemReader 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.

I have checked below

  1. All Spring components are correctly annotated with @Component, @Service, @Controller,@Repository, etc...
  2. @ComponentScan & @EnableAutoCOnfiguration is also provided.
  3. Tried giving "java.lang.String" in declarations.

Code:

    import java.util.Map;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.batch.core.ExitStatus;
    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.StepExecutionListener;
    import org.springframework.batch.item.file.FlatFileItemReader;
    import org.springframework.batch.item.file.mapping.JsonLineMapper;
    import 
    org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.stereotype.Component;

    @Component
    public class InputItemReader extends  FlatFileItemReader<Map<String, 
     Object>> implements StepExecutionListener {

    @Autowired
    private InputFileHeaderValidator inputFileHeaderValidator; 

    @Autowired
    private FileAuditService fileAuditService;

    private final Logger log = 
    LoggerFactory.getLogger(InputItemReader.class);

    private java.lang.String inputFilePath;

    public InputItemReader(String inputFilePath) {
        setLineMapper(new JsonLineMapper());
        setRecordSeparatorPolicy(new JsonRecordSeparatorPolicy());
        setResource(new FileSystemResource(inputFilePath));
        this.inputFilePath = inputFilePath;
    }
   }
Royo answered 16/10, 2018 at 18:13 Comment(7)
can you add the code for InputItemReader ?Gallardo
This may help: #51064714Maddy
there are some articles with similar error messages: #44058710Threephase
With Lombok we can use @NoArgsConstructor annotation.Kempis
This might be so funny, but make sure your "spring stereotypes" imports are spring imports and not from other packages! Happy coding..Gradey
In my case, the problem was the package structure. The ComponentScan did not find the other class.Vulture
I had this issue and sorted it out by adding @NoArgsConstructor ( I'm using lombok). Without lombok, please create a constructor with no params aka a default constructor: RouteService(){}Arietta
C
58

Since you do not provide the public default constructor and you added your own non-default constructor the instantiation will fail. I would suggest you to define the input file path as property like @Value("${inputFilePath}"). If you need further initialization in your bean define a void method and annotate it with @PostConstruct and do the initialization within.

Chicane answered 16/10, 2018 at 19:56 Comment(3)
Yeah, this is the answer .. In application.properties the inputFilePath variable was missing.. @v_schoener thanks ..Royo
By the way from Spring Boot 2.2.x immutable configuration will be supported, and hence support for constructor injection of the properties. github.com/spring-projects/spring-boot/commit/…, github.com/spring-projects/spring-boot/wiki/…, github.com/spring-projects/spring-boot/wiki/…Sandrasandro
Thanks you, just solve the issue by providing a default constructor.Hibbert
K
58

Add a public default constructor in your class. For example.

public User() {
}
Kempis answered 27/4, 2019 at 7:13 Comment(2)
Thank you for sharing it. This helped me solve the issue in my project. I am a bit confused. Many of my service classes do not have a public constructor even then I am able to create bean with the custom constructor. But I started getting an issue with one service class where it got solved by providing a default constructor. I am creating bean from config class where parameters value is fetched from properties file.Palpate
If you're using Lombok you can add annotation(@NoArgsConstructor) on top of the class .Kempis
B
22

Make sure you are using spring-boot-starter-data-jpa

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Brokaw answered 26/1, 2020 at 17:39 Comment(1)
Its always good to provide as much context as possible. For example here you are not specifying a version in the maven dependency. You could expand on the spring-boot bom for example and state a version could be required if the boms is not used. This is just an example. Its always good to try and provide at leat some context if possible. See here stackoverflow.com/help/how-to-answer specifically under the subheading "Answer the question"Schnozzle
D
18

For me, it was because of using @AllArgsConstructor annotation of the lombok. My code was like this:

@Service
@AllArgsConstructor
public class SampleService {

    @Value("${search.page.size}")
    private Integer pageSize;

    private final SampleRepository sampleRepository;

And then, I removed @AllArgsConstructor and added @RequiredArgsConstructor annotation. The problem was solved.

@Service
@RequiredArgsConstructor
public class SampleService {

    @Value("${search.page.size}")
    private Integer pageSize;

    private final BatchRepository batchRepository;
Detwiler answered 14/11, 2021 at 15:2 Comment(1)
This is just an alternative approach. Instead of removing the (@AllArgsConstructor), you could also add (@NoArgsConstructor) to solve the issueCosmetician
M
12

You defined something like this:

@Component
public class InputItemReader{

   public InputItemReader(String input){
     ...
   }
}

The name of your class suggest that your object is not a bean, just a simple object. You should try to use it in classic way:

new InputItemReader(myString);

or to have a static method to process the input String.

Explanation: Spring IoC container will try to instantiate a new InputItemReader object like this :

new InputItemReader( -- WHAT TO PUT HERE? --) 

and will fail to call your constructor, because it will not know what you do actually expect and input string.

UPDATE: Your problem can be solved by removing @Component annotation and defining the bean in a configuration like this:

@Bean
public InputItemReader inputItemReader(InputFileHeaderValidator inputFileHeaderValidator, FileAuditService fileAuditService){
    InputItemReader inputItemReader = new InputItemReader("--HERE SHOULD BE ACTUAL PATH---");
    // set the required service, a cleaner approach would be to send them via constructor
    inputItemReader.setFilteAuditService(fileAuditService);
    inputItemReader.setInputFileHeaderValidator(inputFileHeaderValidator);
    return inputItemReader;
}
Martine answered 16/10, 2018 at 18:24 Comment(2)
I understood your point, but the same code is working for someone else not working for me.. i really did not understand why is it so ? I have added code also could you please check it onceRoyo
How should you receive inputFilePath? You know this value before runtime?Martine
G
5

I was also having the same problem, for me following solution worked perfectly fine:

I had annotated my class as @AllArgsConstructor by importing import lombok.AllArgsConstructor

I had just removed this annotation and the code starts working.

Goldoni answered 26/10, 2020 at 14:31 Comment(0)
Z
5

I had the same error but the error was generated by Feign Client. If you have this error using feign client you must add @EnableFeignClients on your main class:

@SpringCloudApplication
@EnableFeignClients
public class Application {
...
}
Zoroastrianism answered 21/12, 2020 at 15:2 Comment(0)
C
4

Even after following the above solutions, if the problem still exists then please check your import statements.

In my case it was the wrong import of @service annotation.

Canescent answered 4/6, 2021 at 9:34 Comment(0)
O
3

I also had the same error:

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

Description:

Field repository in com.example.controller.CampaignController required a bean of type 'com.example.data.CustomerRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.data.CustomerRepository' in your configuration.de here

I solved this issue by adding @EnableMongoRepositories annotation in the main class:

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = CustomerRepository.class)
public class CampaignAPI {

    public static void main(String[] args) {
        SpringApplication.run(CampaignAPI.class, args);
    }
}
Orthopedic answered 21/1, 2020 at 16:14 Comment(1)
good catch! this solved my issue. However, is it better to add all the repositories, like so? basePackageClasses = Foo1Repository.class, Foo2Repository.class... ?Ledeen
B
3

In my case I missed the @Service annotation so once I placed it works.

Blameful answered 23/2, 2022 at 12:46 Comment(0)
H
2

The issue in my case was a redundant @Autowired, I initially added a dependency using @Autowired, by eventually commenting it out, however, I forgot to comment on the annotation, due to which the method next to @Autowired was considered as some sort of setter.

On removing the redundant annotation it is working fine.

Hairbreadth answered 6/8, 2020 at 14:25 Comment(0)
M
2

Using @Component worked for me

Moonfaced answered 22/12, 2023 at 11:40 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Buffington
C
0

In my case, the issue was way different.

@SpringBootApplication
@EnableNeo4jRepositories("com.digital.api.repositories") // <-- This was non-existent.
public class Application {

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

Look at the @EnableNeo4jRepositories annotation. The package defined, was non-existent. Try defining that package, and be careful that the Repository interfaces are based there. Otherwise, Spring will not find the repositories classes that it should load up!

Conway answered 23/3, 2020 at 21:32 Comment(0)
B
0

I was facing the same issue and it got fixed by removing Constructors from my Model class. Adding sample snippet below:

Map<String, ServiceDefinition> serviceDefinitionMapper = new HashMap<>();
    A def;
    B serviceCharacter;

    @Autowired
    public Scan(Map<String, ServiceDefinition> serviceDefinitionMapper, A def,
            B serviceCharacter) {
        super();
        this.serviceDefinitionMapper = serviceDefinitionMapper;
        this.def = def;
        this.serviceCharacter = serviceCharacter;
    }

Please Note: Do not keep any Constructor/@AllArgsConstructor in your Model class unless it is very much needed to be declared.

Behold answered 26/11, 2020 at 10:47 Comment(0)
A
0

In my case annotating fields with lombok @NonNull was causing the trouble.

Africander answered 2/7, 2021 at 8:12 Comment(0)
K
0

import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;

@Service
    @RequiredArgsConstructor
    @Transactional
    @Slf4j
    public class UserServiceImp implements UserService, UserDetailsService {
    ....
    }
Krenek answered 31/8, 2021 at 12:10 Comment(0)
O
0

I faced the same issue but with my own created interface repository that could not be found. The answer for me was that Spring 2.6.3 has bugs with Java 8. After I switched to the Java 11 version everything goes correctly.

More info about this kind of problems I found here https://github.com/spring-projects/spring-boot/issues/6987

Orpine answered 17/2, 2022 at 13:52 Comment(0)
D
0

In my case I had this problem but it was because of I added an annotation of @Component in my Exception code which I shouldn't because we don't want it to be scanned really we're just using it to handle an exception in our service or I don't know try to delete the annotation in your built-in exception.

Worked on my machine.

Deteriorate answered 15/3, 2022 at 18:6 Comment(0)
C
0

I had this problem while testing with JUnit. It was that I forgot to create @MockBean annotation, that's why the bean was missing.

Cassey answered 16/4, 2022 at 18:24 Comment(3)
Hello Liubomyr and welcome to StackOverflow. Future users may find helpful to see a bit more context. Could you please add some example code in that regard?Terrorstricken
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Buffington
The question was not about testing or any JUnit-case, so mocking and a @MockBean annotation might help in this context. I doubt that this answer is helpful.Nickens
B
0

Not related to the above issue but if you have the same error of bean creation in a spring boot application and you are getting the same above error please check whether you have added the class path in @ComponentScan( of the main spring booth application launching class.

Brassware answered 4/7, 2022 at 17:15 Comment(0)
R
0

For it to work I had to add this @RequiredArgsConstructor annotation to my class, and then added this @Value("${inputFilePath}") annotation to access the variable. See below:

    @Component
    @RequiredArgsConstructor
    public class InputItemReader extends  FlatFileItemReader<Map<String, 
     Object>> implements StepExecutionListener {

        @Value("${inputFilePath}")
        private inputFilePath;


        //...

    }
Redoubtable answered 20/11, 2022 at 11:5 Comment(0)
C
0

In my case, removing the @EnableJpaRepositories annotation from my config fixed the issue. The repositories are still wired correctly.

Before:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
@EnableJpaAuditing
public class PersistenceConfig {

After:

@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
public class PersistenceConfig {
Ceria answered 6/2, 2023 at 17:27 Comment(0)
T
0

Adding to the "services" block of my application-default.yml & application-kubernetes.yml fixed the issue for me.

Tainataint answered 2/3, 2023 at 22:36 Comment(0)
B
0

Check if the application.properties file is written correctly.

For some reason my application was spring.profiles.on=test in the application.properties file.

When i change it to the proper value of spring.profiles.active=test it worked.

Busyness answered 11/4, 2023 at 12:58 Comment(0)
K
0

Generate Constructor from Superclass. It worked for me.

e.g.

public UserModel() {
        super();
        // TODO Auto-generated constructor stub
    }
Kriskrischer answered 3/5, 2023 at 13:32 Comment(0)
E
0

If you want to add beans to your context that are defined in some external library then use @Import annotation and pass the class file name/bean name. Add @Import to your main configuration file having @SpringBootApplication annotation

Ref : https://www.baeldung.com/spring-import-annotation

Elielia answered 14/5, 2023 at 13:35 Comment(0)
B
0

Use:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

instead of:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-data-mongodb</artifactId>
</dependency>
Bully answered 28/5, 2023 at 15:14 Comment(0)
D
0

This issue will also occurs when by mistake method is @Autowired in place of @Override but this is a rare case.

General Case:

  1. Missing Bean Definition (Service or any component should be anotated properly)
  2. Component Scan Make sure that the package containing your class is included in Spring's component scan.
  3. Bean Configuration: Verify that you have a bean definition (for Instance you are trying to autowire) in your Spring configuration.
  4. @Autowired Annotation: Ensure that the @Autowired annotation is correctly used.
Demotic answered 14/9, 2023 at 12:12 Comment(0)
G
0

This issue occurred when using spring boot 3.

I used below dependency for sql connecto. Replaced javax with Jakarta for @Table and @Entity. This resolved my issue.

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.1.0</version>
</dependency>
Guimpe answered 29/9, 2023 at 19:44 Comment(0)
P
0

In my case, I had to use RequiredArgsContructor annotation and removed final keyword for the variable blobContainerName. In the end, it looks like this.

@Service
@RequiredArgsConstructor
public class AzureStorageServiceImpl implements StorageService {

    @Value("${spring.cloud.azure.storage.blob.container-name}")
    private String blobContainerName;
Pochard answered 9/10, 2023 at 12:23 Comment(0)
B
0

I had a similar issue in a different scenario. I had everything perfectly working until I found that the main Application method has several annotations along with @EnableAutoConfiguration.

Also, make sure the services and their classes are @Overriding and @Autowired as needed.

I removed all the unwanted annotations, and it worked like a charm!!

From:

@EntityScan(basePackages = {"com.some.*"})  //removed
@EnableJpaRepositories(basePackages = {"com.some.*"}) //removed
@EnableAutoConfiguration
@EnableAsync
@ComponentScan(basePackages = {"com.some.*"}) //removed

to:

@SpringBootApplication //this will ask Spring to do scanning, if your package structure is good, you can just use this

@Controller
MyController {

//has Autowired dependency
@Autowired
private MyService myservice;

//using myService.someMethod(String parameter);

}

Removed the

[12Dec2023 14:40:32.65]WARN

o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - 
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'myService': 
Error creating bean with name 'myServiceImpl': 
Unsatisfied dependency expressed through method 'someMethod' parameter 0: 
No qualifying bean of type 'java.lang.String' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Description:

Parameter 0 of method someMethod in com.service.MyServiceImpl required a bean of type 'java.lang.String' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Bridgehead answered 12/12, 2023 at 21:39 Comment(0)
M
0

In my case, I just needed to add lombok.config file in the root of my project with the following info:

config.stopBubbling=true
lombok.addLombokGeneratedAnnotation=true
lombok.copyableAnnotations+=org.springframework.beans.factory.annotation.Value
Myeloid answered 17/5 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.