Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. reading from application.conf
- import javax.inject.Inject
- import play.api.Configuration
- import play.api.mvc._
- class TestController @Inject()(cc: ControllerComponents, appConf: Configuration) extends AbstractController(cc) {
- val facultyName: Option[String] = appConf.get[Option[String]]("students.faculty.name") // also getString() is possible
- // add to routes file: GET /testConfig controllers.TestController.testConfig
- def testConfig = Action {
- Ok(s"Result: $facultyName")
- }
- }
- // application.conf:
- students {
- faculty.name = "historical"
- faculty.name = ${?FACULTY_NAME}
- }
- // Output:
- // Result: Some(historical)
- // 2. now set up environment variable: FACULTY_NAME=mathematics
- // Result: Some(mathematics)
- // 3. Bonus! What if we use ${FACULTY_NAME} instead of ${?FACULTY_NAME}?
- students {
- faculty.name = ${FACULTY_NAME}
- }
- // Output with environment variable defined:
- // Result: Some(mathematics)
- // Output without environment variable:
- // Internal server error, sending 500 response
- // Configuration error: Configuration error[application.conf @ file:application.conf: 380:
- // Could not resolve substitution to a value: ${FACULTY_NAME}]
- // at play.api.Configuration$.configError(Configuration.scala:155)
- // at play.api.Configuration$.load(Configuration.scala:101)
- // at play.api.Configuration$.load(Configuration.scala:109)
- // at play.api.Configuration$.load(Configuration.scala:115)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement