Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from application.properties: Attribute value must be constant

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);
    }
}
like image 412
HackerDuck Avatar asked Aug 18 '16 08:08

HackerDuck


People also ask

How read data from application properties file in spring?

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.

How do I fetch values from application properties in spring boot?

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.

What is the use of application properties in spring boot?

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.

How do I view properties file in spring boot?

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.


1 Answers

Change to:

@Scheduled(fixedRateString = "${update.rate}")
public void update()
{
    System.out.println(this.message);
}
like image 66
Jens Avatar answered Oct 14 '22 12:10

Jens