I'm trying to take advantage of @Value
annotation and auto-populate my string-variable from the properties file, but with no luck. Values are not being set and are null
.
Here is my configuration:
SendMessageController.java
@RestController
public class SendMessageController {
@Value("${client.keystore.type}")
private static String keystoreType;
@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
public ResponseEntity<SendMessageResponse> sendMessage(@Validated @RequestBody SendMessageRequest messageRequest) {
.......
}
application.properties
client.keystore.type=JKS
rest-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.example.controllers" />
<context:property-placeholder location="classpath:application.properties"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<mvc:annotation-driven />
</beans>
When I run my application and try to access keystoreType
variable it is always null
.
What am I doing wrong?
Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.
@Value is a Java annotation that is used at the field or method/constructor parameter level and it indicates a default value for the affected argument. It is commonly used for injecting values into configuration variables - which we will show and explain in the next part of the article.
@Value annotation can be used within classes annotated with @Configuration , @Component and other stereotype annotations like @Controller , @Service etc. The actual processing of @Value annotation is performed by BeanPostProcessor and so @Value cannot be used within BeanPostProcessor class types.
For the record, the specification of what a POJO is, means that the class cannot contain pre-specified annotations. So even in another world where @Value could work on a non-spring bean, it would still, by definition, break the POJO aspect of the class.
Spring can not inject @Value
to a static field directly.
you can either add inject the value through an annotated setter like this:
private static String keystoreType;
@Value("${client.keystore.type}")
public void setKeystoreType(String keystoreType) {
SendMessageController.keystoreType = keystoreType;
}
Or Change :
@Value("${client.keystore.type}")
private static String keystoreType;
to :
@Value("${client.keystore.type}")
private String keystoreType;
For the ones still facing the issue after all the preceding suggestions, make sure you are not accessing that variable before the bean has been constructed as described in this answer.
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