Skip to content

IOC & DJ

Yash edited this page Aug 30, 2017 · 1 revision
  1. Spring IOC container

IOC is also known as dependency injection (DI). It is a process whereby objects define their dependencies.

Spring Framework: It is a light weight framework which basically focuses on loose Coupling. Here coupling it means the less dependencies over different components of MVC So that we can improve the maintainability. And it is an integrated framework which is created to address the complexity of the enterprise application development.

IMAGE ALT TEXT HERE

Because of its layered architecture with different modules, it allowing you to use only those parts that you need, without having to bring in the rest.

Their are 2 powerful features of spring in its core layers is IOC & DI.

IOC means you no need to create the objects, Configure the objects then the objects will be constructed by the spring framework for us.

Feature: Light weight « Which means we got memory optimization IOC « We got control of Object construction, destruction and the entire lifecycle will be managed by the Spring container

Spring 2.5 - made annotation driven configuration possible

Spring IOC: (The responsibility of creating objects is shifted from our application code to Spring container hence the phenomenon is called IOC. IOC is achieved through DI.)

  • « Configure the beans | Pojos classes, IOC read those configuration's defied.
    1. Each bean should have default constructor so that using Class.forName( beanClassName ); IOC creates the instance of that bean.
    2. If the default constructor is not available, Then provide the parameters using Constructor injection. So, that IOC passes the parameters of that constructor and instantiates the bean.
  • « Creates the object, and manages the complete lifecycle from creation till its destruction.
  • « If the bean is dependent on other, then configure its dependencies by referencing through DI.

In object-oriented programming, there are several basic techniques to implement IOC. One of them is DI

Dependency Injection can be done by Constructor injection, Setter injection, Interface injection.

« we don't create object but describe how they should be created.

Setter injection Example: Suppose we have an object JdbcTemplate which is dependent on DataSource Object. Before crating jt object Container will find out wheather this object has any dependency over other object. if found that jt dependent on datasource the container will create dataSource object first and then crates the jt object by injecting dataSource object into it using DI.

XML Configuration where we provide the dependencies structure.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${config.db.driverClassName}" />
	<property name="url" value="${config.db.url}" />
	<property name="username" value="${config.db.username}" />
	<property name="password" value="${config.db.password}" />
</bean>

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource" />
</bean>

Java program where we get object from container and reference using Setter injection as a dependency.

@Autowired public JdbcTemplate service;
// Setter method
public void setService(Service service) {
    // Save the reference to the passed-in service inside this client
    this.service = service;
}

IOC container crates all the bean instances which are configured. Those objects are stored | live inside a container, called "application context".

@Autowired - Process to match the exact type of bean configured in the configuration files and wires | inject it to the reference variable.

xml « <bean id="emp" class="com.gmail.Employee"/>
java « @Autowired private Employee emp;

@Qualifier - based on the configured bean name it will pick and inject it to the reference var.

xml « <bean id="addr1" class="com.gmail.EmployeeAddress"></bean>
      <bean id="addr2" class="com.gmail.EmployeeAddress"></bean>
java « @Autowired @Qualifier("addr2") private EmployeeAddress addr;

AngularJS - IOC & DJ

Clone this wiki locally