I have a class that is an entity:
Class.java
@Entity
public class Class {
@Id
@GeneratedValue
private Long id;
@NotNull
@Range(min = 0, max = 10)
private double value;
}
I want to get rid of the hard-coded values from the @Range
annotation and load them from a configuration file.
constraints.properties
minVal=0
maxVal=10
This is what I've tried:
@Component
@Entity
@PropertySource("classpath:/constraints.properties")
public class Class {
@Value("${minVal}")
private final long minValue;
@Value("${maxVal}")
private final long maxValue;
@Id
@GeneratedValue
private Long id;
@NotNull
@Range(min = minValue, max = maxValue)
private double value;
}
The error I get is attribute value must be constant
. How the initialization of these fields should be performed to get the result I want?
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.
Spring @PropertySource annotations is mainly used to read from properties file using Spring's Environment interface. This annotation is in practice, placed on @Configuration classes. Spring @Value annotation can be used to specify expression on field or methods.
Injecting Properties Using @Value Using the @Value annotation, we can inject the values from the application. properties file into class fields in the Spring-managed bean GreetController . Using @Value allows you to set a default value if the requested one, for any reason, isn't available: @Value("${message.
Spring @PropertySource annotations is mainly used to read from properties file using Spring’s Environment interface. This annotation is in practice, placed on @Configuration classes.
Spring @Value annotation can be used to specify expression on field or methods. Common use case is to specify the property from a .properties file along with default value. Let’s see complete example below. Other interesting posts you may like. Spring Boot+AngularJS+Spring Data+Hibernate+MySQL CRUD App.
When we define multiple property files using @PropertySource annotation, then order of those files is very important. For instance, take above example. If we define same property (key-value) pair in both default.properties and config.properties files, then config.properties overrides default.properties value.
First point to notice is Environment got auto-wired by Spring. Thanks to @PropertySoruce annotation , this Environment will get access to all the properties declared in specified .properties file. You can get the value of specif property using getProperty method. Several methods are defined in Environment interface.
First: to inject values into a final field you have to use constructor injection see this question
This means that you pass some unknown value into the constructor.
Although the value never changes it is not constant
, since the compiler cannot know this value, because its determined at runtime. And you can only use expressions as values of annotation whose value can be determined at compile time.
Thats because annotations are declared for a class not for a single instance and in your example the values of the variables are potentially diffrent for every instance.
So I would say, that what you want to achieve is not possible.
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