[英]Spring and @Value annotation
我正在嘗試使用Spring框架構建控制台應用程序。
我有一個由@SpringBootApplication
注釋的@SpringBootApplication
:
@SpringBootApplication
public class LoaderApplication {
public static void main(String[] args) {
SpringApplication.run(LoaderApplication.class, args);
}
}
和由@Component
注釋的@Component
@Component
public class ElasticLoader implements CommandLineRunner {
// Elastic properties
@Value("${elasticTransportAddress:#{null}}")
private String elasticTransportAddress;
private static final Logger log = LoggerFactory.getLogger(ElasticLoader.class);
@Override
public void run(String... arg) throws Exception {
if (!( elasticTransportAddress != null && elasticTransportAddress.isEmpty() )) {
log.error("[elasticTransportAddress] is not defined in property file");
return;
}
log.info("-> ElasticLoader.run");
}
}
如您所見,在該類中,我嘗試通過@Value
注釋注入屬性elasticTransportAddress
值,但是在運行我的應用程序后,我可以看到該屬性elasticTransportAddress
分配。
我錯過了什么?
讓我添加一些注釋:
我想將我的應用程序與其他屬性文件一起使用。 對於這種情況,我創建了具有以下內容的xml-context-config.xml
:
<beans profile="companies">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-dev files on the classpath -->
<context:property-placeholder
location="classpath:companies.properties"
ignore-unresolvable="true" />
<!-- scans for annotated classes in the com.env.dev package -->
<context:component-scan base-package="ru.alexeyzhulin" />
</beans>
並使用我放入的配置文件companies.properties
我的運行配置具有所描述的配置文件:
正如我在應用程序日志中看到的那樣,此配置文件已啟用:
2017-01-15 00:10:39.697 INFO 10120 --- [ main] ru.alexeyzhulin.LoaderApplication : The following profiles are active: companies
但是,當我在application.properties
定義屬性並使用默認配置文件時,將分配屬性elasticTransportAddress
。
if
條件中有錯誤。 這個:
if (!( elasticTransportAddress != null && elasticTransportAddress.isEmpty() ))
等效於此:
if (elasticTransportAddress == null || !elasticTransportAddress.isEmpty())
因此,在兩種情況下,您將收到錯誤日志消息+返回:
解決方法是重寫if
條件,例如
if (elasticTransportAddress == null || elasticTransportAddress.isEmpty())
更新1:當然,您需要確保在應用程序屬性中確實定義了該值。
更新2: Spring完全不使用您的xml-context-config.xml
文件。 推薦的方法是對@SpringBootApplication
使用基於注釋的配置。 您可以通過完全刪除xml-context-config.xml
並創建一個帶有@Configuration注釋的類來做到這一點。 此配置類可以包含定義屬性來源的注釋。 例如:
@Configuration
@Profile("companies")
@PropertySource("classpath:companies.properties")
public final class LoaderApplication {
}
請注意,您不需要明確啟用@ComponentScan
如果使用@SpringBootApplication
。
將companies.properties重命名為application-companies.properties。 鏈接到文檔
正如另一個@Alex所說的,您必須告訴Spring您想從哪里檢索那些屬性。 您缺少的是另一種配置,在您的情況下(基於注釋的配置),您必須添加此類:
@Configuration
public class MyConfiguration {
@Bean
public PropertyPlaceholderConfigurer getCompanyConfigurer() throws IOException {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setLocations(new ClassPathResource("classpath:companies.properties"));
configurer.setIgnoreUnresolvablePlaceholders(true);
return configurer;
}
}
更新:
如果您使用的是基於XML的配置,則必須告訴Spring Boot什么是XML配置,請刪除LoaderApplication中的@SpringBootApplication批注,並以這種方式將XML添加到SpringApplication中:
SpringApplication.run("classpath:application-configuration.xml", args);
最后將此行添加到xml中:
<context:property-placeholder ignore-unresolvable="true" location="classpath:companies.properties"/>
而且,就是這樣,希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.