1. 程式人生 > >Springboot配置檔案對映

Springboot配置檔案對映

新增類和配置檔案的對映:

1.定義對映類

@Component

@PropertySource("classpath:config/XX.properties")

public class ConfigClass{

  @Value("$name")

  public String userName;

... ...

}

 

2. properties檔案內容

XX.properties:

name=Lorry

... ...

 

3使用如下:

public class RestController{

  @Autowired

  ConfigClass config;

  public void method(){

    logger.info(config.userName);

  }

}

4. 說明

即可,@Component作用是被spring發現,@PropertySource是指定對映檔案,@Value指定對映欄位。使用類只要宣告為@Autowired即可。