플레이 플레이! 2.0 application.conf의 구성 변수?
이전에 Play! v1에서는 구성 변수를 정의 application.conf
하고 다음과 같이 액세스하는 것이 정말 쉬웠습니다 .
play.configuration("db.driver")
그러나 지금은 v2에서 비슷한 용도로 사용하거나 적절한 대안을 문서에서 찾을 수 없습니다. 그렇게하는 방법은 무엇입니까?
Play 2.5 play.api.Play.current
부터는 더 이상 사용되지 않습니다. 의존성 주입을 사용하여 Environment
or 를 주입하고 Configuration
이를 사용하여 구성 값을 읽어야합니다.
class HomeController @Inject() (configuration: play.api.Configuration) extends Controller {
def config = Action {
Ok(configuration.underlying.getString("db.driver"))
}
}
자세한 내용 은 Play 문서 를 확인 하세요 .
이에 상응하는 Play 2.0 Scala는 다음과 같습니다.
Play.current.configuration.getString("db.driver")
당신은 또한 필요합니다 import play.api.Play
이에 대한 전체 문서는 여기에 있습니다 .
Play 2.0에 적용-Java Controller에서 다음을 사용할 수 있습니다.
String optionValue = Play.application().configuration().getString("db.driver");
보기에서 변수를 얻으려면 다음을 사용하십시오.
@play.Play.application().configuration().getString("db.driver")
Java 용 Play 2.3.2 에서는 다음 com.typesafe.config.ConfigFactory
옵션을 사용할 수 있습니다 .
Config conf = ConfigFactory.load();
String myFooBarConfiguration = conf.getString("foo.bar");
빠르게 움직이는 API!
Play 2.3 [.8] / Java 에서 테스트 된 또 다른 방법 은 application.conf의 값에 액세스합니다.
Play 버전을 확인하려면 프로젝트 / 플러그인 파일을 확인하십시오. "sbt-plugin"이 포함 된 줄에는 "2.3.8"과 같은 버전 사양이 있어야합니다.
예를 들어, application.conf에
myConfigStringValue=abc
myConfigBooleanValue=true
하나는 자바 파일 / 클래스에서 그 값을 쿼리 할 수 있습니다.
import play.Configuration;
...
String myString = Configuration.root().getString("myConfigStringValue");
Boolean myBoolean = Configuration.root().getBoolean("myConfigBooleanValue");
get ... 메소드는 값을 찾을 수없는 경우 null을 반환하고, 인수로 기본값을 사용하는 get ... 메소드도 있습니다.
자세한 내용은 https://www.playframework.com/documentation/2.3.x/api/java/index.html을 참조 하십시오.
클래스 play.Configuration을 검사합니다.
Play Scala 2.3.x 및 2.4.x에서에서 값을 읽으 conf/application.conf
려면 다음을 수행 할 수 있습니다.
import play.api.Play.current
...
current.configuration.getString("db.driver")
Play 2.0.1 Java에서는 다음을 수행해야합니다.
import play.Application.*;
...
String optionValue = play.Play.application().configuration().getString("my.config");
Play 2.1, Scala에서는 먼저 import play.api.Play Play.current.configuration.getString("varibale name")
Play Scala를 사용하는 경우 몇 가지 모범 사례를 검색 한 후이 방법이 가장 적합하다는 것을 알았습니다. 이를 위해 구성을 삽입 한 다음 다음과 같이 구성 키에 액세스했습니다.
import play.api.Configuration
class myClass @Inject()(
config: Configuration
) {
val configValue: String = config.underlying.getString("configKey")
}
이렇게하면 옵션이 아니라 문자열이 표시됩니다. 사용할 수없는 경우 예외가 발생합니다.
Error injecting constructor, com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'configKey'
주요 목표는 @peoplemerge가 이미 언급 한 순수한 get
솔루션 을 피하는 것이 었고 None 인 경우 특정 예외를 던지는 것입니다.
Play> 2.5.X를 사용하는 Java에서는 ConfigFactory 도우미를 통해 구성 값을 읽을 수 있습니다.
ConfigFactory.load().getString("redis.url")
또는
ConfigFactory.load().getInt("redis.port")
The object Config will convert the param into the correct type. It exposes the methods to handle any java type (getDouble, getLong, etc, etc)
Doc: https://www.playframework.com/documentation/2.5.0/api/java/play/Configuration.html
As a small contribution/improvement to all the @Inject
answers here, you don't need to call the config.underlying
implementation. You can directly use config.getString
example:
@Singleton
class RESTSessionChecker @Inject()(
implicit override val conf: Configuration)
extends Filter {
val MAX_CONCURRENT_REQUESTS = conf.getString("MAX_CONCURRENT_REQUESTS").
getOrElse("100").toInt
...
As others have mentioned, you need to import play.api.Play.current. Then if you run:
current.configuration.getString("db.driver")
On 2.3.x / scala 10 you'll get
type mismatch;
found : Option[String]
required: String
If this is mandatory, this will work:
url = current.configuration.getString("db.driver").get
Anyone suggest a better answer?
// new approach post 2.5.x
import javax.inject.Inject
import play.api.Configuration
class Example @Inject() (playconfiguration: Configuration) {
def index() = {
val confString: String = playconfiguration.getString("confKey").get
}
}
source : https://www.webkj.com/play-framework/play-scala-2.5-reading-config-using-di
'development' 카테고리의 다른 글
Django에서 로그 아웃 한 후 리디렉션 할 위치를 구성하는 방법은 무엇입니까? (0) | 2020.10.06 |
---|---|
데이터 프레임을 행 및 열 방향으로 무작위 화 (또는 영구화)하는 방법은 무엇입니까? (0) | 2020.10.06 |
cURL을 사용하여 jSON 데이터를 얻고 데이터를 디코딩하는 방법은 무엇입니까? (0) | 2020.10.06 |
반응에서 아래로 스크롤하는 방법은 무엇입니까? (0) | 2020.10.06 |
Null이있는 SQL Server 문자열 연결 (0) | 2020.10.06 |