I have a spring boot project and I am trying to set some static constant variables from spring beans. Here are the relevant files
application.properties
app.constants.name=FooTester
app.constants.version=v1
app.constants.port=123
AppConfig.java
@Configuration
public class AppConfig {
@Value("${app.constants.name}")
private String appName;
@Bean
public MethodInvokingBean initAppConstants() {
MethodInvokingBean miBean = new MethodInvokingBean();
miBean.setStaticMethod("app.constants.AppConstants.setAppName");
miBean.setArguments(new String[]{appName});
try {
miBean.prepare();
miBean.invoke();
} catch (Exception e) {
e.printStackTrace();
}
return miBean;
}
public void setAppName(String appName) {
this.appName = appName;
}
}
AppConstants.java
public final class AppConstants {
public static String APP_NAME;
public static String APP_VERSION;
public static String APP_PORT;
private AppConstants(){}
public static void setAppName(Properties p) {
APP_NAME = p.toString();
}
}
This gets the value of name fine but when it gets to the setAppName method, the properties value is a hashmap with the key as FooTester and value as "". If I added in multiple values with the setArguments method like so:
miBean.setArguments(new String[]{"test", "test2", "test3"});
The properties object would only have 1 entry in the hashmap and the key would just be test, test2, test3 and the value as "".
Additionally, I would like the constant values to be final rather than just public static. So I'm not sure that this is the right way to do it. How do I accomplish what I want?