Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with @Bean Annotation in Separate Java Files When Deploying to Google Cloud Functions #1179

Open
yihangYH opened this issue Sep 2, 2024 · 2 comments
Milestone

Comments

@yihangYH
Copy link

yihangYH commented Sep 2, 2024

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 'hi'

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.

@olegz
Copy link
Contributor

olegz commented Sep 3, 2024

Does the file containing @bean annotations is also annotated with @Configurationi or @EnableAutoConfiguration? Do you have some sample code you can show?

@yihangYH
Copy link
Author

yihangYH commented Sep 4, 2024

Does the file containing @bean annotations is also annotated with @Configurationi or @EnableAutoConfiguration? Do you have some sample code you can show?

Yes, please check below code. For the same code, I managed to run successfully in aws lambda

package com.example.demo.functions;
import com.example.demo.model.RequestModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Configuration
public class Request {
    private static final Logger logger = LoggerFactory.getLogger(Request.class);


    @Bean
    public Function<RequestModel, String> hi() {
        return requestModel -> {
            logger.info("Hi function called with name: {}", requestModel.getName());
            String name = requestModel.getName();
            return "Hi " + name;
        };
    }

    @Bean
    public Function<RequestModel, String> goodbye() {
        return requestModel -> {
            logger.info("Goodbye function called with name: {}", requestModel.getName());
            String name = requestModel.getName();
            return "goodbye " + name;
        };
    }
}

@olegz olegz added this to the 4.1.4 milestone Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants