ตัวอย่างการเขียน Spring-boot WebFlux Configuration Properties
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
...
@SpringBootApplication
@ComponentScan(basePackages = {"com.pamarin"})
public class AppStarter {
public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}
classpath:application.properties
pamarin.kong.adminUrl=http://localhost:8001
pamarin.kong.serviceRegistry.name="app"
pamarin.kong.serviceRegistry.url=http://localhost:8080
pamarin.kong.serviceRegistry.routePaths[0]=/
pamarin.kong.serviceRegistry.enabled=false
Reference ไปที่ pamarin.kong.* ในไฟล์ application.properties
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "pamarin.kong")
public class KongProperties {
private String adminUrl;
private ServiceRegistry serviceRegistry;
@Getter
@Setter
public static class ServiceRegistry {
private String name;
private String url;
private List<String> routePaths;
private boolean enabled;
public List<String> getRoutePaths() {
if (routePaths == null) {
routePaths = new ArrayList<>();
routePaths.add("/");
}
return routePaths;
}
public boolean getEnabled() {
return enabled;
}
}
public ServiceRegistry getServiceRegistry() {
if (serviceRegistry == null) {
serviceRegistry = new ServiceRegistry();
}
return serviceRegistry;
}
}
@Slf4j
@RestController
public class HomeController {
private final KongProperties properties;
@Autowired
public HomeController(KongProperties properties) {
this.properties = properties;
}
@GetMapping({"", "/"})
public Mono<KongProperties> hello() {
return Mono.just(properties);
}
}
cd ไปที่ root ของ project จากนั้น
$ mvn clean install
$ mvn spring-boot:run
เปิด browser แล้วเข้า http://localhost:8080
ผลลัพธ์
{
"adminUrl": "http://localhost:8001",
"serviceRegistry": {
"name": "\"app\"",
"url": "http://localhost:8080",
"routePaths": [
"/"
],
"enabled": false
}
}