user1107753
user1107753

Reputation: 1646

spring boot @Value always null

I am using spring boot 1.5.3 and trying to inject the properties from an application-dev.properties file into a Service bean but the value is always coming as null. The value does get loaded in my DevConfiguration class though.

I have a application class as below in my base package

package com.me;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

I have a configuration class as follows in

package com.me.print.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@Profile("dev")
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:application-dev.properties")
})
@ComponentScan(value = {"com.me.print.client"})
public class DevConfiguration {

    @Value("${app.service.url}")
    private String rootUri;

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

My Service bean that I am trying to load the value into is below

package com.me.print.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.me.print.model.zitResponse;

@Service
public class zitPrintClient {

    private final RestTemplate restTemplate;

    @Value("${app.service.url}")
    private String rootUri;

    public zitPrintClient(RestTemplateBuilder restTemplateBuilder) {
        restTemplate = restTemplateBuilder
                //.rootUri(rootUri)
                .build();
    }

    public zitResponse getpooltatus(String poolId) {
        return restTemplate.getForObject("/pool/{poolId}/@status",
                  zitResponse.class, poolId);
    }

}

In the above class the rootURI is always null. Does anyone have any suggestions as to what I am missing

in my application-dev.properties file I have the following

app.service.url=http://localhost:8080/zitprint/v1

Thanks

UPDATE:

does anyone have any suggestions here as I tried to inject properties into my controller as follows:

@Value("${allowedVendors}") String allowedVendors

and if i put the above into a constructor it finds the value but does not find it otherwise:

public PController(@Value("${allowedVendors}") String allowedVendors) {

}

I cant use the property further in the code as with the constructor I have created two instances of the bean 1 via the constructor and the other created by spring DI. Any ideas why the value doesnt inject without the constructor

Thanks

Upvotes: 2

Views: 3281

Answers (1)

Erik Pearson
Erik Pearson

Reputation: 1393

You need to put it as a parameter in the constructor:

public zitPrintClient(RestTemplateBuilder restTemplateBuilder, 
     @Value("${app.service.url}") rootUri) {
         this.rootUri = rootUri; // if you are only using this once, 
                                 // no need to keep this member variable around
         restTemplate = restTemplateBuilder
                .rootUri(rootUri)
                .build();
    }

The constructor gets called first when you are creating the object. The member variable, rootUri, would have it's value injected after the object is created. So, rootUri member variable would be null at the time the constructor is called.

(And, imho, for better readability, your class should start with a capital letter, i.e. ZitPrintClient, but it's your code ...)

Upvotes: 2

Related Questions