Search code examples
springspring-bootspring-cloudcloud-foundry

Does migrating to CfEnv in Spring Boot make properties file redundant?


The deprecation of Auto Reconfiguration in new JavaBuildPack is puzzling me.

see https://docs.cloudfoundry.org/buildpacks/java/configuring-service-connections.html#migrating

The solution is to create a bean CfEnv and to use SpEL to set the properties, e.g.:

cassandra.contact-points=#{ cfEnv.findCredentialsByTag('cassandra').get('node_ips') }

But it seems that the expression language doesn't work with YAML properties. I'm not even sure the .properties will work either. Even if this works with YAML/Properties file then there are scenarios that we will be in trouble, for example, when we don't have environment specific property file but instead we have component specific properties:

We have this structure in one of our repositories:

#application-component1.yml
  myprop.one: ${vcap.services.myservice.myprop.one}
#application-vcap.yml
  vcap.services.myservice.myprop.one: default-value-when-run-locally

Now if we use @Value("#{cfEnv.getService('service').username}") then how can we set the property in YAML or .properties file? How do we run this in our local server when developers are testing it?

Another question is, what if we decide to switch to say Kubernetes or non-cloud option; doesn't that mean we should remove all CfEnv entries?

I can't find anything that addresses these issues, I hope someone can shed light on this for me.


Solution

  • If you are using ${vcap.services.myservice.myprop.one}, which appears to be the case from your example, you can continue using that. The vcap.* prefix is provided by Spring Boot itself. The cloud. prefix was provided by Spring Auto Reconfiguration and it's ONLY the cloud.* prefix that is going away.

    The Spring Auto Reconfiguration module would expose a set of property placeholder values that one could use to access values from VCAP_SERVICES. If you are using these place holders, then you need to switch from using cloud.<property> to vcap.<property>.

    Spring Boot exposes the same information, just under the vcap. prefix instead of cloud..