Package com.dianxin.tori.api.bootstrap
Class VirtualEnvironmentBootstrap
java.lang.Object
com.dianxin.tori.api.bootstrap.VirtualEnvironmentBootstrap
Bootstraps the virtual environment configuration for the application.
This class is responsible for loading environment variables from a
.env file and exposing them as JVM system properties
before any logging framework (e.g. Logback) or other components
are initialized.
Why this class exists
- Logging frameworks such as Logback are initialized lazily
at the first call to
LoggerFactory.getLogger(Class). - If environment variables (e.g.
LOG_LEVEL) are not available at that moment, default or fallback values will be used. - This bootstrap guarantees that values defined in
.envare available asSystem.getProperty(String)before Logback is configured.
How it works
A static initializer block loads the .env file using
Dotenv and copies all entries into JVM system properties.
LOG_LEVEL=DEBUG
DISCORD_TOKEN=xxxxxxxx
After this class is loaded, the above values can be accessed via:
System.getProperty("LOG_LEVEL");
Usage
This class must be loaded explicitly and before any SLF4J logger is created.
public static void main(String[] args) {
Class.forName(
"com.dianxin.core.api.lifecycle.bootstrap.VirtualEnvironmentBootstrap"
);
new MyBot().start();
}
Failing to load this class early may result in incorrect logging
configuration (e.g. LOG_LEVEL being ignored).
Notes
- The
.envfile is optional. If it does not exist, this bootstrap will silently do nothing. - System environment variables always take precedence over
values defined in
.env. - This class is intended to be used as a low-level runtime bootstrap and should not be referenced directly elsewhere.
-
Method Summary