Spring BootのPropties
Spring BootのPropties
概要
この文章はSpring BootでProptiesをどう使うかを検討します.Propertiesを使うには、javaコードの注釈とxmlファイルの構成の2つの方法があります.本文は主にjavaコードの注釈に注目します.
注釈を使ってProptiesファイルを登録します.
登録Propertiesファイルは@PropertySourceの注釈を使ってもいいです.この注釈は@Configrationと一緒に使う必要があります.
一番簡単で直接的な使い方は@Valueの注釈を使うことです.
デフォルトではSpring Bootでは、デフォルトの属性ファイルとしてappration.propertiesファイルを読みます.もちろん、コマンドラインで異なる属性ファイルを提供することもできます.
これらの属性を一つのbeanにパッケージしたいという属性があるなら、@ConfigrationProptiesを使うことも考えられます.
yamlファイル
Spring Bootはyaml形式のファイルにも対応しています.yamlは階層的な属性にとってより友好的で便利です.私達はpropertiesファイルとyamlファイルの比較を見ることができます.
Propties環境変数
私たちはこのようにproperty環境変数に入ることができます.
概要
この文章はSpring BootでProptiesをどう使うかを検討します.Propertiesを使うには、javaコードの注釈とxmlファイルの構成の2つの方法があります.本文は主にjavaコードの注釈に注目します.
注釈を使ってProptiesファイルを登録します.
登録Propertiesファイルは@PropertySourceの注釈を使ってもいいです.この注釈は@Configrationと一緒に使う必要があります.
@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
//...
}
属性ファイルを動的に選択するためにプレイステーションを使用することもできます.@PropertySource({
"classpath:persistence-${envTarget:mysql}.properties"
})
@PropertySourceは、複数の属性ファイルを定義するために複数回使用することもできます.@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
//...
}
@ProptySourcesを使って複数@ProptySourceを含むこともできます.@PropertySources({
@PropertySource("classpath:foo.properties"),
@PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
//...
}
属性ファイルを使う一番簡単で直接的な使い方は@Valueの注釈を使うことです.
@Value( "${jdbc.url}" )
private String jdbcUrl;
属性に標準値を追加することもできます.@Value( "${jdbc.url:aDefaultUrl}" )
private String jdbcUrl;
コードに属性値を使うなら、Evironment APIから取得できます.@Autowired
private Environment env;
...
dataSource.setUrl(env.getProperty("jdbc.url"));
Spring BootのプロパティファイルデフォルトではSpring Bootでは、デフォルトの属性ファイルとしてappration.propertiesファイルを読みます.もちろん、コマンドラインで異なる属性ファイルを提供することもできます.
java -jar app.jar --spring.config.location=classpath:/another-location.properties
テスト環境であれば、@TestPropertySourceを使ってテストのプロパティファイルを指定できます.@RunWith(SpringRunner.class)
@TestPropertySource("/foo.properties")
public class FilePropertyInjectionUnitTest {
@Value("${foo}")
private String foo;
@Test
public void whenFilePropertyProvided_thenProperlyInjected() {
assertThat(foo).isEqualTo("bar");
}
}
属性ファイル以外に、直接key=valueという形でもいいです.@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"foo=bar"})
public class PropertyInjectionUnitTest {
@Value("${foo}")
private String foo;
@Test
public void whenPropertyProvided_thenProperlyInjected() {
assertThat(foo).isEqualTo("bar");
}
}
@Spring BootTestを使って、私達も同様の機能を使うことができます.@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class)
public class SpringBootPropertyInjectionIntegrationTest {
@Value("${foo}")
private String foo;
@Test
public void whenSpringBootPropertyProvided_thenProperlyInjected() {
assertThat(foo).isEqualTo("bar");
}
}
@ConfigrationProptiesこれらの属性を一つのbeanにパッケージしたいという属性があるなら、@ConfigrationProptiesを使うことも考えられます.
@ConfigurationProperties(prefix = "database")
public class Database {
String url;
String username;
String password;
// standard getters and setters
}
属性ファイルは以下の通りです.database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar
Spring Bootはこれらの属性ファイルを自動的にjava beanの属性にマッピングします.prefixを定義することが必要です.yamlファイル
Spring Bootはyaml形式のファイルにも対応しています.yamlは階層的な属性にとってより友好的で便利です.私達はpropertiesファイルとyamlファイルの比較を見ることができます.
database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar
secret: foo
database:
url: jdbc:postgresql:/localhost:5432/instance
username: foo
password: bar
secret: foo
yamlファイルは@ProptySourceでは使えません.@ProptySourceを使うなら、propertiesファイルを指定しなければなりません.Propties環境変数
私たちはこのようにproperty環境変数に入ることができます.
java -jar app.jar --property="value"
~~shell java-Dproperty.name="value"-jar ap.jar
:
export name=valuejava-jar ap.jar
? ,Spring Boot application-environment.properties ,Spring Boot , 。
## java
,java PropertySourcesPlaceholderConfigurer :
@Benpublic static PropertySources Placeholder Configrer properties(){PropertySourcesPlaceholderConfigurer pspc
= new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[ ]
{ new ClassPathResource( "foo.properties" ) };
pspc.setLocations( resources );
pspc.setIgnoreUnresolvablePlaceholders( true );
return pspc;
)
:[https://github.com/ddean2009/learn-springboot2/tree/master/springboot-properties](https://github.com/ddean2009/learn-springboot2/tree/master/springboot-properties)