2017-09-03 132 views
0

我正在向@Value注入一個變量值。出於某種原因,當我有默認值時,它只使用它,它不會在屬性文件中查找它。當我不使用默認值時,它會從屬性文件注入值。沒有其他配置被改變。Spring @Value僅使用默認值

@Value("${migration.paths:#{'classpath:db/migration'}}") 
private String dbMigrationPaths; 

(我的默認值使用SPEL,因爲它有斜槓)

屬性文件配置:

@Bean 
public static PropertySourcesPlaceholderConfigurer configDataSourcesPropertyFile() { 
     PropertySourcesPlaceholderConfigurer bean = new PropertySourcesPlaceholderConfigurer(); 
     bean.setLocations(new ClassPathResource[]{ 
       new ClassPathResource("/file1"), 
       new ClassPathResource("/file2") 
     }); 
     bean.setIgnoreUnresolvablePlaceholders(true); 
     bean.setIgnoreResourceNotFound(true); 
     return bean; 
    } 

兩者都是性文件和財產問題在於file1而不是在file2

+1

請看看[這篇文章](https://stackoverflow.com/questions/28369582/spring-boot-spring-always-assigns-default-value-to-property-despite-of-it-bein ),似乎是一個類似的問題。 – juanlumn

回答

1

您的項目中是否有兩個屬性佔位符?如果是的話,你可能會遇到這裏記錄的這個bug:https://jira.spring.io/browse/SPR-9989。最後看到有一個鏈接到建議的解決方法。

+0

這似乎是它!好的,趕上... – apines