次のようにspringboot・コンフィギュレーション・ファイルは、$参照変数を使用することができます。
pom.xml
<?xmlのバージョン= "1.0"エンコード= "UTF-8"> <プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0"のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス」 のxsi:のschemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion> 4.0.0 </ modelVersion> <groupIdを> com.edu.spring </ groupIdを> <たartifactId> springboot </たartifactId> <バージョン> 1.0.0 </バージョン> <パッケージ> JAR </パッケージ> <名前> springboot </名前> <! -プロジェクトのウェブサイトにFIXME変更それを- > <URL>のhttp:// www.example.com </ URL> <dependencyManagement> <依存性> <依存> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>スプリングブート依存性</たartifactId> <バージョン> 2.1.6.RELEASE </バージョン> <スコープ> インポート </スコープ> </ POM <タイプ>タイプ> </依存> </依存関係> </ dependencyManagement> <プロパティ> <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> <maven.compiler.source> 1.8 </maven.compiler.source > <maven.compiler.target> 1.8 </maven.compiler.target> </プロパティ> <依存性> <依存> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブート・スターター</たartifactId> </依存関係> </依存関係> </プロジェクト>
UserConfig.java
パッケージcom.edu.spring.springboot。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.beans.factory.annotation.Value; 輸入org.springframework.core.env.Environment; 輸入org.springframework.stereotype.Component; @Component パブリック クラスUserConfigに{ @Autowired プライベート環境環境。 @value( "$ {名前}" ) プライベート文字列名; @value( "$ {app.name}" ) プライベート文字列APP_NAME。 公共 のボイドショー(){ System.out.println( "名= "+ environment.getProperty("名前" )); System.out.println( "名=" + 名)。 System.out.println( "app.name = "+ environment.getProperty(" app.name" )); System.out.println( "app.name =" + APP_NAME)。 } }
App.java
パッケージcom.edu.spring.springboot。 輸入org.springframework.boot.SpringApplication。 輸入org.springframework.boot.autoconfigure.SpringBootApplication。 輸入org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication パブリック クラスアプリ { 公共 静的 ボイドメイン(文字列[]引数) { ConfigurableApplicationContextコンテキスト = SpringApplication.run(APP。クラス、引数)。 context.getBean(UserConfigに。クラス).SHOW(); context.close(); } }
結果は以下の通りであります: