Azure / azure-functions-java-worker

Java runtime and core types for Microsoft Azure Functions
MIT License
88 stars 54 forks source link

Support dependency injection #324

Closed kulkarnisonia16 closed 1 year ago

zmmille2 commented 4 years ago

Any updates on this issue?

amamounelsayed commented 4 years ago

Thank you so much @zmmille2 for your comment. Can you please share couple of use cases that you will use dependency injection for with Functions?

zmmille2 commented 4 years ago

C# Azure Functions supports DI already. There are a ton of examples of use cases for DI.

For me personally, I'd really love to DI in services I depend on so I can mock them more easily for testing. Right now, I'm looking at using PowerMock, but doing so is definitely a code smell.

So, is DI for Java Azure Functions on the road map at all?

amamounelsayed commented 4 years ago

Yes sir, it is in our road map no ETA yet. We will keep this thread update it.

gmvg commented 3 years ago

Hi, I just wanted to bring the topic of DI for the Azure function. Are there any updates on ETA?

amamounelsayed commented 3 years ago

Thank you so much @gmvg, we have partner team will work on initial work for this feature next week. Hope we will be in a state that we can have the community build on it soon.

andy-clapham commented 3 years ago

Hello, do you have any contact information for your partner team or their repos? At least knowing if they're going to be basing it around a framework would reduce churn later - e.g. will it be Spring?

helayoty commented 3 years ago

Hello, do you have any contact information for your partner team or their repos? At least knowing if they're going to be basing it around a framework would reduce churn later - e.g. will it be Spring?

