You are on page 1of 12

Spring

Name- Navjot Kaur

1.create a Hello World example using spring framework with Maven.

2.What are the different versions you can find of Spring


Framework, and what are their major features?

The three versions are Spring 2.5, Spring 3.0, and Spring 4.0.

Spring 2.5 was the first version to support annotations. It was released in the
year 2007. Spring 3.0 utilized upgrades in Java5 and furthermore offered help
to JEE6. It was released two years later in 2009. Spring 4.0 was the main
variant to offer comprehensive support to Java 8. It was released
in the year 2013.

3.Describe or define Spring Framework.

It is an open-source Java platform. It is used as an option for heavy


enterprise Java technologies. To put it in simple terms, it simplifies Java
development because it is lightweight. Developers can develop applications
using Spring Framework and deploy even on computers with restricted CPU
memory and resources. Further, it offers support to various other frameworks
including but not limited to Tapestry, JSF, EJB, Hibernate, Struts, and more.

4. What are the advantages of the Spring Framework?

キ Spring Framework is an open-source and does not have any


vendor lock-in.

キ You can use only what you really need because of the layered
architecture it uses.

キ Spring Framework uses POJOs, which makes application


testing much easier.

キ You can use it with all other frameworks of Java with Spring
Frameworks. It does not impose any restrictions or limitations.

キ Spring Framework is a modular framework. It is very flexible.

5.What are the various features of the Spring Framework?

Lightweight: Spring Framework is lightweight when we talk about its


transparency and size.

Inversion of control: It is the principle where the control flow of a program


is flipped. Here, the external sources like services, framework, and other
components control the flow of a program instead of the programmer.

Aspect-oriented Programming: Aspect-oriented programming in Spring


supports binding development by splitting system services from
application business logic.

Container: The container is at the core of the Spring Framework It is the


container that creates the objects, binds them, configures them, and be
in charge of their complete life cycle, which is from creation until the end.

MVC Framework: The Spring Framework s MVC web application framework is


easy to use, and it is configurable. You can use other frameworks instead
of picking the Spring MVC Framework.

Transaction Management: Spring Framework uses a generic abstraction layer


for transaction management. You can use Spring s transaction support in

container fewer environments.

JDBC Exception Handling: You do not need to handle any database-


related exceptions explicitly. Spring JDBC Framework handles it using the
JDBC exception handling.

6.How many modules can you find in Spring Framework? Name them.

You can find twenty modules which are categorized into Core Container,
Web, Data Access/Integration, AOP (Aspect Oriented Programming),
Instrumentation, and Test.

Spring Core Container: As the name suggests, it is the core container


of Spring Framework. It comprises of
Spring Core

Spring Bean

Spring Expression Language

Spring Context

Web: This layer gives support to create a web application. It comprises of the
following modules:

Web

Web – MVC

Web – Socket

Web – Portlet

Data Access/Integration: This layer gives support to communicate with


the database. It comprises of the following modules:

Java DataBase Connectivity – JDBC

Object Relational Mapping – ORM

Object XML Mappers – OXM

Java Messaging Service – JMS

Transaction

Aspect-Oriented Programming: In this layer, developers can use


pointcuts, advice, etc. to decouple the code.

Instrumentation: This layer gives support to classloader implementations and


class instrumentation.

Test: This layer gives support to testing with TestNG and Junit.

7. What is a Spring configuration file?

It is basically an XML file. You can find all the information about the classes in
this file. It further describes how these classes are configured and known to
each other. The XML configuration files are detailed, so it is necessary to keep
them clean. Otherwise, managing them becomes difficult if your project is big.

8.Describe the different components of a Spring application.

Typically, a Spring application comprises of the following components:

キ Interface: All the functions are defined in the interface.

キ Bean class: You can find properties here along with the set and
get functions.

キ Spring Aspect-Oriented Programming (AOP): It gives


the functionality of crosscutting concerns.

キ Bean Configuration File: This file contains the details of


classes and how one can configure them.

User program: A user program uses the function.

9. Explain the various ways of using the Spring Framework

Spring Framework can be used in several ways like:

キ Spring Framework can be used as a comprehensive Spring web


application.
キ Spring Framework can be used as a third-party web framework,
using Spring Frameworks middle-tier.

キ Spring Framework can be used for remote usage.

Spring Framework can be used as Enterprise Java Bean, which can wrap
existing POJOs.

10. What is the Spring IOC Container?

The container is at the core of the Spring Framework. It is the container that
creates the objects, binds them, configures them, and be in charge of their
complete life cycle, which is from creation until the end.

The Spring container uses the Dependency Injection to manage the


components that create an application. The container gets directions for
which objects to create, set, and call by reading the configuration
metadata given either by Java annotations, Java code or by an XML.

11. What is a Dependency Injection?

In Dependency Injection, instead of creating objects, you simply describe


them on how to create it. Developers need to describe which services are
required by a particular component in the configuration file. There is no need
to connect the services to components in the code directly. They are wired by
together by the Spring IoC container.

12. In how many methods can one do Dependency Injection?

There are three ways Dependency Injection is done like:

Constructor Injection
Setter Injection

Interface Injection

