0

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

  • you can't use `@Value` on static fields - Spring doesn't support this. This might be helpful: https://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field. – Andrei Titov Aug 22 '22 at 18:33
  • @AndrewThomas, I have tried to get rid of static modifier but it still doesn't work... thanks – Saverio Mirko Viola Aug 22 '22 at 18:50

2 Answers2

5

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

}
Andrei Titov
  • 1,220
  • 2
  • 15
0

https://docs.spring.io/spring-framework/docs/5.3.25/reference/html/core.html#beans-value-annotations

Thanks, It works for me, I have to add some codes to my project. Then I check the spring core document in "@Value" section. Besides

When configuring a PropertySourcesPlaceholderConfigurer using JavaConfig, the @Bean method must be static.

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer(){
    return new PropertySourcesPlaceholderConfigurer();
}
quentinLi
  • 1
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 06 '22 at 23:11