Home > Software engineering >  Issue in storing @Value returned value in another string
Issue in storing @Value returned value in another string

Time:04-20

  @Value("${Loc}"
    private String downloadLoc;// returning the value
    
     DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyyMMdd");
        String date=dtf.format(LocalDate.now());
        String trigFileName="FEED_" date ".trg";
    String updatedLoc=downloadLoc  trigFileName;

//downloadLoc value returned as null when trying store in updatedLoc

CodePudding user response:

we need a properties file to define the values we want to inject with the @Value annotation, for that first we need to define a @PropertySource in our configuration class — with the properties file name

add this to your application.properties file

Loc=[any value you want to specify]

if you want the value to be dynamic try constructor injection where you just need to declare in properties file but not assign

@Component
@PropertySource("classpath:application.properties")
public class LocProvider {

    private String loc;

    @Autowired
    public LocProvider(@Value("${Loc}") String loc) {
        this.loc= loc;
    }

    // standard getter

and create an object in your class like this

LocProvider value = new LocProvider("abc");

CodePudding user response:

Few things to check

  1. Make sure you imported right package (org.springframework.beans.factory.annotation)
  2. Make sure you have an entry in application.properties file. For eg: Loc=SOME VALUE
  3. If you have application.properties file at some other location, specify it in start args (--spring.config.location=<PATH TO THE FILE>). Refer this link for more details
  • Related