2017-10-05 21 views
0

我創建了一個WebserviceCredentials類,如下配置並用@Autowired進行的@Component類不工作WebserviceCredentials(顯示爲空值),但在使用時它的工作原理@restcontroller類,感謝您的幫助@ConfigurationProperties工作在控制器註解類,但不是在組件類

@Component 
@ConfigurationProperties(prefix="webservice") 
public class WebserviceCredentials { 

    @Value("${webservice.EndPoint}") 
    private String webserviceEndpoint; 
    @Value("${webservice.Username}") 
    private String username; 
    @Value("${webservice.Password}") 
    private String password; 

    public String getwebserviceEndpoint() { 
     return webserviceEndpoint; 
    } 
    public void setwebserviceEndpoint(String webserviceEndpoint) { 
     this.webserviceEndpoint = webserviceEndpoint; 
    } 

    public String getUsername() { 
     return username; 
    } 
    public void setUsername(String username) { 
     this.username = username; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 


} 

回答

0

的設置沒有與如何使用ConfigurationProperties

@ConfigurationProperties(prefix="webservice") 
public class WebserviceCredentials { 
private String endpoint; 
private String username; 
private String password; 

public String getEndpoint() { 
    return endpoint; 
} 
public void setEndpoint(String endpoint) { 
    this.endpoint = endpoint; 
} 

public String getUsername() { 
    return username; 
} 
public void setUsername(String username) { 
    this.username = username; 
} 
public String getPassword() { 
    return password; 
} 
public void setPassword(String password) { 
    this.password = password; 
} 
} 

內嵌然後加入@EnableConfigurationProperties(WebserviceCredentials.class)Configuration或主Applicati在類。

進一步的信息

+0

的問題解決了,我沒有使用在組件類一個bean這使得配置對象空,感謝 –

+0

注意,設置你的不是如何既可以使用配置屬性。 –

0

的@ConfigurationProperties(PREFIX = 「web服務」)應該駐留在你的POJO,代表你的屬性文件中看到https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties。要在WebserviceCredentials類使用它,你可以使用

public class WebserviceCredentials { 
    @Autowire 
    private ConfigurationProperties configurationProperties; 
    //the rest of your code 
} 

您可以參考此鏈接我剛剛發佈

Mapping YMAL properties

相關問題