2

In application.properties:

comment.length=3000

Now I'd like to use this constant:

@Entity(name="clients_client")
public class Client {
    @Column(length="${comment.length}")
    private String comment;
}

When compiling, I get this error:

java: incompatible types: java.lang.String cannot be converted to int

halfer
  • 19,824
  • 17
  • 99
  • 186
Trts
  • 985
  • 1
  • 11
  • 24
  • 2
    Ok, first, your Entity Client is not a Spring Bean, thus you cannot inject property values. Second, if you want to inject Spring Properties into a Spring Bean you should use `@Value` – mrkernelpanic Oct 14 '20 at 07:42
  • similar to this https://stackoverflow.com/questions/33586968/how-to-import-value-from-properties-file-and-use-it-in-annotation – lczapski Oct 14 '20 at 12:55

1 Answers1

0

This is very close to being a duplicate of How to import value from properties file and use it in annotation?, but I think there is a subtle difference between the questions.

You are trying to refer to a property in the @Column annotation by using ${comment.length}. What is really happening is that you try to assign the String "${comment.length}" to the length attribute of the annotation. This is of course not allowed, it expects an int.

Java, or Spring, can not "magically" replace ${propertyName} with a property. Spring, however, has its own way of injecting property values:

@Value("${value.from.file}")
private String valueFromFile;

Even if your entity was a Spring bean (for example annotated with @Component), and you injected the property with @Value, it cannot be used in the annotation. This is because values in annotations need to be constant, and is explained in more detail in the accepted answer to the near duplicate question.

Now I'd like to use this constant:

It simply is not a constant, it is determined at runtime.

Magnilex
  • 11,584
  • 9
  • 62
  • 84