1. 程式人生 > 其它 >java 讀取 application配置檔案

java 讀取 application配置檔案

技術標籤:springbootjava

application.yml

hedd:
  #單字
  younger-brother: 小弟
  little-brother: 小兄弟
  #類資料
  all:
    bigSister: 大妹子
    littleGirl: 小妹子
    #組資料
    ofAll:
      yourUncle: 你大爺,你三大爺,你二大爺

maven

  <!--註釋器配置 -> 讀取配置檔案-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--配置提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

HeddConfiguration.java

/*,
@Configuration:用於定義配置類,可替換xml配置檔案,

被註解的類內部包含有一個或多個被@Bean註解的方法,

這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,

初始化Spring容器。*/
@Configuration
/*
 *讀取配置java專案原始碼 www.fhadmin.org
 * */
@ConfigurationProperties("hedd")
public class HeddConfiguration {

    private String youngerBrother;

    private String littleBrother;

    private All all;



    public String getYoungerBrother() {
        return youngerBrother;
    }

    public void setYoungerBrother(String youngerBrother) {
        this.youngerBrother = youngerBrother;
    }

    public String getLittleBrother() {
        return littleBrother;
    }

    public void setLittleBrother(String littleBrother) {
        this.littleBrother = littleBrother;
    }

    public All getAll() {
        return all;
    }

    public void setAll(All all) {
        this.all = all;
    }


}

All.java


/**
 * @Classname All
 * @Description TODO
 * @Date 2021/1/16 11:16
 * @java專案原始碼 www.fhadmin.org
 */
public class All {

    private String bigSister;
    private String littleGirl;
    private List<String> ofAll;

    public String getBigSister() {
        return bigSister;
    }

    public void setBigSister(String bigSister) {
        this.bigSister = bigSister;
    }

    public String getLittleGirl() {
        return littleGirl;
    }

    public void setLittleGirl(String littleGirl) {
        this.littleGirl = littleGirl;
    }

    public List<String> getOfAll() {
        return ofAll;
    }

    public void setOfAll(List<String> ofAll) {
        this.ofAll = ofAll;
    }

    @Override
    public String toString() {
        return "All{" +
                "bigSister='" + bigSister + '\'' +
                ", littleGirl='" + littleGirl + '\'' +
                ", ofAll=" + ofAll +
                '}';
    }
}

HeddService.java


@Slf4j //日誌
/*
*@Component
*
*把普通pojo例項化到spring容器中,相當於配置檔案中的
*
<bean id="" class=""/>)
*
泛指各種元件,就是說當我們的類不屬於各種歸類的時候(不屬於@Controller、@Services等的時候),我們就可以使用@Component來標註這個類。
*java專案原始碼 www.fhadmin.org
* */
@Component
public class HeddService {

    /*
    * @Autowired 註釋,它可以對類成員變數、方法及建構函式進行標註,
    *
    * 完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。
    *
    * 在使用@Autowired之前,
    *
    * */
    @Autowired
    private HeddConfiguration heddConfiguration;

    /*
    * @PostConstruct
    *
    * 該註解被用來修飾一個非靜態的void()方法。被@PostConstruct修飾的方法會在伺服器載入Servlet的時候執行,
    *
    * 並且只會被伺服器執行一次。PostConstruct在建構函式之後執行,init()方法之前執行。
    *
    *通常我們會是在Spring框架中使用到@PostConstruct註解 該註解的方法在整個Bean初始化中的執行順序:
    *
    *Constructor(構造方法) -> @Autowired(依賴注入) -> @PostConstruct(註釋的方法)
    *
    * */
    @PostConstruct
    public void init() {
        log.info("-------------------------------開始--------------------------------------------");
        log.info("Get current configuration Single word",heddConfiguration.getYoungerBrother());
        log.info("Get current configuration Single word",heddConfiguration.getLittleBrother());
        log.info("Get current configuration Class data",heddConfiguration.getAll());
        log.info("-------------------------------結束--------------------------------------------");
    }
}


wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==