2017-10-18 70 views
1

獲取值我有一個樣品春季啓動應用程序下面的代碼@Value與規劃環境地政司無法從性能

@Configuration 
@PropertySource("classpath:second.properties") 
public class PropertyConfig { 

    @Value("#{guru.username}") 
    String user; 

    @Value("#{guru.password}") 
    String password; 

    @Value("#{guru.url}") 
    String url; 


    @Bean 
    FakeDataSource getFakeDataSource() { 
     FakeDataSource fk = new FakeDataSource(); 

     fk.setName(user); 
     fk.setPassword(password); 
     fk.setUrl(url); 

     return fk; 
    } 

    @Bean 
    PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() { 
     PropertySourcesPlaceholderConfigurer placeholderConfigurer= new PropertySourcesPlaceholderConfigurer(); 
     //placeholderConfigurer.setLocation(new ClassPathResource("second.properties")); 

     return placeholderConfigurer; 
    } 
} 

而且FakeDataSource是一個簡單的POJO的名稱,passowrd,URL屬性。

然後我的主要應用

@SpringBootApplication 
public class SpringGuru101DependencyInjectionApplication { 
    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(SpringGuru101DependencyInjectionApplication.class, args); 

     // Step 2: Make Class 
     FakeDataSource fakeDataSource = ctx.getBean(FakeDataSource.class); 

     System.out.println(fakeDataSource.getName()); 
    } 
} 

但SOUT語句打印空, 我second.properties文件存在於我的資源目錄中有以下內容

guru.username=Saurabh 
guru.password=ido 
guru.url=http://example.com 
+0

嘗試'@ImportResource( 「classpath:second.properties」)' –

+0

請嘗試將井號(#)替換爲美元符號($),以便從配置文件中讀取值。例如:'@Value(「$ {guru.username}」)' – LHCHIN

+0

@ScaryWombat應用程序無法編譯,如果我使用@ImportSource(_classpath_) 'org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:Line 1 in來自類路徑資源[second.properties]的XML文檔無效;嵌套異常是org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;內容沒有prolog.'不允許 – Saurabh

回答

1

有兩個地方應該更正:
(1)正如我在您的問題的評論中所說的,您應該將井口符號(#)替換爲美元符號($)以讀取配置文件中的值。例如:@Value("${guru.username}")

(2)您在方法getPropertySourcesPlaceholderConfigurer之前錯過了public static
而這種改進的方法應該看起來像如下:

@Bean 
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() { 
    PropertySourcesPlaceholderConfigurer placeholderConfigurer= new PropertySourcesPlaceholderConfigurer(); 
    //placeholderConfigurer.setLocation(new ClassPathResource("second.properties")); 

    return placeholderConfigurer; 
} 
+0

謝謝...我檢查了它的工作:) – Saurabh

0

如果您在使用Spring啓動時,您可以使用另一種方法讀取配置是春季啓動建議。這將幫助您擺脫所有的@Value符號,允許彈簧注入屬性而無需附加提示。

可以潛在地做這樣的事情:

@ConfigurationProperties("foo") 
public class FooProperties { 

private boolean enabled; 

private InetAddress remoteAddress; 

private final Security security = new Security(); 

public boolean isEnabled() { ... } 

public void setEnabled(boolean enabled) { ... } 

public InetAddress getRemoteAddress() { ... } 

public void setRemoteAddress(InetAddress remoteAddress) { ... } 

public Security getSecurity() { ... } 

public static class Security { 

    private String username; 

    private String password; 

    private List<String> roles = new ArrayList<>(Collections.singleton("USER")); 

    public String getUsername() { ... } 

    public void setUsername(String username) { ... } 

    public String getPassword() { ... } 

    public void setPassword(String password) { ... } 

    public List<String> getRoles() { ... } 

    public void setRoles(List<String> roles) { ... } 

} 

}

上面的POJO定義了以下屬性:

foo.enabled,默認情況下 foo.remote地址虛假,用一種可以從字符串 foo.security.username強制轉換的類型,其中嵌套的「安全性」的名稱由屬性的名稱確定。特別是返回類型完全不存在使用,並可能已經SecurityProperties foo.security.password foo.security.roles,用字符串的集合

更多細節:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

+0

Afaik這是建議,如果你想要很多字段匹配的屬性類說的「foo.xxx」,但不是當你使用1特定屬性 – Saurabh