Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - Spring @Value annotation returns null

Tags:

java

spring

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?

like image 312
Oleg Avatar asked Apr 10 '16 18:04

Oleg


People also ask

What does @value annotation do in spring boot?

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.

What does @value annotation do in Java?

@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.

Can I use @value in @service?

@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.

Can I use @value in pojo?

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.


Video Answer


2 Answers

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;
like image 75
Rafik BELDI Avatar answered Nov 15 '22 06:11

Rafik BELDI


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.

like image 43
Philippe Simo Avatar answered Nov 15 '22 08:11

Philippe Simo