Skip to content

Spring Inversion Of Control#

What Is the Inversion Of Control (IOC)#

  • Inversion of control: This is the principle of object-oriented programming, in which objects of the program do not depend on concrete implementations of other objects, but may have knowledge about their abstractions (interfaces) for later interaction. IoC refers to transferring the control of objects and their dependencies from the main program to a container or framework.
  • In general, IoC is the approach of outsourcing the construction and management of objects.

Spring Inversion Of Control#

Spring Container#

  • Spring Framework provides for us a Spring Container which fully support IoC. The Spring Container will provide 2 primary functions:
    • Create and Manage objects (Inversion Of Control)
    • Inject object's dependencies (Dependency Injection)

Configure Spring Container#

  • There are 3 ways that we can use to configure the Spring Container in Spring Framework:
    • XML configuration file (legacy, but most legacy apps still use this)
    • Java Annotations (modern)
    • Java Source Code (modern - No XML)

Spring Development Processes#

  • So for configuring Spring Container using XML configuration file (See Spring Inversion Of Control XML), we should follow steps below:

    • Configure Spring Beans.
    • Create a Spring Container.
    • Retrieve beans from Spring Container for using.
  • If we use Java Annotations for configuring Spring Container (See Spring Inversion Of Control Annotation), we should follow steps below:

    • Enable component scanning in Spring config file.
    • Add the @Component Annotation to our Java classes.
    • Retrieve bean from Spring Container.
  • If we use Java Source Code for configuring Spring Container (See Spring Inversion Of Control Java Code), we should follow steps below:

    • Create a Java class and annotate as @Configuration.
    • Add component scanning support: @ComponentScan (Optional).
    • Read Spring Java configuration class.
    • Retrieve bean from Spring Container.

What Is The Spring Bean?#

  • In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in our application. Beans, and the dependencies among them are reflected in the configuration metadata used by a container.
    • A "Spring Bean" is simply a Java object.
    • When Java objects are created by the Spring Container, then Spring refers to them as "Spring Beans".
    • Spring Beans are created from normal Java classes .... just like Java objects.

What Is The Java Annotation?#

  • Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.
  • In general, Java Annotations

    • Are special labels/markers added to Java classes.
    • Provide meta-data about the class
    • Are processed at compile time or run-time for special processing.
  • Annotations in Java are used to provide additional information, so it is an alternative option for XML and Java marker interfaces.

  • There are several built-in annotations in Java. Some annotations are applied to Java code and some to other annotations.

  • Built-In Java Annotations used in Java code

    • @Override
    • @SuppressWarnings
    • @Deprecated
    • @SafeVarArgs
    • @FunctionalInterface
  • Built-In Java Annotations used in other annotations

    • @TaSpring Container using XML configuration file, we should follows these step below:
    • Configure Spring Beans.
    • Create a Spring Container.
    • Retrieve beans from Spring Container for using.rget
    • @Retention
    • @Inherited
    • @Documented
    • @Repeatable

See Also#

References#