java - Spring Boot: External configuration results in empty value -
in project have spring boot-based library delivers me commonly used services. in spring boot project make use of library , experience problems while trying retrieve external configuration.
the library "reads" external configuration within 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 (external) properties in java representation assetserviceconfiguration.
@configuration @enableconfigurationproperties(assetserviceproperties.class) public class assetserviceconfiguration { @bean public assetserviceclient assetserviceclient(assetserviceproperties properties) { return new assetserviceclient(properties.geturl()); } } the assetserviceclient part of library exposed 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 library , configured external configuration file application.properties contains external configuration service.assets defined in 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 project uses spring boot based library 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 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 spring boot project code executes, value of service.assets null. spring boot not read value of external configuration in file application.properties. why? , how can solve run?
another hint: when trying service.assets following line of code within spring boot project
@value("${service.assets}") string url; spring boot reads configuration , gets filled proper value.
the used spring boot version 1.5.3.release.
i appreciate help
@alfcope: yes problem. properties file needs specify configuration attribute 'service.assets.url'. in properties file there attribute 'service.assets'.
see https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties more details type safe configuration.
thank you
Comments
Post a Comment