简体   繁体   中英

Int from application.properties

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM