0% found this document useful (0 votes)
24 views

Bharat Singh June 28, 2019

This document provides an overview of key concepts in Spring including inversion of control (IoC), dependency injection (DI), the Spring IoC container, and different ways to configure beans and inject dependencies including XML configuration, annotations, and Java-based configuration. It includes examples of constructor-based DI, setter-based DI, autowiring dependencies, component scanning, primary and qualifier annotations, and migrating from XML to annotation and Java-based configuration in Spring.

Uploaded by

Anonymous bK5CNN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Bharat Singh June 28, 2019

This document provides an overview of key concepts in Spring including inversion of control (IoC), dependency injection (DI), the Spring IoC container, and different ways to configure beans and inject dependencies including XML configuration, annotations, and Java-based configuration. It includes examples of constructor-based DI, setter-based DI, autowiring dependencies, component scanning, primary and qualifier annotations, and migrating from XML to annotation and Java-based configuration in Spring.

Uploaded by

Anonymous bK5CNN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Java

Bharat Singh
June 28, 2019
Agenda
IoC & DI

Spring Bean Definition & Scope (xml based)

Bean DI Example (xml based)

Beans Autowiring (xml based)

Annotation Based Configuration

Component & ComponentScan

Primary & Qualifier

Java Based Configuration


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. It’s most
often used in the context of object-oriented programming.
By contrast with traditional programming, in which our custom code makes calls to a
library, 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 plug-in our own classes.
The advantages of this architecture are:
 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.
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.
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.
Here’s how you would create an object dependency in traditional programming:
public class Store {
private Item item;
public Store() { item = new Item(); }
}
By using DI, we can rewrite the example without specifying the implementation
of Item that we want:
public class Store {
private Item item;
public Store(Item item) { this.item = item; }
}
Spring IoC Container
An IoC container is a common characteristic of frameworks that implement IoC.
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, and WebApplicationContext for web applications.
In order to assemble beans, the container uses configuration metadata, which can
be in the form of XML configuration or annotations.
Here’s one way to manually instantiate a container:
ApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext.xml");
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.
 Constructor-Based Dependency Injection: The container will invoke a constructor
with arguments each representing a dependency we want to set.
 Setter-Based Dependency Injection: The container will call setter methods of our
class, after invoking a no-argument constructor or no-argument static factory method to
instantiate the bean.
 Field-Based Dependency Injection
In case of Field-Based DI, we can inject the dependencies by marking them with
an @Autowired annotation:
Autowiring Dependencies: Wiring allows the Spring container to automatically resolve
dependencies between collaborating beans by inspecting the beans that have been
defined. There are 4 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 DI by Constructor

1. Add required Spring libraries


using Add External JAR option.
2. Create package com.arit & files
mentioned in image.
3. Create MyBeans.xml file in src
folder.
Change
SpellChecker.java

Change
TextEditor.java
Change
MainApp.java

Change
MyBeans.xml &
Run the project
Bean DI by Setter Method

Change
SpellChecker.java

Change
TextEditor.java
Change
MainApp.java

Change
MyBeans.xml &
Run the project
Annotation Based Configuration
Create a new maven project having archtype quickstart. Update pom.xml.
Create
AppleLaptop.java

Create
AppConfig.java
Update App.java. Save all files & Run Project.
Example 2
Create a new interface
Processor.java.

Create a new class


Inteli7.java.
Change
AppConfig.java
Change
AppleLaptop.java
Component & ComponentScan Annotations

Change
AppConfig.java

Change
Inteli7.java
Change
Inteli7.java &
Run the project
Primary & Qualifier Annotations

Create
AsusPrime.java
& Run the Project
Remove @Primary
from AsusPrime.java

Change
AppleLaptop.java
& Run the Project
Java Based Configuration
Create a new Maven web apps project with following structure.
Change
pom.xml.
Change index.jsp.

Change
viewUserDetails.jsp.
Change
MainController.java.
Change
MainConfig.java.
Change
MainInitializer.java.
Thank You !!!

You might also like