Friday 16 March 2018

Spring ioc di

spring

1.What is Inversion of Control?

.Inversion of Control is a principle in software engineering by which the control of objects or portions of a program is transferred to a container or framework

.IoC enables a framework to take control of the flow of a program and make calls to our custom code. To enable this, frameworks use abstractions with additional behavior built in

. If we want to add our own behavior, we need to extend the classes of the framework or plugin our own classes.

-->Advantage

.decoupling the execution of a task from its implementation

.making it easier to switch between different implementations

.greater modularity of a program

.greater ease in testing a program by isolating a component or mocking its dependencies and allowing components to communicate through contracts

 

 

--Inversion of Control can be achieved through various mechanisms such as: Strategy design pattern, Service Locator pattern, Factory pattern, and Dependency Injection (DI).

 

2.What is Dependency Injection?

.Dependency injection is a pattern through which to implement IoC, where the control being inverted is the setting of object’s dependencies.

.The act of connecting objects with other objects, or “injecting” objects into other objects, is done by an assembler rather than by the objects themselves.

3.The Spring IoC Container

.In the Spring framework, the IoC container is represented by the interface ApplicationContext. The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their lifecycle.

.The Spring framework provides several implementations of the ApplicationContext interface 

--ClassPathXmlApplicationContext and FileSystemXmlApplicationContext for standalone applications

--WebApplicationContext,AnnotationConfigWebApplicationContext for web applications.   

.In order to assemble beans, the container uses configuration metadata, which can be in the form of XML configuration or annotations.

eg:ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");

4.Dependency Injection in Spring

public class Store {

private Item item;

public Store(Item item) {

this.item = item;

}

}

--To set the item attribute in the example above, we can use metadata. Then, the container will read this metadata and use it to assemble beans at runtime.

--Dependency Injection in Spring can be done through constructors, setters or fields.

5. Constructor-Based Dependency Injection

http://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

6.Setter-Based Dependency Injection

7.Autowiring Dependencies

Wiring allows the Spring container to automatically resolve dependencies between collaborating beans by inspecting the beans that have been defined.

There are four modes of autowiring a bean using an XML configuration:

no: the default value – this means no autowiring is used for the bean and we have to explicitly name the dependencies

byName: autowiring is done based on the name of the property, therefore Spring will look for a bean with the same name as the property that needs to be set

byType: similar to the byName autowiring, only based on the type of the property. This means Spring will look for a bean with the same type of the property to set. If there’s more than one bean of that type, the framework throws an exception.

constructor: autowiring is done based on constructor arguments, meaning Spring will look for beans with the same type as the constructor arguments

@Bean(autowire = Autowire.BY_TYPE)

No comments:

Post a Comment