Advanced Java Session 2 (Conecpt of IOC And Dependency Injection And Maven)

1.What is IOC

Inversion of control means the program delegates control to someone else who will drive the flow. IOC (Inversion of control) is a general parent term while DI (Dependency injection) is a subset of IOC.

IOC is a container responsible to instantiate, configure and assemble the objects. The IoC container gets informations from the XML file and works accordingly. The main tasks performed by IoC container are:

  • to instantiate the application class
  • to configure the object
  • to assemble the dependencies between the objects

There are two types of IoC containers. They are:

1.      BeanFactory

2.      ApplicationContext

 

 

 

2.Difference between BeanFactory and the ApplicationContext

The org.springframework.beans.factory.BeanFactory and the org.springframework.context.ApplicationContext interfaces acts as the IoC container. The ApplicationContext interface is built on top of the BeanFactory interface. It adds some extra functionality than BeanFactory such as simple integration with Spring's AOP, message resource handling (for I18N), event propagation, application layer specific context (e.g. WebApplicationContext) for web application. So it is better to use ApplicationContext than BeanFactory.

 

 

 

 

Using BeanFactory

The XmlBeanFactory is the implementation class for the BeanFactory interface. To use the BeanFactory, we need to create the instance of XmlBeanFactory class as given below:

1.      Resource resource=new ClassPathResource("applicationContext.xml");  

2.      BeanFactory factory=new XmlBeanFactory(resource);  

The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

 

 

Using ApplicationContext

The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. We need to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as given below:

1.      ApplicationContext context =   

2.          new ClassPathXmlApplicationContext("applicationContext.xml");  

The constructor of ClassPathXmlApplicationContext class receives string, so we can pass the name of the xml file to create the instance of ApplicationContext.

 

 

 

 

 

 

 

 

 

 

 

 

3.What is Dependency Injection

 

Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application. Dependency Injection makes our programming code loosely coupled. To understand the DI better, Let's understand the Dependency Lookup (DL) first:

 

Dependency Lookup

The Dependency Lookup is an approach where we get the resource after demand. There can be various ways to get the resource for example:

1.      A obj = new AImpl();  

In such way, we get the resource(instance of A class) directly by new keyword. Another way is factory method:

1.      A obj = A.getA();  

This way, we get the resource (instance of A class) by calling the static factory method getA().

Alternatively, we can get the resource by JNDI (Java Naming Directory Interface) as:

1.      Context ctx = new InitialContext();  

2.      Context environmentCtx = (Context) ctx.lookup("java:comp/env");  

3.      A obj = (A)environmentCtx.lookup("A");  

There can be various ways to get the resource to obtain the resource. Let's see the problem in this approach.

 

 

 

 

 

Problems of Dependency Lookup

There are mainly two problems of dependency lookup.

  • Tight coupling The dependency lookup approach makes the code tightly coupled. If resource is changed, we need to perform a lot of modification in the code.
  • Not easy for testing This approach creates a lot of problems while testing the application especially in black box testing.

Dependency Injection

The Dependency Injection is a design pattern that removes the dependency of the programs. In such case we provide the information from the external source such as XML file. It makes our code loosely coupled and easier for testing. In such case we write the code as:

1.      class Employee{  

2.      Address address;  

3.        

4.      Employee(Address address){  

5.      this.address=address;  

6.      }  

7.      public void setAddress(Address address){  

8.      this.address=address;  

9.      }  

10.   

11. }  

In such case, instance of Address class is provided by external souce such as XML file either by constructor or setter method.

Spring framework provides two ways to inject dependency

  • By Constructor
  • By Setter method

 

5. Difference between IOC and Dependency Injection

 

 

Sr. No.

Key

IOC

Dependency Injection

1

Design Principle

It is design principle where the control flow of the program is inverted

It is one of the subtypes of the IOC principle  

2

Implementation

It is a term which is implemented by multiple design patterns  service locator , events , delegates and dependency Injection

DI is design pattern which can be achieved by constructor and setter injection

3

Use Case

Aspect oriented programing is one way to implement IOC  

 In Case of change in business requirement no code change required

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Steps to create maven project

 

Open sts ide – click file – new – maven project – give group id ex : com.consyosoft) this contains production files – give artifact id (means project name) – click finish.

 

Note : groupId uniquely identifies your project across all projects. A group ID should follow Java's package name rules. This means it starts with a reversed domain name you control. For example, org.apache.maven.

 

 

Understanding folder structure and files of maven project

 

In maven project folder, src/main/java will be created automatically, in that group name (package name) will be displayed, in this we keep .java files.

 

In src/main/resources folder we keep properties file,

 

 

In src/test/java (which got created automatically) we keep test cases (test files).

 

In src/test/resources (which got created automatically) we keep test property file.

 

.m2 folder will be automatically created at configured path and downloaded jar files are kept there.

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Advanced Java Session 1 (Concept of Web Application Basics And Advantages of Spring)