Java Spring和@Value注释
我正在尝试用Spring框架构建一个控制台应用程序 我有一个由Java Spring和@Value注释,java,spring,Java,Spring,我正在尝试用Spring框架构建一个控制台应用程序 我有一个由@springbootplication注释的类: @SpringBootApplication public class LoaderApplication { public static void main(String[] args) { SpringApplication.run(LoaderApplication.class, args); } } 和由@组件注释的类 @Component
@springbootplication
注释的类:
@SpringBootApplication
public class LoaderApplication {
public static void main(String[] args) {
SpringApplication.run(LoaderApplication.class, args);
}
}
和由@组件注释的类
@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>
但是当我在application.properties
中定义属性并使用默认配置文件时,属性elasticTransportAddress
将被分配。如果条件出现错误。
这:
相当于:
if (elasticTransportAddress == null || !elasticTransportAddress.isEmpty())
因此,在两种情况下,您会得到错误日志消息+返回:
如果属性未定义或
如果属性已定义且其值为非空李>
修复方法是重写if
条件,例如
if (elasticTransportAddress == null || elasticTransportAddress.isEmpty())
更新1:您当然需要确保这个值是在应用程序属性中真正定义的
更新2:Spring根本没有使用您的xml context config.xml
文件。
建议使用@SpringBootApplication
的基于注释的配置。您可以通过完全删除xml context config.xml
并创建一个用@Configuration注释的类来完成此操作。此配置类可以包含定义属性源的注释。例如:
请注意,如果使用@SpringBootApplication
,则无需显式启用@ComponentScan
您在if
条件中有一个错误。
这:
相当于:
if (elasticTransportAddress == null || !elasticTransportAddress.isEmpty())
因此,在两种情况下,您会得到错误日志消息+返回:
如果属性未定义或
如果属性已定义且其值为非空李>
修复方法是重写if
条件,例如
if (elasticTransportAddress == null || elasticTransportAddress.isEmpty())
更新1:您当然需要确保这个值是在应用程序属性中真正定义的
更新2:Spring根本没有使用您的xml context config.xml
文件。
建议使用@SpringBootApplication
的基于注释的配置。您可以通过完全删除xml context config.xml
并创建一个用@Configuration注释的类来完成此操作。此配置类可以包含定义属性源的注释。例如:
请注意,如果使用@SpringBootApplication
,则无需显式启用@ComponentScan
将companys.properties重命名为application-companys.properties。要查看文档,请将companys.properties重命名为application-companys.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"/>
就这样,希望它能有所帮助。正如另一个@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"/>
就这样,希望能有所帮助。您的ElasticLoader类定义为Springbean吗?不,不是。你能给我举个例子吗?@SasiKathimanda,如果它被注释为@Component
,那么它应该是一个springbean……是的,意识到了。谢谢:)您的ElasticLoader类定义为SpringBean吗?不,不是。你能给我举个例子吗?@SasiKathimanda,如果它被注释为@Component
,那么它应该是一个springbean……是的,意识到了。谢谢:)但是有条件if(elasticTransportAddress==null)
我得到了相同的结果;您在哪里定义了实际属性?(看)我修改了我的问题。似乎default application.properties文件中的属性可以正常工作,但在另一个文件中则不行t@AlexZhulin,问题是Spring根本没有考虑您的xml context config.xml
。请参阅上一次更新,了解如何使用基于注释的配置实现您想要的功能(这是一种更简单和推荐的方法)。使用基于XML的配置是可能的,但无论如何它都需要一个@Configuration
-注释类,所以上面的操作非常简单;您在哪里定义了实际属性?(看)我修改了我的问题。似乎default application.properties文件中的属性可以正常工作,但在另一个文件中则不行t@AlexZhulin,问题是Spring根本没有考虑您的xml context config.xml
。请参阅上一次更新,了解如何使用基于注释的配置实现您想要的功能(这是一种更简单和推荐的方法)。可以使用基于XML的配置,但无论如何它都需要一个@Configuration
-注释类,所以上面的操作非常简单。但为什么是c