Skip to content

Commit 5a41033

Browse files
committed
Add PostInit annotation, fix list dependency handler
1 parent bad704b commit 5a41033

File tree

18 files changed

+205
-95
lines changed

18 files changed

+205
-95
lines changed

src/integrationTest/groovy/annotated/BeanLifecycleSpec.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,35 @@ class BeanLifecycleSpec extends Specification {
1111
Context context = Context.scanPackage(ConfigLifecycle)
1212
then:
1313
annotated.samples.beans_lifecycle.Bar.initialized == false
14+
annotated.samples.beans_lifecycle.Bar.postInitialized == false
1415
annotated.samples.beans_lifecycle.Baz.initialized == false
16+
annotated.samples.beans_lifecycle.Baz.postInitialized == false
1517

1618
when:
1719
context.get(annotated.samples.beans_lifecycle.Bar)
1820
then:
1921
annotated.samples.beans_lifecycle.Bar.initialized
22+
annotated.samples.beans_lifecycle.Bar.postInitialized
2023

2124
when:
2225
context.get(annotated.samples.beans_lifecycle.Baz)
2326
then:
2427
annotated.samples.beans_lifecycle.Baz.initialized
28+
annotated.samples.beans_lifecycle.Baz.postInitialized
2529
}
2630

2731
def "should initialize eager bean right after context is built"() {
2832
when:
2933
Context.scanPackage(EagerBar)
3034
then:
3135
EagerBar.initialized == true
36+
EagerBar.postInitialized == true
3237
}
3338

3439
def "should finalize beans when closing the context"() {
3540
given:
3641
Context context = Context.builder()
3742
.scanPackage(ConfigLifecycle)
38-
.registerConfigurationBeans()
3943
.build()
4044
and:
4145
ConfigLifecycle config = context.get(ConfigLifecycle)

src/integrationTest/groovy/annotated/ConfigurationRegistrationSpec.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package annotated
33
import annotated.samples.config_init.ConfigEagerInit
44
import annotated.samples.config_init.ConfigInit
55
import annotated.samples.config_inject.ConfigInject
6+
import annotated.samples.config_post_init.ConfigPostInit
67
import com.coditory.quark.context.Context
78
import spock.lang.Specification
89

@@ -65,4 +66,16 @@ class ConfigurationRegistrationSpec extends Specification {
6566
ConfigEagerInit.initialized == true
6667
ConfigEagerInit.bar != null
6768
}
69+
70+
def "should post initialize configuration with dependency on internal bean"() {
71+
when:
72+
Context.builder()
73+
.scanPackage(ConfigPostInit)
74+
.build()
75+
.get(annotated.samples.config_post_init.Foo)
76+
then:
77+
ConfigPostInit.postInitialized == true
78+
ConfigPostInit.bar != null
79+
ConfigPostInit.baz != null
80+
}
6881
}

src/integrationTest/groovy/annotated/samples/beans_lifecycle/Bar.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ package annotated.samples.beans_lifecycle
33
import com.coditory.quark.context.annotations.Bean
44
import com.coditory.quark.context.annotations.Close
55
import com.coditory.quark.context.annotations.Init
6+
import com.coditory.quark.context.annotations.PostInit
67

