Introduction
The Spring Cloud GCP project makes the Spring Framework a first-class citizen of the Google Cloud Platform.
Spring Cloud GCP allows you to take advantage of the Spring Framework’s power and simplicity to:
- Pub/Sub can be published and subscribed to on Google Cloud.
- With Spring Data Cloud Spanner, Spring Data Cloud Datastore, and Spring Data Reactive Repositories for the Cloud Fire store, you may map objects, relationships, and collections.
- Google Cloud Storage provides backup for Spring Resources. As a result, you are free to write and read from them.
- With Spring Cloud Sleuth and Google Cloud Trace, you can track the execution of your app.
- To use Google Cloud SQL, configure Spring JDBC with a few settings.
- Spring Cloud Config, supported by the Google Runtime Configuration API, allows you to configure your app.
- Using Spring Integration GCS Channel Adapters, consume and generate Google Cloud Storage data.
- Use Google Cloud IAP with Spring Security.
- With Google Cloud Vision, you can search your photographs for text, objects, and other material.
Spring Cloud GCP Core
Each Spring Cloud GCP module uses GcpProjectIdProvider and CredentialsProvider to get the Google Cloud Platform project ID and access credentials.A Spring Boot starter is provided by Spring Cloud GCP to auto-configure the core components.
Maven Coordinates, using Spring Cloud GCP : –
<dependency> <groupId>com.google.cloud</groupId> <artifactId>spring-cloud-gcp-starter</artifactId> </dependency>
Gradle Coordinates : –
dependencies {
implementation("com.google.cloud:spring-cloud-gcp-starter")
}
GcpProjectIdProvider
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface
public interface GcpProjectIdProvider
Functional interface to provide IDs.
The Spring Cloud GCP starter automatically configures a GcpProjectIdProvider. The given GcpProjectIdProvider returns the value of a spring.cloud.gcp.project-id property if one is specified.
spring.cloud.gcp.project-id=my-gcp-project-id
To get the project ID, an ordered list of rules is used:
- The project ID is specified by the GOOGLE CLOUD PROJECT environment variable .
- The GOOGLE APPLICATION CREDENTIALS environment variable points to the project ID supplied in the JSON credentials file.
- The project ID for the Google Cloud SDK and for Google App Engine.
- From the Google Compute Engine Metadata Server, the project ID for Google Compute Engine.
CredentialsProvider
The CredentialsProvider interface returns the credentials used to authenticate and authorise calls to Google Cloud Client Libraries and it is also a functional interface.
public interface CredentialsProvider { Credentials getCredentials() throws IOException; }
The Spring Cloud GCP starter configures a CredentialsProvider for you.It locates a Google service account’s OAuth2 private key using the spring.cloud.gcp.credentials.location property. The credentials file might originate from a variety of sources, including the file system, the classpath, the URL, and so on, because this is a Spring Resource.
spring.cloud.gcp.credentials.location=file:/usr/local/key.json
To set the credentials directly, use the spring.cloud.gcp.credentials.encoded-key property. The value should be the base64-encoded JSON-formatted account private key.
If the credentials aren’t provided in the properties, the starter looks for them elsewhere:
- The GOOGLE APPLICATION CREDENTIALS environment variable points to a credential file.
- The gcloud auth application-default login command in the Google Cloud SDK provides credentials.
- Built-in credentials for Google App Engine, Cloud Shell and Google Compute Engine.
In most circumstances, you should ignore the spring.cloud.gcp.credentials.location property if your app is running on Google App Engine or Google Compute Engine. Let the Spring Cloud GCP Starter retrieve the relevant credentials for those environments. App Engine Standard uses the App Identity service account credentials, App Engine Flexible uses the Flexible service account credentials, and Google Compute Engine uses the Compute Engine Default Service Account.
GcpEnvironmentProvider
The Spring Cloud GCP starter auto-configures GcpEnvironmentProvider as a functional interface that returns a GcpEnvironment enum. The provider can assist in determining the application’s deployment environment programmatically (App Engine Flexible, App Engine Standard, Kubernetes Engine, or Compute Engine).
public interface GcpEnvironmentProvider { GcpEnvironment getCurrentEnvironment(); }
Customizing bean scope
Spring Cloud GCP starters autoconfigure all required beans in the default singleton scope. You have two alternatives if you need a specific bean or group of beans to be dynamically regenerated.
- With @RefreshScope, annotate custom beans of the required types. If your application is already redefining those beans, this makes the most sense.
- List autoconfigured beans in the Spring Cloud property spring.cloud.refresh.extra-refreshable to override the scope.

Spring Initializr
This starter is available through the GCP Support entry in Spring Initializr.
Conclusion
In this blog, we have covered what is Spring Cloud GCP and Spring Cloud GCP core. If you have any feedback or queries, please let me know in the comments.
“Keep Reading, Keep Coding and Keep Sharing”