From 3b01f711d12f5cc7a32bfe6fc453f6032fec8549 Mon Sep 17 00:00:00 2001 From: Erhan Bagdemir Date: Fri, 29 Nov 2024 17:04:08 +0100 Subject: [PATCH] Minor refactorings --- .../net/reevik/mikron/ioc/ManagedInstance.java | 13 ++++++------- .../java/net/reevik/mikron/ioc/MikronContext.java | 14 +++++++------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/reevik/mikron/ioc/ManagedInstance.java b/src/main/java/net/reevik/mikron/ioc/ManagedInstance.java index dfd582b..edebbb2 100644 --- a/src/main/java/net/reevik/mikron/ioc/ManagedInstance.java +++ b/src/main/java/net/reevik/mikron/ioc/ManagedInstance.java @@ -28,13 +28,12 @@ public class ManagedInstance { private static final Logger LOG = LoggerFactory.getLogger(ManagedInstance.class); private final Object instance; - private final String managedInstanceName; + private final String instanceName; private final MikronContext context; - public ManagedInstance(Object instance, - String managedInstanceName, MikronContext context) { + public ManagedInstance(Object instance, String managedInstanceName, MikronContext context) { this.instance = instance; - this.managedInstanceName = managedInstanceName; + this.instanceName = managedInstanceName; this.context = context; } @@ -46,7 +45,7 @@ public void wire() { } public void configSetup() { - configSetup(managedInstanceName); + configSetup(instanceName); } public void configSetup(String configurationSourceKey) { @@ -86,7 +85,7 @@ public Object getInstance() { return instance; } - public String getManagedInstanceName() { - return managedInstanceName; + public String getInstanceName() { + return instanceName; } } diff --git a/src/main/java/net/reevik/mikron/ioc/MikronContext.java b/src/main/java/net/reevik/mikron/ioc/MikronContext.java index 051822e..d9ee977 100644 --- a/src/main/java/net/reevik/mikron/ioc/MikronContext.java +++ b/src/main/java/net/reevik/mikron/ioc/MikronContext.java @@ -86,6 +86,7 @@ public void register(Object instance, String name) { managedInstances.clear(); managedInstances.put(name, new ManagedInstance(instance, name, this)); initializeContext(); + initializeWiring(); initializeConfigurations(); postConstructAll(); } @@ -99,19 +100,20 @@ private void initializeContext() { registerSelf(); for (var annotationResource : classpathResourceRepository.findClassesBy(Managed.class)) { var componentName = getName(annotationResource); - var propBasedInstanceCreation = createInstancePerPropertyFile(annotationResource, - componentName); + var propBasedInstanceCreation = createInstanceByPropertyFile(annotationResource, componentName); // It is possible that there is no configuration file at all, so we need to instantiate the // component by name. if (!propBasedInstanceCreation) { managedInstances.put(componentName, initObject(annotationResource, componentName)); } } + } + + private void initializeWiring() { managedInstances.values().forEach(ManagedInstance::wire); } - private boolean createInstancePerPropertyFile(ManagedDefinition annotationResource, - String componentName) { + private boolean createInstanceByPropertyFile(ManagedDefinition annotationResource, String componentName) { var propBasedInstanceCreation = false; for (var propFile : propertiesRepository.getPropertyClassNames()) { if (propFile.startsWith(componentName) && !managedInstances.containsKey(propFile)) { @@ -160,9 +162,7 @@ private ManagedInstance instantiateByDefault(ManagedDefinition annotati try { var clazz = annotationResource.clazz(); var constructor = clazz.getConstructor(); - var managedInstance = new ManagedInstance(constructor.newInstance(), name, this); - executeIfAnnotated(Initialize.class, managedInstance); - return managedInstance; + return new ManagedInstance(constructor.newInstance(), name, this); } catch (Exception e) { throw new RuntimeException(e); }