<dependency> <groupId>io.github.yamlesque</groupId> <artifactId>yamlesque-core</artifactId> <version>1.0.0</version> </dependency> yaml # myconfig.yaml database: url: jdbc:mysql://localhost:3306/mydatabase username: root password: password123 poolSize: 10 logging: level: info enabled: true import io.github.yamlesque.core.Yamlesque; import io.github.yamlesque.core.options.FileSource; import io.github.yamlesque.core.options.TypeMapper; public class MyLibraryConfig { private static final String CONFIG_FILE = "myconfig.yaml"; public static void init() { Yamlesque.getInstance() .withFileSource(new FileSource(CONFIG_FILE)) .withTypeMapper(new TypeMapper()) .load(); } public static String getDatabaseUrl() { return Yamlesque.getInstance().getString("database.url"); } public static String getDatabaseUsername() { return Yamlesque.getInstance().getString("database.username"); } public static String getDatabasePassword() { return Yamlesque.getInstance().getString("database.password"); } public static int getDatabasePoolSize() { return Yamlesque.getInstance().getInt("database.poolSize"); } public static String getLoggingLevel() { return Yamlesque.getInstance().getString("logging.level"); } public static boolean isLoggingEnabled() { return Yamlesque.getInstance().getBoolean("logging.enabled"); } } public class MyLibrary { public void connectToDatabase() { String url = MyLibraryConfig.getDatabaseUrl(); String username = MyLibraryConfig.getDatabaseUsername(); String password = MyLibraryConfig.getDatabasePassword(); int poolSize = MyLibraryConfig.getDatabasePoolSize(); // ... } public void logMessage(String message) { if (MyLibraryConfig.isLoggingEnabled()) { String level = MyLibraryConfig.getLoggingLevel(); // ... } } }


上一篇:
下一篇:
切换中文