Using a construct such as
@Component public class SomeClass { @Inject private Environment env; private String key; @PostConstruct private void init() { key = env.getProperty("SOME_KEY_PROPERTY"); } .... }
it is possible to assign some field with some property.
Is there a shorter, more concise form to do this?
Spring get value from system property. You can set the system property on the Java command line using the -Dpropertyname=value syntax or at runtime using System. setProperty function. To inject the value from the system property, annotate your field using the @Value annotation with SpEL expression as below.
Using @Value With Maps We can also use the @Value annotation to inject a Map property.
Most people know that you can use @Autowired to tell Spring to inject one object into another when it loads your application context. A lesser known nugget of information is that you can also use the @Value annotation to inject values from a property file into a bean's attributes.
The Javadoc of the @Value annotation.
@Component public class SomeClass { @Value("#{environment.SOME_KEY_PROPERTY}") private String key; .... }
You should be able to do this(assuming that you have a PropertySourcesPlaceHolderConfigurer registered)
@Value("${SOME_KEY_PROPERTY}") private String key;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With