Spring属性注入最终属性@Value - Java [英] Spring Property Injection in a final attribute @Value - Java

查看:874
本文介绍了Spring属性注入最终属性@Value - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个属性文件,我想存储一个文件路径。一般来说,我使用属性文件我使用这样的方式设置类属性:

  private @Value(#{someProps ['prop.field ']})String someAttrib; 

然后在我的 spring.xml 有一些如下所示:

 < util:properties id =someProps
location =classpath:/ META- INF / properties / somePropFile.properties/>

这个工作很好,很简单,代码很好整洁。但是我不知道当试图将属性值注入到最终的类属性中时,最简单的模式是什么?



显然是这样的:

  private static final @Value(#{fileProps ['dict.english']})String DICT_PATH; 

将无法正常工作。有另外一种方式吗?



干杯!

解决方案

您可以通过构造函数注入。其他一切都将是春天的一个可怕的黑客。


A simple question on Spring injection from a properties file for a final attribute.

I have a properties file which I want to store a file path in. Generally when I use properties files I setup class attributes using something like this:

private @Value("#{someProps['prop.field']}") String someAttrib ;

Then in my spring.xml I would have something like:

<util:properties id="someProps"   
    location="classpath:/META-INF/properties/somePropFile.properties" />

This works well, is simple and makes code nice and neat. But I'm not sure what is the neatest pattern to use when trying to inject properties values into final class attributes?

Obviously something like:

private static final @Value("#{fileProps['dict.english']}") String DICT_PATH; 

will not work. Is there another way?

Cheers!

解决方案

The only way you can inject values into a final field is through Constructor Injection. Everything else would be an awful hack on Spring's side.

这篇关于Spring属性注入最终属性@Value - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