2017-10-12 94 views
0

我有一個簡單的類使用@PropertySource像這樣彈簧自動創建屬性文件

@Component 
@PropertySource(value = "file:${user.home}/.cooperp/app-conf.properties") 
public class ApplicationProperties { 
    @Value("${mysql-location}") 
    private String mysqlLocation; 

    public String getMysqlLocation() { 
     return mysqlLocation; 
    } 

    public void setMysqlLocation(String mysqlLocation) { 
     this.mysqlLocation = mysqlLocation; 
    } 
} 

我知道代表的屬性文件,如果文件沒有找到一個可以添加ignoreResourceNotFound = true這將使春季忽略這一情況並啓動了應用。

我想春天到創建文件它沒有找到,不會忽略或拋出異常。

+1

您需要爲此編寫代碼,這不是'@ PropertySource'的任務/函數。 –

+0

哪裏,在主要的方法? –

+0

你爲什麼要這樣做? – PaulNUK

回答

1

通常我們會將屬性文件保留在項目目錄中。但是如果您的項目需要外部屬性,那麼您可以檢查該文件是否存在於SpringBootApplication類中。如果它沒有,那麼你可以在那裏創建屬性文件。代碼示例:

@SpringBootApplication 
public class SpringDocBotApplication { 

    public static void main(String[] args) { 
     File file = new File("EXPECTED PATH"); 

     try{ 
      if(!file.exists()){ 
       // create propeties file add properties 
      } 
     } catch (IOException e) { 
     //HANDLE EXCEPTION 
     } 
     SpringApplication.run(SpringDocBotApplication.class, args); 
    } 
}