Skip to content

Commit e5189da

Browse files
committed
#54 - Add support for request scope injection for Jex Context
1 parent 07876ad commit e5189da

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/RequestScope.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
*/
1010
class RequestScope {
1111

12+
private static final String JEX_CONTEXT = "io.avaje.jex.Context";
1213
private static final String JAVALIN_CONTEXT = "io.javalin.http.Context";
1314
private static final String HELIDON_REQ = "io.helidon.webserver.ServerRequest";
1415
private static final String HELIDON_RES = "io.helidon.webserver.ServerResponse";
1516
private static final Map<String, Handler> TYPES = new HashMap<>();
1617

1718
static {
19+
TYPES.put(JEX_CONTEXT, new Jex());
1820
TYPES.put(JAVALIN_CONTEXT, new Javalin());
1921
TYPES.put(HELIDON_REQ, new Helidon());
2022
TYPES.put(HELIDON_RES, new Helidon());
@@ -60,6 +62,33 @@ interface Handler {
6062
String argumentName(String paramType);
6163
}
6264

65+
/**
66+
* Jex support for request scoping/BeanFactory.
67+
*/
68+
private static class Jex implements Handler {
69+
70+
@Override
71+
public void factoryInterface(Append writer, String parentType) {
72+
writer.append("BeanFactory<%s, %s>", parentType, "Context");
73+
}
74+
75+
@Override
76+
public void addImports(Set<String> importTypes) {
77+
importTypes.add(Constants.BEAN_FACTORY);
78+
importTypes.add(JEX_CONTEXT);
79+
}
80+
81+
@Override
82+
public void writeCreateMethod(Append writer, String parentType) {
83+
writer.append(" public %s create(Context context) {", parentType).eol();
84+
}
85+
86+
@Override
87+
public String argumentName(String paramType) {
88+
return "context";
89+
}
90+
}
91+
6392
/**
6493
* Javalin support for request scoping/BeanFactory.
6594
*/

inject-test/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
<scope>provided</scope>
4040
</dependency>
4141

42+
<dependency>
43+
<groupId>io.avaje</groupId>
44+
<artifactId>avaje-jex</artifactId>
45+
<version>0.1</version>
46+
<scope>test</scope>
47+
</dependency>
48+
4249
<dependency>
4350
<groupId>io.javalin</groupId>
4451
<artifactId>javalin</artifactId>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example.request;
2+
3+
4+
import io.avaje.http.api.Controller;
5+
import io.avaje.jex.Context;
6+
7+
import javax.inject.Inject;
8+
9+
@Controller
10+
public class JexController {
11+
12+
@Inject
13+
AService service;
14+
15+
@Inject
16+
Context context;
17+
18+
public String get() {
19+
return "hi " + context.toString() + service.hi();
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.example.request;
2+
3+
import io.avaje.inject.SystemContext;
4+
import io.avaje.jex.Context;
5+
import org.junit.jupiter.api.Test;
6+
import org.mockito.Mockito;
7+
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
import static org.junit.jupiter.api.Assertions.assertSame;
11+
12+
class JexControllerTest {
13+
14+
@Test
15+
void uses_factory_taking_context() {
16+
17+
assertNull(SystemContext.getBean(JexController.class));
18+
19+
final JexController$factory factory = SystemContext.getBean(JexController$factory.class);
20+
final JexController jexController = factory.create(Mockito.mock(Context.class));
21+
22+
assertNotNull(jexController);
23+
assertSame(factory.service0, jexController.service);
24+
assertSame(jexController.service, SystemContext.getBean(AService.class));
25+
}
26+
}

0 commit comments

Comments
 (0)