Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign annotation value from application.properties in Spring Boot?

I have an entity/model with discriminator formula and now I'm assigning a discriminator formula like the following sample,

@Entity
@DiscriminatorFormula("type")
class Student {
    // code
}

In the same case, how can I assign discriminator value from application.properties in Spring Boot?

like image 700
jeeva Avatar asked Dec 04 '17 10:12

jeeva


People also ask

How do you set values in application properties?

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.

How do you inject annotations in spring properties?

Spring get value from system property. You can set the system property on the Java command line using the -Dpropertyname=value syntax or at runtime using System. setProperty function. To inject the value from the system property, annotate your field using the @Value annotation with SpEL expression as below.

What is @value annotation in spring boot?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.


1 Answers

The expression in the class level annotation should be a constant, i.e., final and static. What you are trying to achieve is not possible. The best you can do is read it from a constant file rather than application.properties.

like image 121
SakshamB Avatar answered Sep 21 '22 13:09

SakshamB