Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import value from properties file and use it in annotation?

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?

like image 385
RK1 Avatar asked Nov 07 '15 19:11

RK1


People also ask

How use value from 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.

Which annotation is used to get values from properties file and specify the location of the properties 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.

How do you inject annotations in Spring properties?

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.

What is the use of @propertysource annotations in spring?

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.

How to use @value annotation in Spring Boot?

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.

How to define multiple property files using @propertysource annotation?

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.

How to get the value of specif property in spring environment?

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.


1 Answers

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.

like image 187
wastl Avatar answered Nov 15 '22 17:11

wastl