Skip to content

Commit edfd096

Browse files
authored
Merge pull request #334 from SentryMan/hooks
Now can add scope hooks on the BeanScope builder
2 parents 52fe9a7 + b58363e commit edfd096

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ default <D> BeanScopeBuilder provideDefault(Type type, Supplier<D> provider) {
199199
*/
200200
<D> BeanScopeBuilder provideDefault(@Nullable String name, Type type, Supplier<D> provider);
201201

202+
/**
203+
* Adds hooks that will execute after this scope is built.
204+
*
205+
* @param runnables the PostConstruct hooks to add to the bean scope
206+
*/
207+
BeanScopeBuilder addPostConstructHooks(Runnable... runnables);
208+
209+
/**
210+
* Adds hooks that will execute before this scope is destroyed.
211+
*
212+
* @param closables the PreDestroy hooks to add to the bean scope
213+
*/
214+
BeanScopeBuilder addPreDestroyHooks(AutoCloseable... closables);
215+
216+
202217
/**
203218
* Set the ClassLoader to use when loading modules.
204219
*

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
2626
@SuppressWarnings("rawtypes")
2727
private final List<EnrichBean> enrichBeans = new ArrayList<>();
2828
private final Set<Module> includeModules = new LinkedHashSet<>();
29+
private final List<Runnable> postConstructList = new ArrayList<>();
30+
private final List<AutoCloseable> preDestroyList = new ArrayList<>();
2931
private BeanScope parent;
3032
private boolean parentOverride = true;
3133
private boolean shutdownHook;
@@ -106,6 +108,18 @@ public <D> BeanScopeBuilder provideDefault(String name, Type type, Supplier<D> s
106108
return this;
107109
}
108110

111+
@Override
112+
public BeanScopeBuilder addPostConstructHooks(Runnable... postConstructRunnable) {
113+
Collections.addAll(this.postConstructList, postConstructRunnable);
114+
return this;
115+
}
116+
117+
@Override
118+
public BeanScopeBuilder addPreDestroyHooks(AutoCloseable... closables) {
119+
Collections.addAll(this.preDestroyList, closables);
120+
return this;
121+
}
122+
109123
@Override
110124
public BeanScopeBuilder classLoader(ClassLoader classLoader) {
111125
this.classLoader = classLoader;
@@ -130,6 +144,7 @@ public BeanScopeBuilder.ForTesting mock(Class<?> type) {
130144
return mock(type, null, null);
131145
}
132146

147+
@Override
133148
public BeanScopeBuilder.ForTesting mock(Class<?> type, String name) {
134149
return mock(type, name, null);
135150
}
@@ -149,7 +164,8 @@ public BeanScopeBuilder.ForTesting spy(Class<?> type) {
149164
return spy(type, null, null);
150165
}
151166

152-
public BeanScopeBuilder.ForTesting spy(Class<?> type, String name) {
167+
@Override
168+
public BeanScopeBuilder.ForTesting spy(Class<?> type, String name) {
153169
return spy(type, name, null);
154170
}
155171

@@ -206,6 +222,9 @@ public BeanScope build() {
206222
for (Module factory : factoryOrder.factories()) {
207223
factory.build(builder);
208224
}
225+
226+
postConstructList.forEach(builder::addPostConstruct);
227+
preDestroyList.forEach(builder::addPreDestroy);
209228
return builder.build(shutdownHook, start);
210229
}
211230

0 commit comments

Comments
 (0)