78
@Bean
89
class Bar {
910
static boolean initialized = false
11+
static boolean postInitialized = false
1012
static boolean finalized = false
1113

1214
@Init
1315
void init() {
1416
Bar.initialized = true
1517
}
1618

19+
@PostInit
20+
void postInit() {
21+
Bar.postInitialized = true
22+
}
23+
1724
@Close
1825
void close() {
1926
Bar.finalized = true

src/integrationTest/groovy/annotated/samples/beans_lifecycle/Baz.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package annotated.samples.beans_lifecycle
22

33
import com.coditory.quark.context.annotations.Close
44
import com.coditory.quark.context.annotations.Init
5+
import com.coditory.quark.context.annotations.PostInit
56

67
class Baz {
78
static boolean initialized = false
9+
static boolean postInitialized = false
810
static boolean finalized = false
911
final Bar bar
1012

@@ -17,6 +19,11 @@ class Baz {
1719
Baz.initialized = true
1820
}
1921

22+
@PostInit
23+
void postInit() {
24+
Baz.postInitialized = true
25+
}
26+
2027
@Close
2128
void close() {
2229
Baz.finalized = true

src/integrationTest/groovy/annotated/samples/beans_lifecycle/EagerBar.groovy

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ package annotated.samples.beans_lifecycle
22

33
import com.coditory.quark.context.annotations.Bean
44
import com.coditory.quark.context.annotations.Close
5-
import com.coditory.quark.context.annotations.Init;
5+
import com.coditory.quark.context.annotations.Init
6+
import com.coditory.quark.context.annotations.PostInit;
67

78
@Bean(eager = true)
89
class EagerBar {
910
static boolean initialized = false
11+
static boolean postInitialized = false
1012
static boolean finalized = false
1113

1214
@Init
1315
void init() {
1416
EagerBar.initialized = true
1517
}
1618

19+
@PostInit
20+
void postInit() {
21+
EagerBar.postInitialized = true
22+
}
23+
1724
@Close
1825
void close() {
1926
EagerBar.finalized = true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package annotated.samples.config_post_init
2+
3+
import com.coditory.quark.context.annotations.Bean
4+
5+
@Bean
6+
class Bar {
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package annotated.samples.config_post_init
2+
3+
class Baz {
4+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package annotated.samples.config_post_init
2+
3+
4+
import com.coditory.quark.context.annotations.Bean
5+
import com.coditory.quark.context.annotations.Configuration
6+
import com.coditory.quark.context.annotations.PostInit
7+
8+
@Configuration
9+
class ConfigPostInit {
10+
static boolean postInitialized = false
11+
static Baz baz
12+
static Bar bar
13+
14+
@PostInit
15+
void postInit(Baz baz, Bar bar) {
16+
ConfigPostInit.postInitialized = true
17+
ConfigPostInit.baz = baz
18+
ConfigPostInit.bar = bar
19+
}
20+
21+
@Bean
22+
Baz baz() {
23+
return new Baz()
24+
}
25+
26+
@Bean
27+
Foo foo() {
28+
return new Foo()
29+
}
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package annotated.samples.config_post_init
2+
3+
class Foo {
4+
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import static com.coditory.quark.context.BeanFinalizer.closeBean;
1212
import static com.coditory.quark.context.BeanInitializer.initializeBean;
13+
import static com.coditory.quark.context.BeanPostInitializer.postInitializeBean;
14+
import static com.coditory.quark.context.ResolutionPath.emptyResolutionPath;
1315
import static java.util.Objects.requireNonNull;
1416
import static java.util.stream.Collectors.toSet;
1517

@@ -30,6 +32,7 @@ static <T> BeanHolder<T> holder(BeanDescriptor<T> descriptor, BeanCreator<T> cre
3032
private EventEmitter eventEmitter;
3133
private T bean;
3234
private boolean initialized = false;
35+
private boolean postInitialized = false;
3336
private boolean closed = false;
3437

3538
private BeanHolder(BeanDescriptor<T> descriptor, BeanCreator<T> creator, boolean eager) {
@@ -54,6 +57,20 @@ public boolean isInitialized() {
5457
return initialized;
5558
}
5659

60+
boolean isPostInitialized() {
61+
return postInitialized;
62+
}
63+
64+
void postInitialize(Context context) {
65+
if (postInitialized) return;
66+
if (bean == null) {
67+
throw new IllegalStateException("Expected bean to exist before post initialization");
68+
}
69+
ResolutionContext resolutionContext = new ResolutionContext(context, ResolutionPath.of(descriptor));
70+
postInitialized = true;
71+
postInitializeBean(bean, descriptor, resolutionContext);
72+
}
73+
5774
Class<T> getBeanType() {
5875
return descriptor.type();
5976
}
@@ -100,9 +117,6 @@ public T get(ResolutionContext context) {
100117
private void createBean(ResolutionContext context) {
101118
Timer timer = Timer.start();
102119
bean = creator.create(context);
103-
if (bean == null) {
104-
throw new ContextException("Expected non-null bean: " + descriptor);
105-
}
106120
log.debug("Created bean {} in {}", descriptor.toShortString(), timer.measureAndFormat());
107121
if (!initialized) {
108122
initializeBean(bean, descriptor, context);

0 commit comments

Comments
 (0)