In this Java Spring Training session, you will learn Spring – Inversion of Control, Dependency Injection and Bean definitions. Topics covered in this session are:
For more information, visit this link:
Spring Framework
• Core Container
• Data Access/Integration
• Web Layer
• Spring Setup
�...
In this Java Spring Training session, you will learn Spring – Inversion of Control, Dependency Injection and Bean definitions. Topics covered in this session are:
For more information, visit this link:
Spring Framework
• Core Container
• Data Access/Integration
• Web Layer
• Spring Setup
• Key features
• Spring Bean
• Dependency Injection
• Relation between DI and IoC
• Spring IoC Containers
• Spring DI
https://www.mindsmapped.com/courses/software-development/spring-fundamentals-learn-spring-framework-and-spring-boot/
Size: 839.5 KB
Language: en
Added: Aug 17, 2018
Slides: 36 pages
Slide Content
Java Spring Training
Spring –Inversion of Control, Dependency Injection
and Bean definitions
Page 1Classification: Restricted
Agenda
Spring Framework
•Core Container
•Data Access/Integration
•Web Layer
•Spring Setup
•Key features
•Spring Bean
•Dependency Injection
•Relation between DI and IoC
•Spring IoCContainers
•Spring DI
Page 2Classification: Restricted
Spring Framework
•Spring is the most popular application development framework for
enterpriseJava.
•Open source Java platform since 2003.
•Spring supports all major application servers and JEE standards.
•Spring handles the infrastructure so you can focus on your application
•https://spring.io/
Page 3Classification: Restricted
Spring history
Page 4Classification: Restricted
Spring Framework
Page 5Classification: Restricted
Core Container
•TheCoremodule provides the fundamental parts of the framework,
including the IoCand Dependency Injection features.
•TheBeanmodule provides BeanFactorywhich is a sophisticated
implementation of the factory pattern.
•TheContextmodule builds on the solid base provided by the Core and
Beans modules and it is a medium to access any objects defined and
configured. The ApplicationContextinterface is the focal point of the
Context module.
•TheSpELmodule provides a powerful expression language for querying and
manipulating an object graph at runtime.
Page 6Classification: Restricted
Data Access / Integration
•TheJDBCmodule provides a JDBC-abstraction layer that removes the need
to do tedious JDBC related coding.
•TheORMmodule provides integration layers for popular object-relational
mapping APIs, including JPA, JDO, Hibernate, and iBatis.
•TheOXMmodule provides an abstraction layer that supports Object/XML
mapping implementations for JAXB, Castor, XMLBeans, JiBXand XStream.
•The Java Messaging ServiceJMSmodule contains features for producing
and consuming messages.
•TheTransactionmodule supports programmatic and declarative
transaction management for classes that implement special interfaces and
for all your POJOs.
Page 7Classification: Restricted
Web Layer
•TheWebmodule provides basic web-oriented integration features such as
multipart file-upload functionality and the initialization of the IoCcontainer
using servlet listeners and a web-oriented application context.
•TheWeb-MVCmodule contains Spring's model-view-controller (MVC)
implementation for web applications.
•TheWeb-Socketmodule provides support for WebSocket-based, two-way
communication between client and server in web applications.
•TheWeb-Portletmodule provides the MVC implementation to be used in a
portlet environment and mirrors the functionality of Web-Servlet module.
Page 8Classification: Restricted
Miscellaneous
•TheAOPmodule provides aspect-oriented programming implementation
allowing you to define method-interceptors and pointcutsto cleanly
decouple code that implements functionality that should be separated.
•TheAspectsmodule provides integration with AspectJ which is again a
powerful and mature aspect oriented programming (AOP) framework.
•TheInstrumentationmodule provides class instrumentation support and
class loader implementations to be used in certain application servers.
•TheMessagingmodule provides support for STOMP as the WebSocketsub-
protocol to use in applications. It also supports an annotation programming
model for routing and processing STOMP messages from WebSocket
clients.
•TheTestmodule supports the testing of Spring components with JUnit or
TestNGframeworks.
Page 9Classification: Restricted
Spring setup
•Download jars here…
http://repo.spring.io/release/org/springframework/spring/
•Unzip spring-framework-4.x.x.RELEASE-dist.zip
•Jars are available within spring/libs
•Also download jars for Apache Commons Logging
http://commons.apache.org/proper/commons-
logging/download_logging.cgi
•Or use Maven
Page 10Classification: Restricted
Hello World using Spring
•Demo
Page 11Classification: Restricted
Key features of Spring Framework
•Dependency Injection and Inversion of Control
•Aspect Oriented Programming
•Spring Modules
Page 12Classification: Restricted
Bean
Page 13Classification: Restricted
Dependency Injection
•The technology that actually defines Spring (Heart of Spring).
•Dependency Injection helps us to keep our classes as indepedent as
possible.
•Increase reuse by applying low coupling
•Easy testing
•More understandable
Page 14Classification: Restricted
Dependency Injection
•Dependency injectionis a pattern where the container passes objects by
name to other objects, via either constructors, properties, or factory
methods.
•An injection is the passing of adependency(a service) to a
dependentobject(a client). Passing the service to the client, rather than
allowing a client to build or find the service, is the fundamental
requirement of the pattern.
•Two types of Dependency Injection:
•Constructor based
•Setter based
Page 15Classification: Restricted
Relation between DI and IoC
•In software engineering, inversion of control (IoC) describes a design in
which custom-written portions of a computer program receive the flow of
control from a generic, reusable library.
•The Inversionof Control(IoC) is a general concept, and it canbe expressed
inmany different ways anddependency Injectionis merely one concrete
example ofInversionof Control.
Page 16Classification: Restricted
Dependency Injection-IoC Container
•The Spring container(IoC Container)is atthe core of the Spring Framework.
•The container will create the objects, wire themtogether, configure them,
and manage their complete lifecycle fromcreationtill destruction.
16
Page 17Classification: Restricted
•The container gets its instructions on
what objects to instantiate, configure,
and assemble by readingconfiguration
metadata provided.
•The configurationmetadata canbe
represented either by;
•XML,
•Javaannotations,
•Java code.
Dependency Injection-IoC Container
Page 18Classification: Restricted
Spring IoCContainers…
•Spring BeanFactoryContainer
•Spring ApplicationContextContainer –Recommended for most purposes
Note: TheApplicationContextcontainer includes all functionality of
theBeanFactorycontainer, so it is generally recommended over
theBeanFactory. BeanFactorycan still be used for light weight applications
like mobile devices or applet based applications where data volume and
speed is significant.
Page 19Classification: Restricted
Dependency Injection-Code Example
19
To instantiate the above classes, one way is to do the usual new operator likenew
Foo()ornew Bar()OR we can use the Spring dependency injection to instantiate these
classes and set the properties accordingly.
Page 20Classification: Restricted
Dependency Injection-Code Example
20
Foo f = new
Foo("Cleopatra");
Bar b = new
Bar("Arthur",26);
b.setFoo(f);
Page 21Classification: Restricted
Dependency Injection-Code Example
21
Spring's ClassPathXmlApplicationContextis the commonly used object that
hold the information of all the beans that it instantiates.
Page 22Classification: Restricted
Dependency Injection-Bean Scopes
22
Scope Description
Singleton (Default) Scopes a single bean definition to a single object instance per Spring
IoC container.
Prototype Scopes a single bean definition to any number of object instances.
Page 23Classification: Restricted
Bean scope -Demo
•Demo of bean scope –Prototype and Singleton
Page 24Classification: Restricted
Other Bean Scopes
Scope Description
singleton This scopes the bean definition to a single instance per
Spring IoC container (default).
prototype This scopes a single bean definition to have any number of
object instances.
request This scopes a bean definition to an HTTP request. Only valid
in the context of a web-aware Spring ApplicationContext.
session This scopes a bean definition to an HTTP session. Only valid
in the context of a web-aware Spring ApplicationContext.
global-
session
This scopes a bean definition to a global HTTP session. Only
valid in the context of a web-aware Spring
ApplicationContext.
Page 25Classification: Restricted
Bean definition –Configuration metadata
PropertiesDescription
class This attribute is mandatory and specify the bean class to be used to create
the bean.
Name /id This attribute specifies the bean identifier uniquely. In XML-based
configuration metadata, you use the id and/or name attributes to specify
the bean identifier(s).
scope This attribute specifies the scope of the objects created from a particular
bean definition and it will be discussed in bean scopes chapter.
constructor-
arg
This is used to inject the dependencies and will be discussed later
propertiesThis is used to inject the dependencies and will be discussed later
autowiring
mode
This is used to inject the dependencies and will be discussed later
lazy-
initialization
mode
A lazy-initialized bean tells the IoCcontainer to create a bean instance
when it is first requested, rather than at startup.Default is false.
initialization
method
A callback to be called just after all necessary properties on the bean have
been set by the container. It will be discussed in bean life cycle chapter.
destruction
method
A callbackto be used when the container containing the bean is destroyed.
It will be discussed in bean life cycle chapter.
Page 26Classification: Restricted
Spring configuration metadata
•XML based configuration file.
•Annotation-based configuration
•Java-based configuration
Page 27Classification: Restricted
Spring Bean Life-Cycle methods –init and destroy
<bean id="exampleBean"
class="examples.ExampleBean"
init-method=“init”
destroy-method=“destroy” />
public class ExampleBean{
public void init() {
// do some initialization work
}
public void destroy() {
// do some destruction work
}
}
Note: you need to register a shutdown hookregisterShutdownHook()method that is declared on
the AbstractApplicationContextclass. This will ensures a graceful shutdown and calls the relevant
destroy methods.
Page 28Classification: Restricted
Spring Bean Life-Cycle methods –init and destroy
Page 31Classification: Restricted
Constructor based DI
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--Definition for textEditorbean -->
<bean id="textEditor" class=“demo.TextEditor">
<constructor-argref="spellChecker"/>
</bean>
<!--Definition for spellCheckerbean -->
<bean id="spellChecker" class=“demo.SpellChecker">
</bean>
</beans>
Page 32Classification: Restricted
Setter based DI
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--Definition for textEditorbean using inner bean -->
<bean id="textEditor" class=“demo.TextEditor">
<property name="spellChecker">
<bean id="spellChecker" class=“demo.SpellChecker"/>
</property>
</bean>
</beans>
Page 33Classification: Restricted
Exercise:
•Create a Quiz Application
•A Quiz has a list of Questions and associated Answers
•Every Answer associated with a question is a set of answers submitted by
multiple persons
•Inject the list of questions and associated answer set for each question
using DI using beans.xml
•E.g. Question 1: Is Java an OOP language?
Answer 1a: Yes (by Bill)
Answer 1b: Yes, it is (by John)
Answer 1c: No, it is not (by Jack) … no limit on number of answers
Note: Every set of answers is actually a list of answer objects. Each answer
object has an id, answerString, submittedBy. So we need to understand how
this collection can be injected in Beans.xml
Page 34Classification: Restricted
Topics to be covered in next session
•Auto-wiring
•Annotations based configuration
•Java based configuration