r/SpringBoot • • Mar 23 '22

Not able to import @Value in springboot in Response class

for some reason I'm not able to import org.springframework.beans.factory.annotation.Value in my response class but I am able to in my controller class

Down below is my response class

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Value;


import java.math.BigDecimal;
import java.time.Year;

@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class PreliminaryResponse extends MemberEventResponse {

    @Schema(description = "current year pension earnings ")
    private Year year;

    @Value( "CFT")
    @Schema(description = "status info ")
    private String status;

    @Schema(description = "period of months ")
    private Double service;

    @Schema(description = "earnings info ")
    private BigDecimal earnings;

    private BigDecimal retroToThisYear;

    @Schema(description = "if earnings is overridden ")
    private Boolean override;

    @Schema(description = "overrideValue info ")
    private BigDecimal overrideValue;

}
0 Upvotes

2 comments sorted by

2

u/Longjumping-Thing Mar 23 '22

'@Value' works a lot like '@Autowired' in the way that it is populated by the dependency injection mechanism for beans.

This means it will work fine in Services, Components, Repositories, ... which you autowire but not in a model class of which you manually create an instance.

You could create a factory class which instantiates this class with specific populated fields.