2017-07-25 80 views
1

我有一個小小的SpringBoot應用程序,它可以通過OpenLdap執行不同的功能。SpringBoot Configuration with application.yml

  • getUser
  • createUser
  • deleteUser
  • etc.

這工作正常。現在我想創建一個application.Yml,我可以用不同的憑據來管理不同的環境。我閱讀了一些教程,但我仍然有一些理解問題。其實我的代碼看起來像這樣:

UserController中:

... 
protected static String serverURL = "xxxxx:90xx"; 
protected static String LdapBindDn = "cn=admin, xxxxx"; 
protected static String LdapPassword = "xxxx"; 
... 

@RequestMapping(value = "/{userid:.+}",method = RequestMethod.GET,consumes="application/json",produces = "application/json") 
public UserData getUser(@PathVariable String userid) {   
    DirContext context = connectToLdap(); 
    //some operations... 
    context.close();  
    return user; 
} 
... // same for the other functions 

我的計劃是現在,我想在一個額外的application.yml而不是在UserController中的開始(見上文)指定的憑據。

以後,我在src /主/資源創建的application.yml:

# Actual environment 
spring: 
    profiles.actives: development 
--- 
# Dev Profile 
spring: 
    profiles: dev 
datasource: 
    serverUrl: ldaps://xxxxxx:90xx 
    AdminName: xxxx 
    AdminPassword: xxxxxx 
    BaseDN: xxxxx 
--- 
# Production Profile 
spring: 
    profiles: prod  
datasource: 
serverUrl: ldaps://xxxx2:90xx 
AdminName: xxxxx2 
AdminPassword: xxxxx2 
BaseDN: xxxxxx 

現在我需要調用此配置。我已經閱讀過一篇教程(http://therealdanvega.com/blog/2017/06/26/spring-boot-configuration-using-yaml),我必須爲.yml文件的屬性創建額外的類「ApplicationProperties」。

@Component 
@ConfigurationProperties("datasource") 
public class ApplicationProperties { 

private String serverURL; 
private String adminName; 
private String adminPassword; 
private String baseDN; 

// Getter-/Setter-Methods 

}

現在,我需要從從.yml值開始定義我的變量,對不對?我回到我的UserController並嘗試類似這樣的:

private String serverURL; 
private String adminName; 
private String adminPassword; 
private String baseDN; 

@Autowired 
ApplicationProperties appProp; 

@RequestMapping(value = "/{userid:.+}",method = RequestMethod.GET,consumes="application/json",produces = "application/json") 
public UserData getUser(@PathVariable String userid) {   
    DirContext context = connectToLdap(); 
    //some operations... 
    context.close();  
    return user; 
} 
... // same for the other functions 


private DirContext connectToLdap(){ 
    System.out.prinln(appProp.getServerURL()); 
    System.out.prinln(appProp.getAdminName()); 
    System.out.prinln(appProp.getAdminPassword()); 
    .... // Code for the Ldap connection 
} 

但變量「appProp」仍爲空。我知道,這是一個很大的理解問題。我不知道如何從.yml文件調用這些屬性。

感謝您提前給予的幫助!

+0

[spring-boot config](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config。HTML)你可以在這裏的詳細信息像配置文件特定的配置,如何在你的班級使用,等 – fiskra

+0

感謝您的鏈接。但我已經讀過這個。 有人可以告訴我,我在代碼中的錯誤理解是哪裏? – InfoEngi

+0

首先,您可以創建基於配置文件的屬性,因爲spring boot會自動選擇它們,比如'application- {profile}。{properties | yml}' – fiskra

回答

0

您可以通過創建一個配置類從你.yml文件屬性:

application.yml

datasource: 
    serverUrl: ldaps://xxxxxx:90xx 
    adminName: xxxx 
    adminPassword: xxxxxx 
    baseDN: xxxxx 

ApplicationConfig.java類:

@Configuration 
@EnableConfigurationProperties 
@ConfigurationProperties(prefix = "datasource") 
public class ApplicationConfig { 

    private String serverUrl; 
    private String adminName; 
    private String adminPassword; 
    private String baseDN; 

    //getters setters 

} 

然後,你可以打電話給你ApplicationConfig

@Autowired 
public ApplicationConfig app; 

public UserData getUser(@PathVariable String userid) {   
     DirContext context = connectToLdap(); 
     //some operations... 
     appProp.getServerUrl(); 
     appProp.getAdminName(); 
     context.close();  
     return user; 
} 

,我建議你創建一個基於配置文件屬性如春開機自動拾取它們,就像application-{profile}.{properties|yml} 您可以創建application-production.yml文件,並通過在類中添加註釋@Profile("production")設置您的個人資料。

+0

好吧,我已經更新了您所說的代碼。往上看。 現在我得到所有的時間「java.lang.NullPointerException」,因爲例如函數「appProp.getAdminName()」返回「null」。 – InfoEngi

+0

我更新了我的文章並添加了完整的解決方案。 – fiskra

+0

非常感謝你「fiskra」。你以前的版本也在工作。錯誤在.yml文件中。 我寫過「profiles.actives:development」,但它必須是「profiles.active:development」。 現在它的工作!謝謝你。 – InfoEngi