Read Configuration File
Updated at 1685686682000Read Configuration File
Configuration file is so importance in every project, it helps us change project's configuration easily. You can use it to configure database connection, memory connection or anything else you don't want to hardcode. EzyFox will help you read .properties file or yaml to Properties object or POJO and it also supports you read by profile (alpha. beta or release). You can read full example here
Add dependency
<dependency> <groupId>com.tvd12</groupId> <artifactId>properties-file</artifactId> <version>${propertiesFileVersion}</version> </dependency>
Read .properties file
Let's say we have 3 files like this:
application.properties
include.profiles=cache,database hello=world
application-cache.properties
cache.name=foo
application-database.properties
database.name=hello-world
Because application.properties
contains include.profiles=cache,database
so, to read 3 files we just need read application.profiles
like this:
Properties properties = new MultiFileReader() .read("application.properties"); System.out.println("properties: " + properties);
And you will get:
properties: {database.name=hello-world, hello=world, cache.name=foo}
Read .properties by profile
Let's say we have alpha
phase, you just need create files with suffix -alpha
(similar to another phases), we will have 3 more files:
application-alpha.properties
include.profiles=cache-alpha,database-alpha
application-cache-alpha.properties
cache.host=alpha-cache.host
application-database-alpha.properties
database.host=alpha-db.host
Because application-alpha.properties
contains include.profiles=cache-alpha,database-alpha
so, to read 3 files we just need read application-alpha.profiles
like this: EzyFox will combine all properties of normal and alphase phase to one Properties
object and we have:
properties alpha: {cache.host=alpha-cache.host, database.name=hello-world, hello=world, database.host=alpha-db.host, cache.name=foo}
To read the configuration to POJO
you just use like this:
ApplicationConfig applicationConfig = new PropertiesMapper() .reader(new MultiFileReader("alpha")) .file("application.properties") .map(ApplicationConfig.class); System.out.println("applicationConfig alpha: " + applicationConfig);
And you will get:
applicationConfig alpha: ApplicationConfig(hello=world, database=ApplicationConfig.Database(name=hello-world, host=alpha-db.host), cache=ApplicationConfig.Cache(name=foo, host=alpha-cache.host))
Read yaml file
Read yaml
files is same to .properties
, you just need input application.yaml
file and the alpha
phase and everything done. Let's we have 6 yaml
files like this:
application.yaml
include: profiles: - cach - database hello: world
application-cache.yaml
cache: name: foo
application-database.yaml
database: name: hello-world
application-alpha.yaml
include: profiles: - cache-alpha - database-alpha
application-cache-alpha.yaml
cache: host: alpha-cache.host
application-database-alpha.yaml
database: host: alpha-db.host
To read alpha
phase to Properties
object, we just need use:
Properties yamlPropertiesAlpha = new MultiFileReader("alpha") .read("application.yaml"); System.out.println("yaml properties alpha: " + yamlPropertiesAlpha);
And you will get the same result with .properties
yaml properties alpha: {cache.host=alpha-cache.host, database.name=hello-world, hello=world, database.host=alpha-db.host}
To read alpha
phase to POJO
, we just need use:
ApplicationConfig applicationConfigYaml = new PropertiesMapper() .reader(new MultiFileReader("alpha")) .file("application.yaml") .map(ApplicationConfig.class); System.out.println("applicationConfigYaml alpha: " + applicationConfigYaml);
A you will get the result with .properties
too:
applicationConfigYaml alpha: ApplicationConfig(hello=world, database=ApplicationConfig.Database(name=hello-world, host=alpha-db.host), cache=ApplicationConfig.Cache(name=null, host=alpha-cache.host))
You can read full example here
Next step
You can see how to Encrypt and Decrypt properties value.