Spring 5 1st Unit
Spring 5 1st Unit
Spring 5 1st Unit
It
provides support to various frameworks. It is an open source Java
application development framework which supports developing all types of Java
applications such as enterprise applications, web applications, cloud based
applications and many more. It makes development easier.
Spring modules does not have tight coupling on each other, developer can pick
and choose the modules as per the need for building an enterprise application.
2. Non-invasive approach:
Spring frame work does not force programmer to inherit API given by spring.
(Application program interface)
3. Loose Coupling:
Because of dependency injection concept, spring objects are loosely
coupled. Spring is LightWeight because of many reasons one being it's
components are loosely couples. For example, you can use Spring JDBC
without Spring MVC.
4. Modular fashion:
Spring framework is designed in modular fashion. A programmer can use only
needed modules and ignore the rest.
5. Easy Testing:
Dependency injection and POJO model makes easy to test an application.
8. MVC framework:
Spring framework is a alternative to web MVC(Model View Controller)
frameworks like Struts.
9) Predefined Templates
Spring framework provides templates for JDBC(JAVA DATABASE
CONNECTIVITY) , Hibernate, JPA etc. technologies. So there is no need
to write too much code.
Let's take the example of JdbcTemplate, you don't need to write the code for
exception handling, creating connection, creating statement, committing
transaction, closing connection etc. You need to write the code of executing
query only. Thus, it save a lot of JDBC code.
Applications of Spring
Following is the list of few of the great benefits of using Spring Framework −
POJO Based - Spring enables developers to develop enterprise-class
applications using POJOs. The benefit of using only POJOs is that you do not
need an EJB container product such as an application server but you have the
option of using only a robust servlet container such as Tomcat or some
commercial product.
Modular - Spring is organized in a modular fashion. Even though the number
of packages and classes are substantial, you have to worry only about the
ones you need and ignore the rest.
Integration with existing frameworks - Spring does not reinvent the wheel,
instead it truly makes use of some of the existing technologies like several
ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, and
other view technologies.
Testablity - Testing an application written with Spring is simple because
environment-dependent code is moved into this framework. Furthermore, by
using JavaBeanstyle POJOs, it becomes easier to use dependency injection
for injecting test data.
Web MVC - Spring's web framework is a well-designed web MVC framework,
which provides a great alternative to web frameworks such as Struts or other
over-engineered or less popular web frameworks.
Central Exception Handling - Spring provides a convenient API to translate
technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for
example) into consistent, unchecked exceptions.
Lightweight - Lightweight IoC containers tend to be lightweight, especially
when compared to EJB containers, for example. This is beneficial for
developing and deploying applications on computers with limited memory and
CPU resources.
Transaction management - Spring provides a consistent transaction
management interface that can scale down to a local transaction (using a
single database, for example) and scale up to global transactions (using JTA,
for example).
Spring Framework module groups:
Core Container: These are core modules that provide key features of the Spring
framework.
Data Access/Integration: These modules support JDBC and ORM (Object Relationl
Mapping) data access approaches in Spring applications.
Web: These modules provide support to implement web applications.
Others: Spring also provides few other modules such as the Test for testing Spring
applications.
AOP (Aspect Oriented Programming) and aspects: These modules help in isolating
cross-cutting functionality from business logic. An aspect represents a class that
contains advices, join points etc like transaction management. An aspect can be
configured through Spring XML configuration or spring AspectJ integration.
Java Database Connectivity (JDBC): It provides an abstract layer to support JDBC calls
to relational databases.
JMS (Java Messaging System) JMS can be roughly divided into two areas of
functionality, namely the production and consumption of messages.
Spring OXM stands for Spring Object XML Mappers and it is a module available in
Spring to ease the mapping between java objects and XML documents. The module is
extensible and hence it provides integration with various popular frameworks like Castor,
JAXB, XmlBeans and XStream.
Transactions: It provides a simple transaction API which abstracts the complexity of
underlying repository specific transaction API's from the application.
Spring Framework provides the following modules to support web application development:
WebSocket: It is used for 2 way communication between client and server in WebSocket
based web applications.
Test: This module provides the required support to test Spring applications
using TestNG or JUnit. The Spring Framework provides first-class support
for integration testing in the spring-test module.
There are many approaches to implement IoC, Spring Framework provides IoC implementation
using Dependency Injection(DI).
We need not create objects in dependency injection instead describe how objects should be
created through configuration.
DI is a software design pattern. It facilitate loose coupling, reuse and ease of testing.
The core container module of Spring framework provide IoC using Dependency Injection.
The Spring container knows which objects to create and when to create through the additional
details that we provide in our application called Configuration Metadata.
1. Application logic is provided through POJO classes.
3. IoC container produces objects required by the application using POJO classes and
configuration metadata. IoC container is of two types – BeanFactory and
ApplicationContext.
BeanFactory:
It is the basic Spring container with features to instantiate, configure and manage the
beans.
org.springframework.beans.factory.BeanFactory is the main interface representing a
BeanFactory container.
ApplicationContext:
ApplicationContext is Spring container. It is more commonly used in Spring
applications.
org.springframework.context.ApplicationContext is the main Interface representing
an ApplicationContext container.
It inherits the BeanFactory features and provides added features to support enterprise
services such as internationalization, validation, etc.
ApplicationContext is the preferred container for Spring application development. Let us look
at more details on the ApplicationContext container.
BeanFactory ApplicationContext
Does not support annotation based Dependency Support annotation based Dependency
Injection. Injection.
Support enterprise services such as
Does not support enterprise services.
validations, internationalization, etc.
By default it support Lazy Loading.
By default it support Eager
// Loading BeanFactory Loading.Beans are instantiated during
BeanFactory factory = new load time.
ClassPathXmlApplicationContext (“config.xml
”); // Loading ApplicationContext and
instantiating bean
// Instantiating bean during first access using ApplicationContext context = new
getBean() ClassPathXmlApplicationContext(“config
ReportService reportService = .xm
factory.getBean(”reportService”);
XML configuration
Annotation based configuration
Java based configuration
In XML configuration, the configuration metadata is provided as a simple XML file
describing bean definitions that the Spring container has to manage.
An instance is created and initialized with default values using default constructor.
You can use the index attribute to specify explicitly the index of constructor arguments, as the
following example shows:
You can also use the constructor parameter name for value disambiguation, as the following
example shows:
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateAnswer" value="42"/>
</bean>
It also supports setter-based DI after some dependencies have already been injected through
the constructor approach.
The constructor-injected components are always returned to the client (calling) code in a fully
initialized state. Setter injection should primarily use for optional dependencies.
Dependency Injection
Dependency injection (DI) is a process whereby objects define their dependencies (that is, the
other objects with which they work)
The container then injects those dependencies when it creates the bean. This process is
fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the
instantiation or location of its dependencies on its own by using direct construction of classes or
the Service Locator pattern.
Code is cleaner with the DI principle, and decoupling is more effective when objects are provided
with their dependencies. The object does not look up its dependencies and does not know the
location or class of the dependencies. As a result, your classes become easier to test,
particularly when the dependencies are on interfaces or abstract base classes, which allow for
stub or mock implementations to be used in unit tests.
Notice that there is nothing special about this class. It is a POJO that has no dependencies on
container specific interfaces, base classes, or annotations.
Constructor argument resolution matching occurs by using the argument’s type. If no potential
ambiguity exists in the constructor arguments of a bean definition, the order in which the
constructor arguments are defined in a bean definition is the order in which those arguments are
supplied to the appropriate constructor when the bean is being instantiated. Consider the
following class.
package x.y;
public class ThingOne {
Assuming that the ThingTwo and ThingThree classes are not related by inheritance, no potential
ambiguity exists. Thus, the following configuration works fine, and you do not need to specify the
constructor argument indexes or types explicitly in the <constructor-arg/> element.
<beans>
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg ref="beanTwo"/>
<constructor-arg ref="beanThree"/>
</bean>
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
</beans>
When another bean is referenced, the type is known, and matching can occur (as was the case
with the preceding example). When a simple type is used, such as <value>true</value>, Spring
cannot determine the type of the value, and so cannot match by type without help. Consider the
following class:
Keep in mind that, to make this work out of the box, your code must be compiled with the debug
flag enabled so that Spring can look up the parameter name from the constructor. If you cannot
or do not want to compile your code with the debug flag, you can use the
@ConstructorProperties JDK annotation to explicitly name your constructor arguments. The
sample class would then have to look as follows:
package examples;
// Fields omitted
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
Core technologies: dependency injection, events, resources, i18n,
validation, data binding, type conversion, SpEL, AOP.
POJO in Java stands for Plain Old Java Object. It is an ordinary object,
which is not bound by any special restriction. The POJO file does not
require any special classpath. It increases the readability & re-usability of
a Java program.
POJOs are now widely accepted due to their easy maintenance. They are
easy to read and write. A POJO class does not have any naming
convention for properties and methods. It is not tied to
any Java Framework; any Java Program can use it.
https://www.javatpoint.com/pojo-in-java
1)The development team is implementing a banking application, the team wants to
develop logging functionality independently from the main business logic of the application
for better code maintenance. Choose the best Spring feature which helps the team to achieve
this requirement.
Non-Invasive
Spring IoC
DI
Due to Non-Invasive feature of Spring, the developer need not extend any class or
implement any interface exclusively for Spring support and Spring IoC enables
Spring to create objects for developers
Explanation:
Explanation :
Using the Spring IOC, an object’s dependencies are injected rather than creating or
depending on other objects. However, there are other options, which are also right.
3)Which of the following statements are TRUE about Spring? [Choose 2 options.]
Explanation:
The Spring Framework modules are organized as loosely coupled modules, the
developer can choose modules based on the need by adding the corresponding Jars.
Q1 of 3outlined_flag
The development team needs to implement a Spring application with the data access layer to
perform the required database operations. Which of the Spring modules given below can be
best used to achieve this requirement? [Choose 2 options].
Web (Web module provides support to create web-based applications.)
Core
ORM
Aop
explanation :
Correct Answer!
The core module is the base module in the spring framework on which all other
modules of the framework are dependent. Therefore, the development team needs
this module to achieve the requirement.
Correct Answer!
ORM module of Spring Data Access Layer provides integration support for ORM solutions.
Q2 of 3outlined_flag
An online shopping web application has to be developed with the data access layer to get
item details. Which Spring modules can be best used to implement this application? [Choose
3 options.]
Web
Core container
Data Access modules - JDBC/ORM
Test
Explanation :
Correct Answer!
Explanation :
Correct Answer!
Correct Answer!
Q3 of 3outlined_flag
A Banking application business layer has been developed using Spring Core and AOP
modules, data access layer using Spring Data Access/Integration module. The development
team wants to implement the presentation tier of the application using simple Model-View-
Controller architecture. Can you suggest the best Spring modules to support this requirement?
[Choose 2 options.]
websockets
aspects
webmvc
web
Explanation :
Yes, you are right!
This module provides an implementation of MVC (model-view-controller) pattern and
extends its support to implement RESTful Web Services. So, it can support the development
of the presentation tier.
Q1 of 3outlined_flag
The development team needs to implement a Spring application with the data access layer to
perform the required database operations. Which of the Spring modules given below can be
best used to achieve this requirement? [Choose 2 options].
web
Core
ORM
aop
Explanation :
Correct Answer!
The core module is the base module in the spring framework on which all other modules of
the framework are dependent. Therefore, the development team needs this module to achieve
the requirement.
Explanation :
Correct Answer!
ORM module of Spring Data Access Layer provides integration support for ORM solutions.
Q2 of 3outlined_flag
An online shopping web application has to be developed with the data access layer to get
item details. Which Spring modules can be best used to implement this application? [Choose
3 options.]
Web
Core container
Data Access modules - JDBC/ORM check
Test
Explanation :
Correct Answer!
The core container is the base of Spring framework and all other modules are built on top of
it. Therefore, to develop any Spring based application this module is required. However,
there are other options, which is also right.
Explanation :
Correct Answer!
Data Access module provides different data access approaches. Therefore, to develop a data
access layer of spring application this module is required.
Yes, you are right!
Web module provides support to create web-based applications. Therefore, to
develop any web-based application this module is required.
A Banking application business layer has been developed using Spring Core and AOP
modules, data access layer using Spring Data Access/Integration module. The development
team wants to implement the presentation tier of the application using simple Model-View-
Controller architecture. Can you suggest the best Spring modules to support this requirement?
[Choose 2 options.]
websockets
aspects
webmvc
web
Explanation :
Explanation :
Q1 of 3outlined_flag
Consider a MobileService class in the package com.infosys with appropriate properties and
methods.
A Spring configuration file defining a bean of MobileService class is shown below:
<bean id="mobileService" class="------------"/>
What should be the value of the class attribute of <bean> tag?
com.infosys
com.infosys.MobileService
MobileService
com.infosys.MobileService.class
Explanation :
Spring does not allow more than one bean definition in the configuration
Explanation :
}
A Spring configuration file(config.xml) defining bean of the GreetingService class is shown
below:
<bean id = "greetingService" class = "com.infosys.service.GreetingService"
/>
The main method of the application includes the following statements:
ApplicationContext context = new
ClassPathXmlApplicationContext("config.xml");
GreetingService greetingService = (GreetingService)
context.getBean("greetingService");
greetingService.setGreeting("Hi, Welcome to Spring Course!!");
System.out.println(greetingService.getGreeting());
What will happen when we execute the above main method?