문제

Eclipse 플러그인을 사용하여 GWT 1.7 + GAE 응용 프로그램을 구축했습니다. 시스템 상수는 ... GWT.I18N.CLIENT.CONSTANTS 클래스를 확장하는 Singleton MyConstants 클래스에 의해로드되는 MyConstants.properties 파일에로드됩니다.

MyConstants가 설정을 포함하는 여러 파일 중 하나를로드하고 싶습니다.

  • MyConstants-local.properties
  • MyConstants-Alpha. Properties
  • MyConstants-beta
  • MyConstants-prod.properties

Guice의 열거 단계에 대한 몇 가지 언급을 발견했지만 Gin이 지원하지 않는 것 같습니다. 게다가, 그것은 dev/prod 만 처리하며 확실히 로컬/베타/프로드 솔루션이 필요합니다.

GAE에로드 할 때 작동하는 명령 줄 Arg 또는 다른 인스턴스 정의 된 런타임 매개 변수를 사용하여이를 수행하는 방법이 있습니까?

도움이 되었습니까?

해결책

GAE 서버 측면에서는 DEV 환경을 배포 된 프로덕션 환경과 이러한 코드의 비트로 구별 할 수있었습니다.

하나의 인터페이스와 두 개의 클래스 파일을 만듭니다.

public interface MyConstants {
 public String myConstant(); 
}

public class MyConstantsDev implements MyConstants {
 public String myConstant() { return "xyzzy-dev"; };
}

public class MyConstantsProd implements MyConstants {
 public String myConstant() { return "xyzzy-prod"; };
}

"user.dir"env var. user.dir 경로의 마지막 디렉토리는 고유 한 Google App Engine 응용 프로그램 식별자 또는 루트 프로젝트 개발 디렉토리입니다. 이 사실을 알면 사용할 상수 세트를 결정할 수 있습니다.

public class MyServerModule extends com.google.inject.AbstractModule {

 String appIdentifier = new File( System.getProperty("user.dir") ).getName();
 if ( appIdentifier.equals("trunk") ) {
  // Load MyConstants-dev.properties
  bind( MyConstants.class ).to( MyConstantsDev.class ).in(Singleton.class);
 } else {
  // Load MyConstants-prod.properties
  bind( MyConstants.class ).to( MyConstantsProd.class ).in(Singleton.class);
 }
}

이를 통해 Dev/Prod 상수를 다음과 같은 클래스에 주입 할 수 있습니다.

public class MyDomainClass {

 @Inject
 public MyDomainClass( Logger logger, MyConstants const ) { 
  logger.debug( const.myConstant() ); 
 };
}

다른 팁

특히 제작 된 유형-안전 인터페이스를 사용하여이를 해결해야합니다 ...

if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production)
{
  //It's production.
}
else
{
  //It's Dev - and (SystemProperty.environment.value() == SystemProperty.Environment.Value.Development)
}

개발과 배포 된 환경에 대해 다른 것은 다음과 같은 것입니다. SERVER_SOFTWARE 환경 변수:

if (System.getenv("SERVER_SOFTWARE").startsWith("Dev")) {
  // Load MyConstants-dev.properties
} else {
  // Load MyConstants-prod.properties
}

어쩌면 그에 따라로드 할 Guice 모듈을 선택할 수 있습니다.

gae를 얻는 것에 대한 위의 답변 Server_Software 변하기 쉬운; Server_Software 환경 변수는 CGI 기본값이므로 웹 서버 설정이므로 시스템 환경 VAR을 통해 액세스 할 수 없지만 ServletContext에서 사용할 수 있습니다.

다음은 필요한 가치를 얻을 수있는 코드입니다 (Serversoftware가 아닌 ServerInfo가 왜 ServerInfo가 있는지 알 수 없습니다 ...) :

if (getServletContext().getServerInfo().startsWith("Dev")) {
  // Load MyConstants-dev.properties
} else {
  // Load MyConstants-prod.properties
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top