SpringBOOT에서 JVM 인수를 전달하는 방법
스프링 부트 애플리케이션을 시작할 메인 스프링 부트 클래스에서 JVM 인수를 전달하고 싶습니다.
스프링 부트 애플리케이션에서 JVM 인수를 설정하는 방법을 공유해 주시겠습니까?
아래 옵션을 시도해 봤는데 운이 없습니다.
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
아니면 당신은 이것을 봄 부츠에 사용할 수 있습니다.
bootRun {
systemProperties "property1": "value1", "property2": "value2"
}
-DargumentName을 사용하여 JVM 인수를 추가합니다.
-DargumentName="value1"
그런 다음 스프링 응용 프로그램에서 다음을 수행하여 값을 검색할 수 있습니다.
@Value("${argumentName}")
private String myVariable
이것이 도움이 되길 바랍니다!
Spring Boot은 상당히 정교한 환경 변수 및 구성 속성 관리 기능을 갖추고 있습니다.명령행을 통해 값 "bar"가 있는 foo를 전달하는 간단한 작업을 수행할 수 있습니다.
java -jar app.jar --foo="bar"
Spring Boot 응용 프로그램에서 구성의 외부화 방법에 대해 필요한 모든 것이 아래 링크에 있습니다.
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
업데이트:
Spring Boot 응용 프로그램을 실행하는 경우 항상 아래 패턴을 사용하여 실행해야 합니다.
$ java -jar <jar-name> [--key1="value1"]... [--keyN="valueN"]
그리고 여기 있는 다른 의견들 중 일부에서 제안된 명령줄을 사용하지 않습니다.
추가할 수 있는 JVM 인수app.conf파일:
export JAVA_OPTS='-Dyour_param=1234'
응용 프로그램을 시작할 때 -D 스위치로 시스템 속성을 설정할 수 있습니다.
java -DsomeProperty=123 your.package.name.YourMainClassName
나중에 코드에서 간단히 호출하여 속성 값을 얻을 수 있습니다.
System.getPropetry("someProperty")
다음과 같이 앱을 실행할 수 있습니다.
$ java -server -Dmyproperty=blabla -jar myapp.jar
코드에서 내 재산의 가치에 접근할 수 있습니다.
다음과 같이 앱을 실행할 수 있습니다.
$ java -jar app.jar --someProperty=123
애플리케이션 호출:
import org.springframework.core.env.Environment;
@Autowired
private Environment env;
String someProperty= env.getProperty("someProperty");
@Value 주석을 사용해야 합니다.예를들면
@Value("#{systemProperties.test}")
콩 속성에서 직접 사용할 수 있습니다. 방법의 매개 변수에 사용하는 예가 있습니다.
@SpringBootApplication
public class ReviewsMicroserviceApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(ReviewsMicroserviceApplication.class, args);
System.out.println(ctx.getBean("value"));
}
@Bean
public String value(@Value("#{systemProperties.test}")String value){
return value;
}
}
프로그램을 실행하려면 테스트 속성을 JVM 속성에 추가해야 합니다.
-Dtest="hallo reos"
메이븐 플러그인으로 스프링 부트 앱을 실행할 때 속성을 설정해야 합니다.-Dspring-boot.run.jvmArguemtns=xxx그러나 실제로는 System-Properties를 설정하지 않고 Spring-Properties를 설정합니다(application.xml에 정의됨).
여기서 속성을 사용하여 스프링 부트 앱을 시작하는 명령:
mvn spring-boot:run -D"spring-boot.run.jvmArguments=-Dserver.port=8399 -Dactivemq.url=http://localhost:61616"
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@SpringBootApplication
public class DemoApplication {
@Autowired
MyFantasticClass myFantasticClass;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner commandLineRunner() {
return args -> {
System.out.println(myFantasticClass.getMyFantasticProperty());
};
}
}
@Component
class MyFantasticClass{
@Value("${myFantasticProperty}")
String myFantasticProperty;
String getMyFantasticProperty() {
return myFantasticProperty;
}
}
저는 이 대답을 읽고 있었습니다.application.properties그리고 사용하기 시작했습니다.application.properties환경 관련 변수를 설정합니다.
bootstrap.properties:
slack.channel=test
bootstrap.yml:
slack:
channel: ${slack.channel}
로컬 환경을 항상 UTF-8로 유지하려면 가장 쉬운 방법은 POM 내에서 이를 정의하는 것입니다.
<properties>
<spring-boot.run.jvmArguments>-Dfile.encoding=UTF-8</spring-boot.run.jvmArguments>
</properties>
다음과 같이 내부에 여러 개의 인수를 추가할 수도 있습니다.
<properties>
<spring-boot.run.jvmArguments>-Duser.timezone=UTC -Dfile.encoding=UTF-8</spring-boot.run.jvmArguments>
</properties>
이 기능은 다음 명령을 사용하여 로컬 환경에서 Spring Boot 응용 프로그램을 실행하는 경우에만 작동합니다.
mvn spring-boot:run
명령줄 java를 사용하여 Spring Boot 응용 프로그램을 실행하는 경우 매개 변수를 환경 변수로 저장하거나 다른 답변에서 언급한 것처럼 인수를 사용하여 Java를 실행해야 합니다.
언급URL : https://stackoverflow.com/questions/33635176/how-to-pass-jvm-arguments-in-springboot
'programing' 카테고리의 다른 글
| 현재 날짜에 하루를 추가하려면 어떻게 해야 합니까? (0) | 2023.07.26 |
|---|---|
| JQuery AJAX가 UTF-8을 내 서버로 보내지 않고 IE에서만 전송합니다. (0) | 2023.07.26 |
| sql 쿼리 - 대소문자 구분 함수를 사용합니다. (0) | 2023.07.26 |
| 'mat-form-field'는 알려진 요소가 아닙니다. - Angular 5 & Material 2 (0) | 2023.07.26 |
| jQuery Simple modal을 닫으려면 어떻게 해야 합니까? (0) | 2023.07.26 |
