From 812a1764985e35f1a548be30d9ae3aeb3e3d4f80 Mon Sep 17 00:00:00 2001 From: ogesaku Date: Sat, 27 Apr 2024 16:59:37 +0200 Subject: [PATCH] Better eager context --- .../com/coditory/quark/context/Context.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/coditory/quark/context/Context.java b/src/main/java/com/coditory/quark/context/Context.java index 45a20f0..09c4db5 100644 --- a/src/main/java/com/coditory/quark/context/Context.java +++ b/src/main/java/com/coditory/quark/context/Context.java @@ -56,13 +56,23 @@ static Context createEager(String name, Set> beanHolders, Set, List>> holders = ContextResolver.resolve(beanHolders, properties); Context context = new Context(name, holders, properties, eventBus); context.init(initBeanHolders); - holders.forEach((descriptor, creator) -> { - if (descriptor.name() != null) { - context.get(descriptor.type(), descriptor.name()); - } else { - context.getAll(descriptor.type()); + try { + holders.forEach((descriptor, creator) -> { + if (descriptor.name() != null) { + context.get(descriptor.type(), descriptor.name()); + } else if (descriptor.type() != Object.class) { + // Object is too generic, can be skipped for more readable exceptions + context.getAll(descriptor.type()); + } + }); + } catch (Throwable exception) { + try { + context.close(); + } catch (Throwable closeException) { + log.warn("Could not close context", closeException); } - }); + throw exception; + } log.info("Created eager context in {}", totalTimer.measureAndFormat()); eventBus.emit(new ContextEvent.ContextPostCreateEvent()); return context;