- java.lang.Object
-
- com.github.zhgzhg.tinydi.TinyDI
-
- All Implemented Interfaces:
java.lang.Runnable
public class TinyDI extends java.lang.Object implements java.lang.RunnableThe context in which dependency injection will happen. This class performs classpath scanning, annotation processing, and dependency injection.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTinyDI.ConfigTinyDI's configuration helper.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.ObjectcomponentFor(java.lang.Class<?> componentClass)Returns instance of an already registered component, taking into account its instantiation scope.java.lang.ObjectcomponentFor(java.lang.String componentName)Returns instance of an already registered component, taking into account its instantiation scope.static TinyDI.Configconfig()Provides builder class through whichTinyDIcan be configured and instantiated.protected java.lang.reflect.Constructor<?>constructorWithLocalClassLoader(java.lang.reflect.Constructor<?> ctor)Returns a Constructor instace, that's been loaded with this component's class loader.ScopeDIinstantiationStrategy(java.lang.Class<?> componentClass)Returns the instantiation strategy recognized by TinyDI for a particular class.ScopeDIinstantiationStrategy(java.lang.Object instance)Returns the instantiation strategy used by TinyDI to create an instance of a particular object.protected java.lang.reflect.MethodmethodWithLocalClassLoader(java.lang.reflect.Method method)Returns a Method instance, that's been loaded with this component's class loader.java.lang.Class<?>registeredComponentClass(java.lang.String componentName)Returns the class definition of an already registered component.java.lang.StringregisteredComponentName(java.lang.Class<?> componentClass)Returns the name of the first found registered component with a specific type.java.util.Set<java.lang.String>registeredComponentNames()Returns a collection of all the registered component names.protected voidregisterProxiedRecords(java.util.List<Recorded> additionalRecords)Registers the passed @Recorded instances.protected java.lang.Class<?>reloadWithLocalClassLoader(java.lang.Class<?> foreignClass)Returns a Class instance, that's been loaded with this component's class loader.voidrun()
-
-
-
Method Detail
-
config
public static TinyDI.Config config()
Provides builder class through whichTinyDIcan be configured and instantiated.- Returns:
- New instance of
TinyDI.Configeventually producing newTinyDIcontext.
-
registerProxiedRecords
protected void registerProxiedRecords(java.util.List<Recorded> additionalRecords)
Registers the passed @Recorded instances. Useful for programmatic DI.- Parameters:
additionalRecords- List of instances to be registered as participants in the DI process.
-
run
public void run()
- Specified by:
runin interfacejava.lang.Runnable
-
constructorWithLocalClassLoader
protected java.lang.reflect.Constructor<?> constructorWithLocalClassLoader(java.lang.reflect.Constructor<?> ctor)
Returns a Constructor instace, that's been loaded with this component's class loader.- Parameters:
ctor- The original Constructor instance to reload- Returns:
- A reloaded with the current class loader Constructor instance.
-
methodWithLocalClassLoader
protected java.lang.reflect.Method methodWithLocalClassLoader(java.lang.reflect.Method method)
Returns a Method instance, that's been loaded with this component's class loader.- Parameters:
method- The original Method instance to reload- Returns:
- A reloaded with the current class loader Method instance.
-
reloadWithLocalClassLoader
protected java.lang.Class<?> reloadWithLocalClassLoader(java.lang.Class<?> foreignClass)
Returns a Class instance, that's been loaded with this component's class loader.- Parameters:
foreignClass- The original Class instance to reload- Returns:
- A reloaded with the current class loader Class instance.
-
registeredComponentNames
public java.util.Set<java.lang.String> registeredComponentNames()
Returns a collection of all the registered component names.- Returns:
- A set with the available component names.
-
registeredComponentClass
public java.lang.Class<?> registeredComponentClass(java.lang.String componentName)
Returns the class definition of an already registered component.- Parameters:
componentName- The name the component was registered with.- Returns:
- The registered
Classinstance or null if such is not found.
-
registeredComponentName
public java.lang.String registeredComponentName(java.lang.Class<?> componentClass)
Returns the name of the first found registered component with a specific type.- Parameters:
componentClass- The component type to search for.- Returns:
- The name the component or null.
-
instantiationStrategy
public ScopeDI instantiationStrategy(java.lang.Class<?> componentClass)
Returns the instantiation strategy recognized by TinyDI for a particular class.- Parameters:
componentClass- The class to look into- Returns:
- Any member of
ScopeDIor null if the strategy cannot be determined.
-
instantiationStrategy
public ScopeDI instantiationStrategy(java.lang.Object instance)
Returns the instantiation strategy used by TinyDI to create an instance of a particular object.- Parameters:
instance- The object to check.- Returns:
- Any member of
ScopeDIor null if the strategy cannot be determined.
-
componentFor
public java.lang.Object componentFor(java.lang.Class<?> componentClass)
Returns instance of an already registered component, taking into account its instantiation scope.- Parameters:
componentClass- The class type of the component to search for and possibly instantiate- Returns:
- A nonnull instance if a class match's been found, otherwise null.
-
componentFor
public java.lang.Object componentFor(java.lang.String componentName)
Returns instance of an already registered component, taking into account its instantiation scope.- Parameters:
componentName- The name of the component to search for and possibly instantiate- Returns:
- A nonnull instance if a name match's been found, otherwise null.
-
-