Home > Back-end >  Spring @Value not working in Spring Boot 2.5.5, getting null values
Spring @Value not working in Spring Boot 2.5.5, getting null values

Time:08-24

I am trying to inject some property values into variables by means of Spring @Value annotation but I get null values. I tried different configurations and triks but it doesn't work. Think is that before today everythink was working properly. I do not know what I changed in order to get things broken.

Here is my java class:

@Component
@ConditionalOnProperty(prefix = "studioghibli", name = "get")
public class StudioGhibliRestService {

    @Value("${studioghibli.basepath}")
    private static String BASE_PATH;

    @Value("${studioghibli.path}")
    private static String PATH;

    @Value("${studioghibli.protocol:http}")
    private static String PROTOCOL;

    @Value("${studioghibli.host}")
    private static String HOST;

    private static String BASE_URI = PROTOCOL.concat("://").concat(HOST).concat(BASE_PATH).concat(PATH);

    @Autowired
    StudioGhibliRestConnector connector;

    public List<StudioGhibliFilmDTO> findAllFilms() throws SipadContenziosoInternalException {
        var response = connector.doGet(BASE_URI, null, null);
        if (!response.getStatusCode().is2xxSuccessful() || !response.hasBody()) {
            throw new SipadContenziosoInternalException(Errore.INTERNAL_REST_ERROR, "FindAll(), microservizio ".concat(BASE_URI), null);
        }
        return (List<StudioGhibliFilmDTO>) response.getBody();
    }

}

As you can see, the class is annotated with @Component, that because I will need to use it as @Service layer in order to make a rest call in my business logic. The class is also annotaded with conditional on property...

Here is a screenshot of the debug window at startup:

debug window

Since the PROTOCOL value is null, i get a null pointer exception immediately at start up.

Here is part of the application-dev.properties file:

studioghibli.get
studioghibli.protocol=https
studioghibli.host=ghibliapi.herokuapp.com
studioghibli.basepath=/
studioghibli.path=/films

CodePudding user response:

First of all, @Value annotation does not work with static fields.

Secondly, fields with @Value annotation is processed when the instance of the class (a bean) is created by Spring, but static fields exist for a class (for any instance), so when the compiler is trying to define your static BASE_URI field other fields are not defined yet, so you get the NPE on startup.

So you might need a refactoring, try to inject values with the constructor like this:

@Component
@ConditionalOnProperty(prefix = "studioghibli", name = "get")
public class StudioGhibliRestService {
    
    private final StudioGhibliRestConnector connector;

    private final String baseUri;

    public StudioGhibliRestService(StudioGhibliRestConnector connector,
            @Value("${studioghibli.basepath}") String basePath,
            @Value("${studioghibli.path}") String path,
            @Value("${studioghibli.protocol:http}") String protocol,
            @Value("${studioghibli.host}") String host) {

        this.connector = connector;
        this.baseUri = protocol.concat("://").concat(host).concat(basePath).concat(path);
    }

    // other code

}
  • Related