Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Issue with @Bean Annotation in When Deploying Spring Cloud Functin to Google Cloud Functions

I am experiencing an issue when deploying a Spring Cloud Function application to Google Cloud Functions (GCP). The problem arises when I try to define functions using the @Bean annotation in separate .java files (i.e., files other than the one containing the main method).

When I define functions using the @Bean annotation in separate .java files, everything works perfectly fine when running the application locally. However, when I deploy the same application to Google Cloud Functions, I encounter the following error:

IllegalArgumentException: Failed to lookup function to route based on the value of 'spring.cloud.function.definition' property 'uppercase'

The error disappears if I move all the @Bean annotated methods into the same .java file that contains the main method. This behavior seems inconsistent and is causing issues when trying to modularize the codebase.

I have seen a similar issue reported, but there has been no response. I am wondering if this is a known issue with a potential workaround or if there is any additional configuration required when deploying to Google Cloud Functions.

I have tried using the following annotations to ensure that all @Bean methods are properly scanned and registered, but the issue persists:
@SpringBootApplication(scanBasePackages = {"com.example.demo", "com.example.demo.functions"}) @ComponentScan(basePackages = "com.example.demo.functions")

Any guidance or assistance would be greatly appreciated.

0 1 459
1 REPLY 1

Hi @yihangTL,

Welcome to Google Cloud Community!

If you're encountering issues when defining functions in separate files, but not when consolidating them into a single file, this may indicate problems with component scanning or classpath configuration during deployment.

Here are some suggestions that might help you in resolving this issue: 

  • Verify Application Structure: Ensure your project adheres to a standard Maven or Gradle structure. Use the provided starter dependencies for Spring Cloud GCP through Maven or Gradle, as these starters include all necessary dependencies and transitive dependencies for different Spring Cloud GCP modules.
  • Check Deployment Configuration: Confirm that your deployment configuration correctly specifies the function handler and includes all required dependencies. Spring Initializr offers GCP support with auto-configuration for Spring Cloud GCP integrations, simplifying dependency management and build file generation. 
  • Use Sample Applications: Refer to sample applications available on GitHub for guidance. These samples can serve as practical examples for correct setup and configuration.
  • Seek Support: If the issue persists, consider reaching out for support. You can file an issue in the Spring Cloud GCP GitHub repository for bug reports or feature requests.

I hope the above information is helpful.