通过bean注入的方式实现系统初始化properties配置

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通过实现系统初始化properties配置
 * ex-oulingcheng
 * 2020/5/9
 */
@Configuration
public class SftpConfiguration implements InitializingBean {

    @ConfigurationProperties(prefix = "borf.sftp")
    @Bean
    public SftpUtilT.SftpConfig sftpConfig() {
        return new SftpUtilT.SftpConfig();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SftpUtilT.setSftpConfig(sftpConfig());
    }
}

/**
 * 使用该配置的类
 */
@Slf4j
class SftpUtilT {
    private static String host;
    private static String username;
    private static String password;
    private static int port;

    public static void setSftpConfig(SftpConfig sftpConfig) {
        host = sftpConfig.getHost();
        username = sftpConfig.getUsername();
        password = sftpConfig.getPassword();
        port = sftpConfig.getPort();
    }
    
    @Getter
    @Setter
    @Accessors(chain = true)
    public static class SftpConfig {
        private String host;
        private String username;
        private String password;
        private int port;
    }
}

你可能感兴趣的:(java)