今更ながら、Spring Boot の DI が超強力であることに気づいた。例えば、以下の AppProperties
package com.minosys.sample_app;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
@ConfigurationProperties("app")
@Data
public class AppProperties {
private List<List<String>> apiControlList;
}
では List of List の property を設定しているが、application-default.yaml に
app:
api-control-list:
- ["GROUP1", 1]
- ["GROUP2", 0]
と書くことで代入可能。実際、
package com.minosys.sample_app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@ConfigurationPropertiesScan
public class SampleAppApplication {
@Autowired
AppProperties prop;
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(SampleAppApplication.class, args);
SampleAppApplication app = ctx.getBean(SampleAppApplication.class);
app.start();
}
public void start() {
System.out.println("<<< Sample App >>>");
System.out.printf("app.api-control-list: %s", prop.getApiControlList().toString());
}
}
とすると、標準出力に
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.0)
2024-06-07T04:39:15.842+09:00 INFO 8756 --- [sample-app] [ main] c.m.sample_app.SampleAppApplication : Starting SampleAppApplication v0.0.1-SNAPSHOT using Java 21.0.3 with PID 8756 (C:\Users\minos\work\com-minosys-app\sample-app\build\libs\sample-app-0.0.1-SNAPSHOT.jar started by minos in C:\Users\minos\work\com-minosys-app\sample-app)
2024-06-07T04:39:15.846+09:00 INFO 8756 --- [sample-app] [ main] c.m.sample_app.SampleAppApplication : No active profile set, falling back to 1 default profile: "default"
2024-06-07T04:39:16.696+09:00 INFO 8756 --- [sample-app] [ main] c.m.sample_app.SampleAppApplication : Started SampleAppApplication in 1.526 seconds (process running for 2.317)
<<< Sample App >>>
app.api-control-list: [[GROUP1, 1], [GROUP2, 0]]
と出力されることから、List of List が設定されたことが分かる。
コメントを残す