Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

Spring Property Injection in a final attribute @Value - Java

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?

question from:https://stackoverflow.com/questions/7130425/spring-property-injection-in-a-final-attribute-value-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...