Skip to content

Commit 385d93a

Browse files
authored
add dependecy injection examples with Gooogle Juice and Dagger2 (#698)
1 parent 79427b3 commit 385d93a

24 files changed

Lines changed: 539 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Dependency Injection Examples for Azure Java Functions
2+
3+
This rep contains examples that integrate Google Dagger2 and Google Juice with Azure Java Functions.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# azure-function-guice
2+
Integration Google Dagger2 with Azure Functions
3+
4+
Utilize [com.microsoft.azure.functions.spi.inject.FunctionInstanceInjector](https://github.com/Azure/azure-functions-java-additions/blob/dev/azure-functions-java-spi/src/main/java/com/microsoft/azure/functions/spi/inject/FunctionInstanceInjector.java) provided by azure functions java worker to integrate Google Dagger2 framework into Azure Java Function.
5+
6+
## Local Setup
7+
1. Clone the repo
8+
2. Enter corresponding directory and run `mvn clean package` to build the project
9+
3. Run `mvn azure-functions:run` to run the project on local.
10+
Local example:
11+
12+
![img.png](img.png)
13+
14+
![img_1.png](img_1.png)
15+
4. Run `mvn azure-functions:deploy` to deploy the function app, for more info about deploy azure function java app please refer to [deploy java function app](https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-java?tabs=bash%2Cazure-cli%2Cbrowser#deploy-the-function-project-to-azure)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": "2.0",
3+
"extensionBundle": {
4+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
5+
"version": "[3.*, 4.0.0)"
6+
}
7+
}
5.92 KB
Loading
144 KB
Loading
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.azfs.example</groupId>
6+
<artifactId>dagger-function</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Azure Java Functions</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<java.version>1.8</java.version>
15+
<azure.functions.maven.plugin.version>1.21.0</azure.functions.maven.plugin.version>
16+
<azure.functions.java.library.version>2.0.1</azure.functions.java.library.version>
17+
<functionAppName>dagger-function-20221101192648097</functionAppName>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.microsoft.azure.functions</groupId>
23+
<artifactId>azure-functions-java-library</artifactId>
24+
<version>${azure.functions.java.library.version}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.google.dagger</groupId>
29+
<artifactId>dagger</artifactId>
30+
<version>2.44</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.microsoft.azure.functions</groupId>
35+
<artifactId>azure-functions-java-spi</artifactId>
36+
<version>1.0.0</version>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<!-- Test -->
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter</artifactId>
44+
<version>5.4.2</version>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.mockito</groupId>
50+
<artifactId>mockito-core</artifactId>
51+
<version>2.23.4</version>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.8.1</version>
62+
<configuration>
63+
<source>${java.version}</source>
64+
<target>${java.version}</target>
65+
<encoding>${project.build.sourceEncoding}</encoding>
66+
<annotationProcessorPaths>
67+
<path>
68+
<groupId>com.google.dagger</groupId>
69+
<artifactId>dagger-compiler</artifactId>
70+
<version>2.16</version>
71+
</path>
72+
</annotationProcessorPaths>
73+
</configuration>
74+
</plugin>
75+
<plugin>
76+
<groupId>com.microsoft.azure</groupId>
77+
<artifactId>azure-functions-maven-plugin</artifactId>
78+
<version>${azure.functions.maven.plugin.version}</version>
79+
<configuration>
80+
<!-- function app name -->
81+
<appName>${functionAppName}</appName>
82+
<!-- function app resource group -->
83+
<resourceGroup>java-functions-group</resourceGroup>
84+
<!-- function app service plan name -->
85+
<appServicePlanName>java-functions-app-service-plan</appServicePlanName>
86+
<!-- function app region-->
87+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values -->
88+
<region>westus</region>
89+
<!-- function pricingTier, default to be consumption if not specified -->
90+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values -->
91+
<!-- <pricingTier></pricingTier> -->
92+
<!-- Whether to disable application insights, default is false -->
93+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights-->
94+
<!-- <disableAppInsights></disableAppInsights> -->
95+
<runtime>
96+
<!-- runtime os, could be windows, linux or docker-->
97+
<os>windows</os>
98+
<javaVersion>8</javaVersion>
99+
</runtime>
100+
<appSettings>
101+
<property>
102+
<name>FUNCTIONS_EXTENSION_VERSION</name>
103+
<value>~4</value>
104+
</property>
105+
</appSettings>
106+
</configuration>
107+
<executions>
108+
<execution>
109+
<id>package-functions</id>
110+
<goals>
111+
<goal>package</goal>
112+
</goals>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
<!--Remove obj folder generated by .NET SDK in maven clean-->
117+
<plugin>
118+
<artifactId>maven-clean-plugin</artifactId>
119+
<version>3.1.0</version>
120+
<configuration>
121+
<filesets>
122+
<fileset>
123+
<directory>obj</directory>
124+
</fileset>
125+
</filesets>
126+
</configuration>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.azfs;
2+
3+
import com.azfs.model.Communicator;
4+
import com.microsoft.azure.functions.ExecutionContext;
5+
import com.microsoft.azure.functions.HttpMethod;
6+
import com.microsoft.azure.functions.HttpRequestMessage;
7+
import com.microsoft.azure.functions.HttpResponseMessage;
8+
import com.microsoft.azure.functions.HttpStatus;
9+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
10+
import com.microsoft.azure.functions.annotation.FunctionName;
11+
import com.microsoft.azure.functions.annotation.HttpTrigger;
12+
13+
import javax.inject.Inject;
14+
import java.util.Optional;
15+
16+
/**
17+
* Azure Functions with HTTP Trigger.
18+
*/
19+
public class Function {
20+
/**
21+
* This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
22+
* 1. curl -d "HTTP Body" {your host}/api/HttpExample
23+
* 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
24+
*/
25+
26+
private final Communicator communicator;
27+
28+
@Inject
29+
public Function(Communicator communicator) {
30+
this.communicator = communicator;
31+
}
32+
33+
@FunctionName("HttpExample")
34+
public HttpResponseMessage run(
35+
@HttpTrigger(
36+
name = "req",
37+
methods = {HttpMethod.GET, HttpMethod.POST},
38+
authLevel = AuthorizationLevel.ANONYMOUS)
39+
HttpRequestMessage<Optional<String>> request,
40+
final ExecutionContext context) {
41+
context.getLogger().info("Java HTTP trigger processed a request.");
42+
43+
// Parse query parameter
44+
final String query = request.getQueryParameters().get("name");
45+
final String name = request.getBody().orElse(query);
46+
47+
communicator.communicate(context);
48+
49+
if (name == null) {
50+
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
51+
} else {
52+
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
53+
}
54+
}
55+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.azfs.component;
2+
3+
import com.azfs.Function;
4+
import com.azfs.module.FunctionModule;
5+
import dagger.Component;
6+
7+
@Component(modules = FunctionModule.class)
8+
public interface FunctionComponent {
9+
Function buildFunction();
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.azfs.dihook;
2+
3+
import com.azfs.component.DaggerFunctionComponent;
4+
import com.microsoft.azure.functions.spi.inject.FunctionInstanceInjector;
5+
6+
public class MyFunctionInstanceInjector implements FunctionInstanceInjector {
7+
@Override
8+
public <T> T getInstance(Class<T> aClass) throws Exception {
9+
return (T) DaggerFunctionComponent.create().buildFunction();
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.azfs.model;
2+
3+
import com.microsoft.azure.functions.ExecutionContext;
4+
5+
public class Communicator {
6+
private final String id;
7+
8+
public Communicator(String id) {
9+
this.id = id;
10+
}
11+
12+
public void communicate(ExecutionContext context){
13+
context.getLogger().info("Message sent out from injected communicator :) ");
14+
//add your own logics ...
15+
}
16+
}

0 commit comments

Comments
 (0)