1. 程式人生 > >提取屬性檔案或yml檔案

提取屬性檔案或yml檔案

屬性檔案格式一:物件形式

1、application.yml

thread:
 corePoolSize: 30
 maxPoolSize: 50
 queueCapacity: 1000
 keepAliveSeconds: 300

2、配置類

@Data
@NoArgsConstructor
@AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "thread")//字首
//如果檔名為application.yml,且在resource一級目錄中,則可忽略
@PropertySource(value = "classpath:config/application.yml",ignoreResourceNotFound = true)
public class ThreadPoolSettings {
    // 核心執行緒數
    private Integer corePoolSize;

    // 最大執行緒數
    private Integer maxPoolSize;

    // 佇列最大長度
    private Integer queueCapacity;

    // 執行緒池維護執行緒所允許的空閒時間
    private Integer keepAliveSeconds;
}

3、應用形式

@Autowired
private ThreadPoolSettings threadPoolSettings;

 

屬性檔案格式二:Map形式

1、application.yml

customer:
  discount :
    1 : com.huace.thread.com.huace.pattern.strategy.OrgnicCustomer

    2 : com.huace.thread.com.huace.pattern.strategy.VipCustomer

    3 : com.huace.thread.com.huace.pattern.strategy.SuperVipCustomer

2、配置類

@Data
@NoArgsConstructor
@AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "customer")
@PropertySource(value = "classpath:/application.yml",ignoreResourceNotFound = true)
public class PropertiesConfig {

    public  Map<String,String> discount = new HashMap<>();
}

3、應用形式

@Autowired
private PropertiesConfig propertiesConfig;

 

屬性檔案格式三:集合形式

1、application.yml

customer:
  students :
   - name : chenmingjian
     age  : 25
     city : shenzhen

   - name : wangyuxuan
     age  : 26
     city : shenzhen

2、配置類

@Data
@NoArgsConstructor
@AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "customer")
public class StudentSetting {

    private List<Student> students;
}

3、應用形式

@Autowired
private StudentSetting studentSetting;