In the below-given code I get the compilation error Attribute value must be constant
. How to deal with it?
@Component
public class ScheduledTasks {
@Value("${example}")
String message;
@Value("${update.rate}")
final static long rate;
@Scheduled(fixedRate = this.rate)
public void update()
{
System.out.println(this.message);
}
}
Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.
You can use @Value("${property-name}") from the application. properties if your class is annotated with @Configuration or @Component . You can make use of static method to get the value of the key passed as the parameter. Save this answer.
So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.
Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.
Change to:
@Scheduled(fixedRateString = "${update.rate}")
public void update()
{
System.out.println(this.message);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With