Spring Core Framework

1. How does Spring relates or differs from Java EE?

Java EE that stands for Java Enterprise Edition is a specification. It is an effort for
standardizing technologies to build Web and enterprise applications using the Java
programming language. Vendors follow the Java EE specification to provide
implementations, such as Web containers, application servers, message brokers, email
systems, and so on. Programmers use Java EE implementations to develop
applications.
The power of Java EE comes with its own set of complexities. You have to deal with a
large number of XML descriptors, create set of classes for even the most basic
enterprise components, and so on. From the developers perspective, it took
considerable effort and time to package, ship, deploy or redeploy, and start an
application. Spring is one solution to such problems encountered in Java EE.
Spring is a standalone framework for improvements and substitutions to Java EE. The
Spring Framework is a Java platform supported by a large number of independent
modules to develop enterprise applications. For example, you use the Spring MVC
module to create MVC-based Web applications, Spring Security to secure
applications, Spring Data for persistence, Spring Cloud for cloud-native distributed
microservices, and so on.
With Spring, you only need a simple servlet container like Tomcat. You could pack
only what you need and get your application running at considerably less time and
effort. In short, you can consider Spring as an integration platform that allows you to
use all Java EE technologies.
Both Spring and Java EE are evolving at a fast rate and each complements the other.
Java EE 7 is arguably the best release of the EE specification to date and the
contribution goes to the Spring Framework and also the other way around.

2. Why do we go for Spring Framework?

The Spring Framework is an open source application framework to make Java EE
programming easy. Spring aims to help structure your whole application in a
consistent and productive manner. It provides you with the building blocks to easily
bind different layers of your application together. There are many cases in which
Spring Framework can be used. For example, you can use it when you want to
develop a Web application, or you want to expose RESTful Web services, create
powerful messaging systems, highly distributed microservices, or big enterprise
applications that integrate all of these and more.

3. What is Spring configuration file?

The Spring configuration file is an XML file that defines the beans that are used in the
application. In a Spring application, an instance of ApplicationContext, when created,
reads the configuration file and initializes all of the Beans. This file also contains
information of all the Bean classes and describes how these classes are configured and
introduced to each other. In addition, the configuration file tells Spring how to
instantiate, configure, and assemble the objects in your application. Spring other than
the XML configuration file also supports Annotation-based and Java-based
configuration metadata.

4. What is BeanFactory?

BeanFactory is the way to access the Spring container. It contains a collection of
beans with their definitions and instantiates them whenever asked for.
The BeanFactory is responsible for instantiating application objects, configuring such
objects, and assembling their dependencies.

5. What is Spring IoC container and what are its types?

Inversion of Control (IoC) is a widely used pattern in Java that helps wire lightweight
containers or assembles components from different projects into a cohesive
application. IoC is a common characteristic of modern frameworks, including Spring.
In Spring, it is the Spring container that instantiates and manages the application
objects. With the inversion of control, the flow of the business logic depends on the
object graph that is instantiated by the assembler and is made possible by object
interactions.
The Spring IoC container is responsible to instantiate, configure and assemble objects
that are known as beans. It also manages their lifecycle. The container receives the
information on what objects to instantiate, configure, and manage by reading the
configuration metadata you define for your application.
Types of Spring IoC containers:
  • BeanFactory: Provides the configuration framework and basic functionality
  • ApplicationContext: Adds more enterprise-specific functionality. ApplicationContext is a superset ofBeanFactory

6. What are the benefits of Inversion Of Control (IoC)?

The primary benefits of IoC are:
  • Makes it easier to test your code. Without IoC, the code you are testing is hard to isolate as it will be highly coupled to the rest of the system
  • Enables replacing components without requiring recompilation. This is particularly useful when developing modular systems
  • Minimizes the amount of code in your application.
  • Enables loose coupling of your code
  • Supports lazy loading of services and eager instantiation of beans

7. What are the common implementations of ApplicationContext?

The common implementations of ApplicationContext are:
  • ClassPathXmlApplicationContext: Loads bean definitions from XML files
present in the classpath
  • FileSystemXmlApplicationContext: Loads bean definitions from XML files
present in the file system
  • XmlWebApplicationContext: Loads bean definitions from an XML file contained in a Web application. By default it loads the configuration file from /WEB-INF/applicationContext.xml.

8. What is the difference between BeanFactory and ApplicationContext?

BeanFactory and ApplicationContext are two ways to access beans in a Spring
application.
Some key differences are:

  • When you use the BeanFactory, Beans are instantiated when they are requested for the first time, like ingetBean() method, and not when the object of BeanFactory itself gets created. This way of instantiating is known as lazy-instantiation.But singleton beans do not get created lazily when you use theApplicationContext. By default, the ApplicationContext instantiates the singleton beans immediately and set its properties. So ApplicationContext loads singleton beans eagerly which is known as pre-instantiation.
  • ApplicationContext creates and manages resources objects on its own whereas for BeanFactory you need to provide a resource object explicitly.
  • ApplicationContext has the ability to publish the event to beans that are registered as listeners whileBeanFactory does not support this feature.
  • ApplicationContext provides support for internationalization while BeanFactory doesn’t.

Comments

Popular posts from this blog

Spring Annotations

Spring Data Access

Spring Model View Controller (MVC)