Quarkus技术问询:如何在外部或运行时定义读取application.properties?
Great questions—Quarkus has flexible, production-ready tools for handling external and runtime configuration, so let’s break this down step by step.
1. Reading External/Runtime Properties Files (Including application.properties)
Quarkus offers multiple practical ways to load configuration from outside your packaged app or adjust it at runtime:
Specify external config file paths via system property
When starting your app, use thequarkus.config.locationssystem property to point to external files. You can use absolute paths, relative paths, or classpath references. For example:java -jar your-app-runner.jar -Dquarkus.config.locations=file:/opt/config/external-application.properties,classpath:/another-config.propertiesQuarkus will load these files alongside the internal
application.properties, with external values overriding internal ones automatically.Use environment variables
Quarkus maps configuration properties to environment variables seamlessly (uppercase property names, dots replaced with underscores). For example,quarkus.mailer.tobecomesQUARKUS_MAILER_TO. Set this variable at runtime (e.g., in Docker, Kubernetes, or your OS) and Quarkus will pick it up without modifying any files—perfect for containerized deployments.Dynamic configuration refresh
If you need to update properties while the app is running, first enable refresh in your base config:quarkus.config.refresh.enabled=trueAfter modifying your external properties file, send a POST request to the
/q/config/refreshendpoint (enabled by default in dev mode; secure it for production). Any beans using@ConfigPropertywill automatically pick up the new values without restarting the app.
2. Quarkus-Specific External Config for Mail Senders (Including Externalizing application.properties)
Absolutely—Quarkus is built to make this straightforward, even for mail sender use cases:
Fully externalize your
application.properties
You can move your entireapplication.propertiesfile outside your app package and point to it using thequarkus.config.locationssystem property mentioned earlier. For example:java -jar your-mail-app-runner.jar -Dquarkus.config.locations=file:/opt/mail-config/application.propertiesQuarkus will load this external file as the primary config source, so all your mailer settings (recipients, SMTP server, credentials, etc.) can be managed externally without rebuilding the app.
Quarkus-native way to inject external recipient lists
Use MicroProfile Config's@ConfigPropertyannotation to inject recipient values directly into your mail sender code, which pulls automatically from your external config. Here’s a practical example:import io.quarkus.mailer.Mail; import io.quarkus.mailer.Mailer; import jakarta.inject.Inject; import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.List; @Path("/send-email") public class MailSenderResource { @Inject Mailer mailer; // Inject recipients from external config (uses default if not set) @ConfigProperty(name = "app.mail.recipients", defaultValue = "admin@example.com") List<String> emailRecipients; @POST public void sendNotification() { mailer.send(Mail.withText( emailRecipients, "System Notification", "This is an automated message from your Quarkus app." )); } }In your external
application.properties, just define the recipients as a comma-separated list:app.mail.recipients=user1@domain.com,user2@domain.com,team@domain.comProfile-specific external config
For different environments (dev, staging, prod), create profile-specific external files likeapplication-prod.propertiesand activate the profile at runtime:java -jar your-app-runner.jar -Dquarkus.profile=prod -Dquarkus.config.locations=file:/opt/config/Quarkus will automatically load the matching profile file from the specified directory, making environment-specific mail configuration simple.
内容的提问来源于stack exchange,提问作者fatherazrael




