How can a record data class be autowired in Spring?

Viewed 1543

I have Spring project with the following record. I need it autowired in my classes, but I can't run the Spring Boot app anymore.

The reason:

Parameter 0 of constructor in sk.eea.jira.rest.Issue 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.

Record class:

@Component
public record Issue(String assignee,
                    String created,
                    String reporter,
                    String resolution,
                    String status,
                    String summary,
                    String updated,
                    String key) {
}

How can I autowire such records in Spring?

1 Answers

Your case has nothing to do with records.

How will spring inject those values in parameters of constructor? Dependency injection can not be done in your case.

You can define a bean in your configurations manualy

@Configuration
public class CustomConfigurations{

   @Bean
   public Issue getIssue(){
    return new Issue("","","","","","","","",);
   }
} 

This will make autowiring work.

But in the end it does not make any sense to autowire a Issue

This is a Pojo object created only for some specific request.

Related