简体   繁体   中英

Spring Boot: External configuration results in empty value

in my project I have a Spring Boot-based library that delivers me some commonly used services. In a Spring Boot project I make use of the library and do experience some problems while trying to retrieve some external configuration.

The library "reads" some external configuration within the AssetServiceProperties.

@ConfigurationProperties("service.assets")
public class AssetServiceProperties {

    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

This class offers those (external) properties in Java representation for the AssetServiceConfiguration.

@Configuration
@EnableConfigurationProperties(AssetServiceProperties.class)
public class AssetServiceConfiguration {
    @Bean
    public AssetServiceClient assetServiceClient(AssetServiceProperties properties) {
        return new AssetServiceClient(properties.getUrl());
    }
}

The AssetServiceClient is the part of the library that is exposed for clients.

public class AssetServiceClient {

    private String url;
    private RestTemplate restTemplate;
    public static final String CONTROLLER = "assets";

    public AssetServiceClient(String url) {
        this.url = url;
        restTemplate = new RestTemplate();
    }

    public AssetsResponse getByService(String service) {
        UriComponentsBuilder builder = UriComponentsBuilder.
            fromUriString(url).
            pathSegment(CONTROLLER).
            queryParam("service", service);

        return restTemplate.getForObject(builder.build().encode().toUri(), AssetsResponse.class);
    }
}

The Spring Boot project imports the library and is configured with an external configuration file application.properties that contains the external configuration service.assets that is defined in the imported library.

@SpringBootApplication
@EntityScan(basePackageClasses = { Application.class, Jsr310JpaConverters.class })
@Import({AssetServiceConfiguration.class})
@PropertySource(value="classpath:internal.properties")
@PropertySource(value="file:${application.properties}")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

The relevant part of the project that uses the Spring Boot based library is my AssetController.

@RestController
@RequestMapping(value = "/assets")
public class AssetController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private AssetServiceClient assetServiceClient;
    private AssetResourceAssembler assetResourceAssembler;

    @Autowired
    public AssetController(
            AssetServiceClient assetServiceClient,
            AssetResourceAssembler assetResourceAssembler) {
        Assert.notNull(assetServiceClient, "assetServiceClient cannot be null");
        this.assetServiceClient = assetServiceClient;
        this.assetResourceAssembler = assetResourceAssembler;
    }

    @GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE, MediaTypes.HAL_JSON_VALUE})
    public ResponseEntity<Resources<AssetResource>> getAll() {
        AssetsResponse assets = assetServiceClient.getByService("tasks");
        return ResponseEntity.ok(assetResourceAssembler.toResourceList(assets.getContent()));
    }
}

The problem definition : When the Spring Boot project code executes, the value of service.assets is null. Spring Boot does not read the value of the external configuration in the file application.properties . Why? And how can I solve it to get it run?

Another hint: when trying to get the service.assets with the following line of code from within the Spring Boot project

@Value("${service.assets}")
String url;

Spring Boot reads the configuration and gets it filled with a proper value.

The used Spring Boot Version is 1.5.3.RELEASE.

I appreciate any help

@alfcope: Yes that was exactly my problem. The properties file needs to specify the configuration attribute 'service.assets.url'. In my properties file there only was the attribute 'service.assets'.

See also https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties for more details about type safe configuration.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM