Skip to content

Commit 807092a

Browse files
committed
Register context as bean
1 parent 61df3b5 commit 807092a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/integrationTest/groovy/annotated/BeanConditionsSpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//file:noinspection UnnecessaryQualifiedReference
12
package annotated
23

34
import com.coditory.quark.context.Context

src/main/java/com/coditory/quark/context/Context.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.stream.Collectors;
1919

2020
import static com.coditory.quark.context.BeanDescriptor.descriptor;
21+
import static com.coditory.quark.context.BeanHolder.holder;
2122
import static com.coditory.quark.context.Preconditions.expectNonNull;
2223
import static com.coditory.quark.context.ResolutionPath.emptyResolutionPath;
2324
import static java.util.Collections.unmodifiableList;
@@ -94,6 +95,12 @@ private Context(String name, Map<BeanDescriptor<?>, List<BeanHolder<?>>> beanHol
9495
requireNonNull(eventBus);
9596
this.name = name;
9697
this.eventBus = eventBus;
98+
// register self
99+
BeanDescriptor<Context> descriptor = descriptor(Context.class);
100+
BeanHolder<Context> holder = holder(descriptor, (ResolutionContext r) -> this, true);
101+
holder.setEventEmitter(eventBus);
102+
beanHolders.put(descriptor, List.of(holder));
103+
// index beans
97104
this.beanHolders = beanHolders;
98105
this.beanHoldersByType = groupBeanCreatorsByType(beanHolders);
99106
this.holders = beanHolders.values().stream()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.coditory.quark.context
2+
3+
import spock.lang.Specification
4+
5+
class BeanDependenciesSpec extends Specification {
6+
def "should register bean with dependency on the dependency context"() {
7+
given:
8+
Context context = Context.builder()
9+
.add(new Baz())
10+
.add(Bar.class, { new Bar(it.get(Context.class)) })
11+
.add(new Foo())
12+
.buildEager()
13+
14+
when:
15+
Bar bar = context.get(Bar)
16+
then:
17+
bar.ctx != null
18+
bar.baz != null
19+
bar.foo != null
20+
and:
21+
bar.ctx.get(Baz) != null
22+
bar.ctx.get(Foo) != null
23+
}
24+
25+
class Baz {}
26+
27+
class Foo {}
28+
29+
class Bar {
30+
Context ctx
31+
Baz baz
32+
Foo foo
33+
34+
Bar(Context ctx) {
35+
this.ctx = ctx
36+
this.baz = ctx.getOrNull(Baz.class)
37+
this.foo = ctx.getOrNull(Foo.class)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)