Reading External Application Configuration
There are many reasons to externalize your API configuration. For example, ajWriteFile requires you to set the file path of destination file. These values should not be hard coded in a generated program. The API would be more robust if these values can be loaded from an external file so that administrators can modify when the API is deployed in different environments.
Setting it up in Excel
Go to the Application Configuration worksheet (%%AppConfig) and define the name of the property you want to add in the Name column.
The value column is where the property value will be imported from an external source. If a value is set here, it becomes the default value.
Modifying configuration values for REST APIs
AlchemyJ REST API can load application configuration from application.yml. Follow the steps below to create and modify the yml file.
If you have not created the application.yml file for your API before, compile the REST API first.
In the output folder, locate \src\main\resources\application.yml. Make a copy of this file and put it in the folder as specified in the application configuration file location in REST API.
Open this application.yml file with a text editor or a yml editor of your choice.
You can find the application properties under alchemyj\applicationProps. Modify their values if needed.
When the REST API is loaded, the values you just changed in application.yml will be loaded into the %%AppConfig worksheet.
AlchemyJ looks for the application.yml file in the following order:
- YML file as specified by Application Configuration File Environment Variable in the REST API worksheet
- YML file as specified by Application Configuration File Location in the REST API worksheet
- If none of the above exists, it will load the values from the application.yml inside the jar file.
Setting configuration values for Java APIs
Java classes do not have an application configuration file as in REST API. Application configuration is usually loaded from your application via yml file, xml file or from a database instead. When initializing an AlchemyJ class in Java, use the following code to load the configuration values.
private Map<String, String> createApplicationProperties() {
Map<String, String> result = new LinkedHashMap<>();
result.put("Property_name_1", "192.168.5.149");
result.put("Property_name_2", "25.0");
result.put("Property_name_3", "sample@gmail.com");
return result;
}
public void init(){
AlchemyjClassContextHolder.getInstance().getClassInitPropertyHolder().setApplicationPropertiesMap(createApplicationProperties());
}