# 2.9 PropKit configuration reading

The PropKit utility class is used to read external key-value pair configuration files. PropKit can be used anywhere in the system with extreme convenience. The format of the configuration file is as follows:

userName=james
email=no-reply@jfinal.com
devMode=true
1
2
3

Below is a code example of PropKit:

PropKit.use("config.txt");
String userName = PropKit.get("userName");
String email = PropKit.get("email");
 
// Using Prop in combination
Prop p = PropKit.use("config.txt");
Boolean devMode = p.getBoolean("devMode");
1
2
3
4
5
6
7

Below is a specific usage example in a project:

public class AppConfig extends JFinalConfig {
  public void configConstant(Constants me) {
    // The first time you use the 'use' method to load a configuration, it becomes the primary configuration. You can directly retrieve its values using PropKit.get(...)
    PropKit.use("a_little_config.txt");
    me.setDevMode(PropKit.getBoolean("devMode"));
  }
 
  public void configPlugin(Plugins me) {
    // For configurations not loaded for the first time using 'use', you need to specify the configuration file name each time using 'use' before retrieving its values.
    String redisHost = PropKit.use("redis_config.txt").get("host");
    int redisPort = PropKit.use("redis_config.txt").getInt("port");
    RedisPlugin rp = new RedisPlugin("myRedis", redisHost, redisPort);
    me.add(rp);
 
    // For configurations not loaded for the first time using 'use', you can also first obtain a Prop object and then retrieve its values through this object.
    Prop p = PropKit.use("db_config.txt");
    DruidPlugin dp = new DruidPlugin(p.get("jdbcUrl"), p.get("user")…);
    me.add(dp);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

As shown in the code above, PropKit can load multiple configuration files simultaneously. You can directly operate on the first configuration file loaded using the PorpKit.get(...) method. For configuration files not loaded first, you need to use PropKit.use(...).get(...) to operate.

PropKit's use is not limited to within the YourJFinalConfig class; it can be used anywhere in the project. Additionally, after the PropKit.use(...) method loads the configuration file content, it caches the data in memory. You can clear the cached content using PropKit.useless(...).

Last Updated: 9/17/2023, 5:34:57 AM