記事ディレクトリ
kotlin の Spring Boot プロジェクトを使用して、文字列、リスト、マップなどの読み取りを含む、プロパティ内のコンテンツを読み取ります。
1. プロパティの内容を読みます
1.1 プロパティの文字列を読み取る
設定ファイルからブックの名前と数を読み取ります。
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "test.book")
class BookProperties {
lateinit var name: String
var count by Delegates.notNull<Int>()
}
次に、プロパティで次の内容を構成します。
test.book.name=Kotlin
test.book.count=20
このようにして、プログラムを開始すると、設定された名前の値 Kotlin とカウント値 20 を取得できます。
1.2 プロパティでリストとマップを読み取る
著者の名前が含まれる設定ファイルからリストを読み取り、顧客をマップ形式で読み取り、各人が購入した本の数を示します。
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "test.book")
class BookProperties {
lateinit var authors: List<String>
lateinit var customers: Map<String, Int>
}
プロパティで設定する内容は以下の通りです
test.book.authors=AAA,BBB,CCC
test.book.customers[tom]=23
test.book.customers[jerry]=20
このように、BookProperties Bean を使用すると、リストとマップの値を読み取ることができます。
2. プロパティの内容を静的クラスにコピーする
プロパティの内容を静的クラスにマップするという要件があり、静的クラスは 1 回だけロードされます。(意味が分からないので、先に穴を掘ります)
2.1 Java
Javaでは次のように書くことができます
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Data
@RequiredArgsConstructor
@Configuration
public class YYProperties {
private final BookProperties bookProperties;
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "test.book")
public static class BookProperties {
private String name;
private int count;
private List<String> authors;
private Map<String, Integer> customers;
}
}
プロパティファイルの構成は以下のとおりです。
test.book.name=JAVA
test.book.count=40
test.book.authors=AAA,BBB,CCC
test.book.customers[tom]=12
test.book.customers[jerry]=28
このようにして、yYProperties Bean を使用すると、このメンバー変数 bookProperties の値を次のように読み取ることができます。
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class YYService {
private final YYProperties yyProperties;
public void hello() {
System.out.println(yyProperties.getBookProperties().getAuthors());
System.out.println(yyProperties.getBookProperties().getName());
System.out.println(yyProperties.getBookProperties().getCount());
System.out.println(yyProperties.getBookProperties().getCustomers());
}
}
2.2 コトリン
では、kotlinではこのように書けるのでしょうか?
kotlin には静的修飾子 static がなく、オブジェクトのみがあることは誰もが知っています。オブジェクトクラスがクラスファイルにコンパイルされた後、final が追加されますが、final によって変更されたクラスに @Configuration アノテーションを追加することはできません。
3. 静的変数に値を代入する
場合によっては、プロパティで設定した値を静的変数に代入したい場合があります。
このニーズはいつ発生しますか? たとえば、util クラスで使用する必要があるキーを設定しますが、この util クラスは Bean として登録されておらず、設定されたクラス コンテンツも注入されていないため、静的な変数値が必要です。
3.1 Java
3.1.1 @ConfigurationProperties アノテーションを使用する
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Configuration
@ConfigurationProperties(prefix = "test.book")
public class YYProperties {
public static String name;
public static int count;
public static List<String> authors;
public static Map<String, String> customers;
public void setName(String name) {
YYProperties.name = name;
}
public void setCount(int count) {
YYProperties.count = count;
}
public void setAuthors(List<String> authors) {
YYProperties.authors = authors;
}
public void setCustomers(Map<String, String> customers) {
YYProperties.customers = customers;
}
}
プロパティでこのように設定します
test.book.name=JAVA
test.book.count=40
test.book.authors=AAA,BBB,CCC
test.book.customers[tom]=12
test.book.customers[jerry]=28
このようにして、この書き込まれた値を使用できます
System.out.println(YYProperties.name);
System.out.println(YYProperties.authors);
System.out.println(YYProperties.customers);
System.out.println(YYProperties.count);
印刷結果は以下のようになります。
JAVA
[AAA, BBB, CCC]
{
tom=12, jerry=28}
40
3.1.2 @Value アノテーションの使用
もちろん、@configurationProperties アノテーションを使用せずに @Value アノテーションを直接使用する場合はどうすればよいでしょうか?
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Configuration
//@ConfigurationProperties(prefix = "test.book")
public class YYProperties {
public static String name;
public static int count;
public static List<String> authors;
public static Map<String, String> customers;
@Value("${test.book.name}")
public void setName(String name) {
YYProperties.name = name;
}
@Value("${test.book.count}")
public void setCount(int count) {
YYProperties.count = count;
}
@Value("${test.book.authors}")
public void setAuthors(List<String> authors) {
YYProperties.authors = authors;
}
@Value("#{${test.book.customers}}")
public void setCustomers(Map<String, String> customers) {
YYProperties.customers = customers;
}
}
注目すべきはマップの構成で、メソッド上に @value アノテーションが付いており、同時にそれを利用している
#{
${
test.book.customers}}
また、設定ファイルのプロパティも異なります。
test.book.name=JAVA
test.book.count=40
test.book.authors=AAA,BBB,CCC
test.book.customers={'a': 'aa', 'b':'bb'}
出力結果は、
3.2 コトリン
3.2.1 @ConfigurationProperties アノテーションを使用する
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "redmi.book")
class BookProperties {
fun setName(name: String) {
BookProperties.name = name
}
fun setAuthors(authors: List<String>) {
BookProperties.authors = authors
}
fun setCustomers(customers: Map<String, String>) {
BookProperties.customers = customers
}
companion object {
var name: String = ""
var authors: List<String> = ArrayList()
var customers: Map<String, String> = HashMap()
}
}
プロパティでこのように設定するだけです
redmi.book.customers[aa]=AA
redmi.book.customers[bb]=BB
redmi.book.name=lisi
redmi.book.authors=aaa,bbb,ccc
このようにして、静的変数を他のクラスで直接呼び出すことができます。
println(BookProperties.authors)
println(BookProperties.customers)
出力結果は、
[aaa, bbb, ccc]
{
aa=AA, bb=BB}
3.2.2 @Value アノテーションの使用
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
class BookProperties {
@Value("\${redmi.book.name}")
fun setName(name: String) {
BookProperties.name = name
}
@Value("\${redmi.book.authors}")
fun setAuthors(authors: List<String>) {
BookProperties.authors = authors
}
@Value("#{\${redmi.book.customers}}")
fun setCustomers(customers: Map<String, String>) {
BookProperties.customers = customers
}
companion object {
var name: String = ""
var authors: List<String> = ArrayList()
var customers: Map<String, String> = HashMap()
}
}
プロパティは次のように構成されます
redmi.book.customers={
'a': 'aa', 'b': 'bb'}
redmi.book.name=lisi
redmi.book.authors=aaa,bbb,ccc
このようにして、対応する値も取得できます。