However, in Spring Framework, only constructor injection and setter injections


are used.

13. What makes constructor injection different from setter injection.

Constructor Injection

There is no incomplete or limited injection.

It doesn t override the setter property.


It creates a new instance if any change is done.

It works better for a lot of properties.

setter injection

There can be incomplete or limited injection.

It overrides the constructor property.

It does not create any new instance if a change is made.

It works better for a fewer properties.

14. Tell us about a few differences between BeanFactory


and ApplicationContext.

Beanfactory:-

It is an interface explained in org.springframework.beans.factory.BeanFactory


It uses Lazy initialization.

It explicitly gives a resource object using the syntax.

It doesn t aid internationalization.


It doesn t aid annotation-based dependency.


Applicationcontext:-

It is an interface explained in org.springframework.context.ApplicationContext

It uses Aggressive initialization

It builds and manages resource objects on its own

It aids internationalization

It aids annotation-based dependency .

15. What are Spring Beans?

Spring Beans are the objects that serve as the backbone of the user s

application. Beans are controlled by the Spring IoC container. By controlling it
means, they are instantiated, configured, wired, and managed by a Spring
IoC container. Beans are created using the configuration metadata that the
users provide to the container.

16.How is configuration metadata provided to the Spring container?

Configuration metadata can be given to Spring container in one of the ways:

Annotation-Based configuration: One can use annotations on the relevant class


to describe the bean wiring into the component class itself. It is not the default
setting. So, if you wish to use it, you would have to enable it in the
Spring configuration file before one can use it.

XML-Based Configuration: The dependencies and the services required by


beans are mentioned in configuration files. These are in an XML format.
They comprise of many bean definitions and application-specific
configuration options. They generally begin with a bean tag.

Java-based configuration: The primary features in Spring Framework s new


Java-configuration support are @Bean annotated methods, and
@Configuration annotated classes.

17. How many bean scopes does Spring support?

It supports five scopes which are:

Singleton: This gives scope for the bean definition to a single instance
per Spring IoC container.

Prototype: This gives scope for a single bean definition to have any
number of object instances.

The below scopes are available only if the users employ a web-aware
ApplicationContext.

Request: This gives scope for a bean definition to an HTTP-request.

Session: This gives scope for a bean definition to an HTTP-session.

Global-session: This gives scope for a bean definition to a Global


HTTP-session.

18. Explain the Bean life cycle in Spring Bean Factory Container

The Spring container first instantiates the bean from the bean s definition in

the XML file.

Spring using the dependency injection populates all of the properties.

The factory calls setBeanName() by giving the bean s ID if the bean executes


the BeanNameAware interface.

The factory calls setBeanFactory() by creating an instance of itself if the bean


executes the BeanFactoryAware interface.

If there are any BeanPostProcessors related to the bean, then


preProcessBeforeInitialization() methods are called.

If the bean specifies the init method, it is then called.

If there are any BeanPostProcessors related to the bean, then


preProcessAfterInitialization() methods are called.

19.What is auto wiring and name the different modes of auto wiring?

The Spring container has the capability to auto-wire relationships between the
connecting beans. The Spring can automatically resolve collaborators for your
bean by seeing the contents of the BeanFactory.

The different modes of bean auto-wiring are:

no: This is the pre-selected option, which means no auto wiring. You need to
use an explicit bean reference for wiring.

byName: It injects the object dependency based on the name of the bean. It
pairs and wires its properties with the beans explained by the exact names in
the XML file.

byType: As the name says, it injects the object dependency according to type.
It matches and wires a property if the type matches with one of the names
of the beans in an XML file.

Constructor: A constructor injects the dependency by calling the constructor


of the class. It has many parameters.

Autodetect: By default, the container auto wires using a constructor. If it


fails, it then tries to auto-wire by byType.

20. Explain the difference between various annotations like


@Controller, @Component, @Repository & @Service in Spring?

@Controller: This indicates a class as a Spring Web MVC controller. It


automatically imports the beans marked with it into the Dependency Injection
container.

@Component: This indicates a java class as a bean. It is a general


stereotype for any Spring-managed component. The component-scanning
process of Spring can pick it up and pull it into the application context.

@Service: This annotation is an area of component annotation. There is no


new behavior over the @Component annotation. One can use @Service
instead of the @Component in service-layer classes because it specifies intent
appropriately.

@Repository: This annotation is an area of the @Component annotation. It


comes with the same functions and uses. However, it offers some extra
advantages, precisely for DAOs. It gets the DAOs into the DI container and
qualifies the unchecked exceptions for translation into Spring
DataAccessException.
21. Describe Spring DAO support

The Data Access Object support in Spring makes it straightforward to access


technologies like Hibernate, JDBC, or JDO in a proper and stable way. It
enables one to select any of the technologies without any hassle. It also
enables one to code without stressing out about catching exceptions that are
related to each of this technology.

22.What do you mean by the Spring MVC framework?

The Spring web MVC framework offers model-view-controller architecture and


ready to use components. You can use these components to create flexible
and loosely coupled web apps. The MVC pattern supports in isolating the
different aspects of the application like business logic, input logic, and the UI
logic while giving a loose coupling between every one of these components.

You might also like