@andy-clapham For now, you can use Spring Framework to use Azure Function for HTTP requests only (not the bindings.

Here is a sample of how to use it.

parameshjava commented 2 years ago

Hello, do you have any contact information for your partner team or their repos? At least knowing if they're going to be basing it around a framework would reduce churn later - e.g. will it be Spring?

@andy-clapham For now, you can use Spring Framework to use Azure Function for HTTP requests only (not the bindings.

Here is a sample of how to use it.

@helayoty can you please help me, how this component being reused in the given example, since not able to find anywhere the component being injected.

bluebeaker-songhyunkim commented 2 years ago

Hey~ Do we have Dependency Injection for Spring framework now in 2022 June?

jpnaidu07 commented 2 years ago

Hello

It's been more than 2+ years since any update on DI for java azure functions.

kaibocai commented 2 years ago

For now, you can use Spring Cloud Function on Azure for DI on java azure functions.

The DI Hook feature will be roll out in upcoming months, it will allows you to use whatever DI framework you preferred.

kaibocai commented 2 years ago

Hey~ Do we have Dependency Injection for Spring framework now in 2022 June?

Please checkout Spring Cloud Function on Azure

marxxxx commented 2 years ago

The DI Hook feature will be roll out quickly, it will allows you to use whatever DI framework you preferred.

Could you share any details about this feature? How the api will look like and when it can be expected to be available?

kaibocai commented 2 years ago

We are actively working on it and hoping to test and release in upcoming months. Sorry I can not provide a accurate estimated time for now. We will also have documents for it once the feature is out. Thanks.

kaibocai commented 1 year ago

The dependency injection hook PR https://github.com/Azure/azure-functions-java-worker/pull/667 is merged. Please refer to this repo for samples that using google guice and google dagger2 in azure java functions.

Will update here once this feature is fully released.

matthewcorven commented 1 year ago

@kaibocai the dagger2 example is failing at runtime. I have JDK 11 provided by Azul.

 *  Executing task: mvn clean package 

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< com.azfs.example:dagger-function >------------------
[INFO] Building Azure Java Functions 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.1.0:clean (default-clean) @ dagger-function ---
[INFO] Deleting C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ dagger-function ---
[INFO] Copying 1 resource
[INFO] 
[INFO] --- compiler:3.8.1:compile (default-compile) @ dagger-function ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target\classes
[INFO] /C:/Users/mcorven/git/azure-function-java-dependency-injection-samples/dagger-function/src/main/java/com/azfs/dihook/MyFunctionInstanceInjector.java: C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\src\main\java\com\azfs\dihook\MyFunctionInstanceInjector.java uses unchecked or unsafe operations.
[INFO] /C:/Users/mcorven/git/azure-function-java-dependency-injection-samples/dagger-function/src/main/java/com/azfs/dihook/MyFunctionInstanceInjector.java: Recompile with -Xlint:unchecked for details.
[INFO]
[INFO] --- resources:3.3.0:testResources (default-testResources) @ dagger-function ---
[INFO] skip non existing resourceDirectory C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\src\test\resources
[INFO]
[INFO] --- compiler:3.8.1:testCompile (default-testCompile) @ dagger-function ---
[INFO] No sources to compile
[INFO]
[INFO] --- surefire:3.0.0-M8:test (default-test) @ dagger-function ---
[INFO] No tests to run.
[INFO] 
[INFO] --- jar:3.3.0:jar (default-jar) @ dagger-function ---
[INFO] Building jar: C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target\dagger-function-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- azure-functions:1.21.0:package (package-functions) @ dagger-function ---
[INFO] Java home : C:\Users\mcorven\bin\zulu11.62.17-ca-jdk11.0.18-win_x64
[INFO] Artifact compile version : 1.8
[INFO]
[INFO] Step 1 of 8: Searching for Azure Functions entry points
[INFO] Reflections took 69 ms to scan 5 urls, producing 3 keys and 3 values
[INFO] 1 Azure Functions entry point(s) found.
[INFO]
[INFO] Step 2 of 8: Generating Azure Functions configurations
[INFO] Generation done.
[INFO]
[INFO] Step 3 of 8: Validating generated configurations
[INFO] Validation done.
[INFO] 
[INFO] Step 4 of 8: Copying/creating host.json
[INFO] Successfully saved to C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target\azure-functions\dagger-function-20221101192648097\host.json
[INFO]
[INFO] Step 5 of 8: Copying/creating local.settings.json
[INFO] Successfully saved to C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target\azure-functions\dagger-function-20221101192648097\local.settings.json
[INFO]
[INFO] Step 6 of 8: Saving configurations to function.json
[INFO] Starting processing function: HttpExample
[INFO] Successfully saved to C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target\azure-functions\dagger-function-20221101192648097\HttpExample\function.json
[INFO]
[INFO] Step 7 of 8: Copying JARs to staging directory C:\Users\mcorven\git\azure-function-java-dependency-injection-samples\dagger-function\target\azure-functions\dagger-function-20221101192648097
[INFO] Copied successfully.
[INFO] Step 8 of 8: Installing function extensions if needed
[INFO] Extension bundle specified, skip install extension
[INFO] Successfully built Azure Functions.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.240 s
[INFO] Finished at: 2023-02-16T10:54:01-05:00
[INFO] ------------------------------------------------------------------------
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: func host start 

Azure Functions Core Tools
Core Tools Version:       4.0.4915 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.14.0.19631

Functions:

        HttpExample: [GET,POST] http://localhost:7071/api/HttpExample

For detailed output, run func with --verbose flag.
[2023-02-16T15:54:09.435Z] Listening for transport dt_socket at address: 5005
[2023-02-16T15:54:11.570Z] Worker process started and initialized.
[2023-02-16T15:54:14.015Z] Host lock lease acquired by instance ID '000000000000000000000000B528B912'.
[2023-02-16T15:54:21.117Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=35a6b6a5-8ecf-4917-8a06-f2b5998d2c1b)
[2023-02-16T15:54:21.212Z] Executed 'Functions.HttpExample' (Failed, Id=35a6b6a5-8ecf-4917-8a06-f2b5998d2c1b, Duration=96ms)
[2023-02-16T15:54:21.213Z] System.Private.CoreLib: Exception while executing function: Functions.HttpExample. System.Private.CoreLib: Result: Failure
Exception: NoSuchMethodException: com.azfs.Function.<init>()
Stack: java.lang.InstantiationException: com.azfs.Function
[2023-02-16T15:54:21.214Z]      at java.base/java.lang.Class.newInstance(Class.java:571)
[2023-02-16T15:54:21.215Z]      at com.microsoft.azure.functions.worker.broker.EnhancedJavaMethodExecutorImpl.lambda$execute$1(EnhancedJavaMethodExecutorImpl.java:22)
[2023-02-16T15:54:21.216Z]      at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:20)
[2023-02-16T15:54:21.217Z]      at com.microsoft.azure.functions.worker.broker.EnhancedJavaMethodExecutorImpl.execute(EnhancedJavaMethodExecutorImpl.java:22)      
[2023-02-16T15:54:21.217Z]      at com.microsoft.azure.functions.worker.chain.FunctionExecutionMiddleware.invoke(FunctionExecutionMiddleware.java:19)
[2023-02-16T15:54:21.218Z]      at com.microsoft.azure.functions.worker.chain.InvocationChain.doNext(InvocationChain.java:21)
[2023-02-16T15:54:21.219Z]      at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:80)
[2023-02-16T15:54:21.219Z]      at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:37)
[2023-02-16T15:54:21.220Z]      at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:12)
[2023-02-16T15:54:21.221Z]      at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:44)
[2023-02-16T15:54:21.222Z]      at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:93)
[2023-02-16T15:54:21.223Z]      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
[2023-02-16T15:54:21.223Z]      at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[2023-02-16T15:54:21.224Z]      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[2023-02-16T15:54:21.225Z]      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[2023-02-16T15:54:21.226Z]      at java.base/java.lang.Thread.run(Thread.java:829)
[2023-02-16T15:54:21.227Z] Caused by: java.lang.NoSuchMethodException: com.azfs.Function.<init>()
[2023-02-16T15:54:21.227Z]      at java.base/java.lang.Class.getConstructor0(Class.java:3349)
[2023-02-16T15:54:21.228Z]      at java.base/java.lang.Class.newInstance(Class.java:556)
[2023-02-16T15:54:21.229Z]      ... 15 more
[2023-02-16T15:54:21.230Z] .
kaibocai commented 1 year ago

Hi @matthewcorven , thanks for trying out this new feature. Currently we haven't relased the core tools that include this feature, it should be released in next one week or two. (Add @VpOfEngineering and @MadhuraBharadwaj-MSFT for awareness.) However, this feature is already avaliable on azure portal. So can you try deploy your function app to azure and try it out on the cloud. Thank you.

matthewcorven commented 1 year ago

Thanks so much for the quick reply @kaibocai! Eagerly awaiting the release.

afrancoc2000 commented 1 year ago

Same issue using Guice can't wait for the release! thank you so much 😄

kaibocai commented 1 year ago

Hello! We have the latest core tools that support this feature released, you can find it at this download page https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=v4%2Cwindows%2Ccsharp%2Cportal%2Cbash#install-the-azure-functions-core-tools

Please make sure when runnning on local it's version matches below

image

If you want to use SpringBoot as your DI framework, we have a new release of spring cloud function that use this feature to support better coding experience (adding @tzolov). You can find more docs here

ghost commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.