Is it possible to obtain a constant form a java properties file? I am trying to develop my application so that I can set a flag in a master properties file that dictates whether the application uses the development config file or the production one.
Most of application works fine but Ive hit a bit of a brick wall with the database table mapping in the entity classes. I am using Dynamo DB so the actual class itself requires an annotation as follows:
@DynamoDBTable(tableName = "table_name")
public class EmailTemplates {
...
The tableName value must be a constant and I’m trying to use the following to retrieve the value from the properties file and pass it to the tableName;
@DynamoDBTable(tableName = DynamoTable.TABLE_NAME)
public class EmailTemplates {
...
The DynamoTable class:
public final class DynamoTable {
public static final String TABLE_NAME = ResourceBundle.getBundle(ResourceBundle.getBundle("ProjectStage").getString("ProjectStage")).getString("EmailTemplates");
}
Unfortunately its not working as its saying the value is not a constant. If I simply put a literal string (ie. “aStringValue”) then its fine, but not from the properties file.
NB. Just to be clear there isn’t a problem with the above code retrieving the values from the properties file. The problem is that its not being treated as a constant.
You can’t do what you want to do.
You’re asking the javac compiler to execute arbitrary code to read a properties file from disk, extract a value, and insert that as a constant into your code (at compile time).
As you can see, that’s a bit much for the compiler to do.
You can only execute that code at runtime, which doesn’t suite your situation because you’re trying to assign a value to an annotation (and thus the value for the annotation must be available at compile time).