Amazing Project - Feel free to learn!

Report a Bug · Request a Feature . Ask a Question

IoC Container

As a curious and passionate software developer, I always wondered how Spring Core works. To better understand its inner workings, I created this project that mainly uses Java Reflection and supports most annotations like @Component, @Autowire, @Bean, @PostConstructor, and more. Manage instantiated Components by an ApplicationContext (reloading, updating, getting) Bean like Spring Core. Handle custom components and bean scopes by using proxies. Enrich Components with aspects. And automatically resolve dependencies

Table of Contents

General Information

A library that replicates the functionality of the Spring Core framework. It supports most annotations such as @Component, @Service, @Repository, @Configuration, @Autowire, @Qualifier, @PostConstruct, @PreDestroy... In addition, there are 3 modes for a bean (SINGLETON, PROTOTYPE, PROXY). Building from scratch helps us understand how IoC Containers work under the hood.

It’s really hard and complicated to know how spring core works. Because it is composed of many modules and is produced by many people. I try to integrate many components into one and make it simple by following these steps:

  • Finding and loading all classes available in the project and pushing them into a set collection. Pay attention to getting classes because classes in directory is different compared to classes in the jar
  • Scanning and filtering the classes that are identified are components.
  • Instantiate these components to become an instance.
  • Made an Application Context interface. As we know Spring has an interface applicationContext to retrieve all beans. I also made an applicationContext interface to retrieve it from getting, reloading, and updating beans.

Prerequisites

Main Features

  • Create an instance of a specified component
  • Automatically resolve dependencies
  • Create Beans
  • Add your own custom mapping annotations for component and beans
  • Manage instantiated components by an applicationContext interface.
  • Reload instantiated components
  • Handle custom components and bean scopes by using proxies.
  • Enrich components with aspects

Installation & Getting Started

  • Run 'mvn clean install' and get the dependency in the m2 folder.Just import the dependency into your project.

      <dependency>
        <groupId>org.ioc</groupId>
        <artifactId>IOC-Container</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
  • In your main method call InitApplicationContext.run(YourStartUpClass.class) and annotate your startup class with @ComponentEntryApplication

    @ComponentEntryApplication
    public class App {
        public static void main(String[] arg) {
            InitApplicationContext.run(App.class);
        }
    }
  • You can also run your app with InitApplicationContext.run(YourStartUpClass.class, configs) . With the Configuration class, you can customize many features.

    Configurion configs = new Configuration()
            .instantiations()
            .addDependencyResolver(new StringConfigProducer())
            .addDependencyResolver(new StringConfigProducerTwo())
            .and()
            .scanning()
            .addComponentDetailsCreatedCallback(new CustomScopeEventHandler())
            .addComponentAnnotation(CustomComponent.class)
            .and();
    InitApplicationContext.run(App.class, configs);

Below are the supported annotations in this library:

  • @Component - @Service - @Configuration - @Repository
    • Actually, We can use a configuration class to provide custom annotations that can act like @Bean and @Component.
  • @Bean - Specify the bean-producing method.
  • @Scope - Specify the scope of the component. SINGLETONPROTOTYPE, or PROXY.
  • @Autowired - Inject an instance in the constructor or field of a object
  • @PostConstruct - Specify a method that will be executed after the component has been created.
  • @PreDestroy - Specify a method that will be executed just before the component has been disposed.
  • @StartUp - Specify the startup method for the app.
  • @AliasFor - Use this annotation to integrate your own annotations with InitApplicationContext. AliasFor works with Autowired, NamedInstance, Nullable, PostConstruct, PreDestroy, Qualifier.
  • @NamedInstance - Specify the name of the component/ bean.
  • @Nullable - required dependency can be null.
  • @Qualifier - Specify the name of the dependency that you are requiring.

Documentation

Currently, there are no real documents for this library. I hope can do it in the future. But you can check out my integration tests here https://github.com/nampython/TEST-IOC-CONTAINER.git

More info

If you are having trouble running the app, contact me at quangnam130520@gmail.com