網頁

2020/10/1

Spring Boot @ConfigurationProperties nested properties

Spring Boot @ConfigurationProperties類對應的properties可用巢狀類別(nested class)呈現。

範例環境:

  • Java 8
  • Spring Boot 2.3.2.RELEASE
  • Maven
  • Lombok

system開頭的properties對應至SystemProperties;system.admin開頭的對應至SystemProperties.admin

system.properties

system.name=Demo system
system.version=1.0.0
system.url=192.168.0.111
system.port=8080

system.key-map.key1=value1
system.key-map.key2=value2;

system.admin.username=admin
system.admin.password=12345
system.admin.schemas=greenlake, redsky

@NestedConfigurationProperty僅用來作為Spring Boot Configuration Processor的標示,對程式無影響。

SystemProperties

package com.abc.demo.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.List;
import java.util.Map;

@Getter
@Setter
@ConfigurationProperties(prefix = "system")
public class SystemProperties {

    private String name;    // system.name
    private String version; // system.version
    private String url;     // system.url
    private String port;    // system.port

    private Map<String, String> keyMap; // {key2=value2;, key1=value1}

    @NestedConfigurationProperty
    private Admin admin; // system.admin

    @Getter
    @Setter
    public static class Admin {

        private String username; // system.admin.username
        private String password; // system.admin.password
        private List<String> schemas; // system.admin.schemas

    }

}

@SpringBootApplication類取得SystemProperties實例並印出properties內容。

DemoApplication

package com.abc.demo;

import com.abc.demo.properties.SystemProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;

@EnableConfigurationProperties(SystemProperties.class)
@PropertySource("classpath:system.properties")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
        SystemProperties systemProperties = ctx.getBean(SystemProperties.class);

        System.out.println(systemProperties.getName());    // Demo system
        System.out.println(systemProperties.getVersion()); // 1.0.0
        System.out.println(systemProperties.getUrl());     // 192.168.0.111
        System.out.println(systemProperties.getPort());    // 8080

        System.out.println(systemProperties.getKeyMap());  // {key2=value2;, key1=value1}

        System.out.println(systemProperties.getAdmin().getUsername()); // admin
        System.out.println(systemProperties.getAdmin().getPassword()); // 12345
        System.out.println(systemProperties.getAdmin().getSchemas());  // [greenlake, redsky]

    }

}

啟動後印出結果如下。

Demo system
1.0.0
192.168.0.111
8080
{key2=value2;, key1=value1}
admin
12345
[greenlake, redsky]

參考github


沒有留言:

張貼留言