Spring Framework Flashcards

1
Q

What is Spring Framework?

A

It is an open-source framework that provides the necessary plumbing to build enterprise-level applications so that the application code consists of only business logic and little configuration setup.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is Spring popular?

A

Spring simplifies Java development by

  1. being lightweight and minimally invasive
  2. loose coupling through Dependency Injection (DI)
  3. managing dependencies using Inversion of Control (IoC)
  4. providing declarative programming through aspects
  5. eliminating boilerplate code with aspects and templates
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is Spring lightweight and minimally invasive?

A

The EJB framework that was used before Spring forces one to implement the framework-related methods that might not be needed in some cases.

Spring avoids littering code and never forces one to implement Spring-specific interfaces. With Spring, the only code needed is the core business logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain Spring’s loose coupling through Dependency Injection (DI).

A

With DI, objects are given their dependencies at the creation time by container that manages each object in that system. Objects aren’t expected to create/obtain their own dependencies, rather dependencies are injected into objects that need them.

If an object only knows about its dependencies by their interface, then the dependency can be swapped with a different implementation without object knowing the difference. This loose coupling is the benefit of DI.

For eg., instead of instantiating the implementation of interface like this:

private Animal animal;
public Animal ( ) {
    this.animal = new Dog( );
}

We can rewrite this without specifying the implementation of Animal that we want:

public Animal ( Animal animal ) {
   this.animal= animal;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Application Context?

A

ApplicationContext is the central interface in Spring, which extends another interface BeanFactory, and is essentially a representation of Spring container.

ApplicationContext is used to configure the beans and manage their lifecycles as well as provide other supports like setting property values.

There are 5 types of ApplicationContext:

AnnotationConfigApplicationContext
AnnotationConfigWebApplicationContext
XmlWebApplicationContext
FileSystemXmlApplicationContext
ClassPathXmlApplicationContext
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Spring bean?

A

A Spring bean is simply a Java Object.

When Java objects are created and managed by Spring container, they are called Spring beans.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Spring container?

A

In general, Container is the component that consists of an entire runtime environment: an application + its dependencies, libraries, binaries and other configuration files needed to run the application, bundled into single package.

By containerizing the application platform and its dependencies, we can make the application independent of underlying OS and infrastructure.

In reality, it is a piece of code which reads the bean configuration file and performs corresponding actions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Spring container do?

A

Spring container

  • creates beans
  • configures them
  • wires them together
  • manages their complete lifecycle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many types of Spring container are there?

A

There are two types:

  1. Bean factories- simplest and provides basic support for DI, creation and management of beans
  2. Application Context- extends BeanFactory interface and provides application framework support such as resolving textual messages from properties file, holds bean information and metadata
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Inversion of Control (IoC)?

A

IoC is a principle by which control of objects is transferred to a container/framework.

The object dependencies are defined only through constructor arguments or properties that are set on the instances and are injected by the Spring container, aka Application Context, when beans are created. This process is fundamentally the inverse of the bean itself controlling the lifecycle, hence the name.

The spring-context is the basic package for Spring’s IoC Container. DI is the way by which IoC is implemented in Spring.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Dependency Injection (DI)?

A

DI is the pattern through which IoC is implemented in Spring, where the control being inverted is the setting of object dependencies.

Objects define their dependencies in Spring container which are then injected during bean creation. In this manner, object creation and maintenance is outsourced/delegated to other class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the three ways in which dependency can be injected in Spring?

A

There are 3 ways to perform dependency injection in Spring:

  1. via constructors
  2. via setter methods
  3. Fields in class (@Autowired annotation)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain Constructor-based DI.

A

In constructor-based dependency injection, the container invokes a constructor with arguments each representing a dependency we want to set.

@Component
public class Car {
 @Autowired
    public Car(Engine engine, Transmission transmission) {
        this.engine = engine;
        this.transmission = transmission;
    }
}

Our Car depends on Engine and Transmission classes, hence when Spring encounters Car class, instances of Engine and Transmission will be obtained by calling @Bean annotated methods of the configuration class.

With XML-based configuration, we will have to use constructor-arg tag in Car bean which has a ref element in it, referring to Engine and Transmission beans.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain Setter-based DI.

A

For setter-based DI, 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.

@Bean
public Store store ( ) {
    Store store = new Store ( );
    store.setItem ( item1 ( ) );
    return store;
}

In the XML-based configuration, we will have to use the properties tag inside Store bean which has a ref element in it, referring to item1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Spring MVC?

A

It is the design pattern for developing web applications, where application is separated into Model, View and Controller.

The View (HTML/JSP pages) is where the user inputs the request.

This request is intercepted by Controller which performs business logic on it for which Model holds the domain objects containing data.

This data from the Model is displayed back on View.

MVC pattern provides clear separation of concerns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Explain the scopes of Spring bean.

A
  1. Singleton: only one instance of the bean will be created for each container.
  2. Prototype: new instance of the bean will be created for each new request
  3. Request: new instance of the bean will be created for each new HTTP request
  4. Session: new instance of the bean will be created for each new HTTP session by the container
  5. Global Session: used to create global session beans for Portlet applications
18
Q

Explain the categorization of Spring modules in groups.

A

The Spring Framework features are organized into about 20 modules, which are categorized into 5 basic groups:

  1. Core container: core, bean, context, SPEL
  2. Data Access/Integration: JDBC, ORM, OXM, JMS
  3. Web: Web, Web-servlet, Web-socket, Web-portlet
  4. AOP: AOP, Aspects
  5. Test: JUnit, TestNG
19
Q

Explain the modules in Spring’s Core Container group.

A

It contains core, bean, context and SPEL modules.

Core and Bean provide the fundamental Spring framework features such as IOC and DI.

Context module builds on Core and Bean modules and provides a way to access objects in framework-style manner, by configuring ApplicationContext.

SPring Expression Language (SPEL) provides a way for querying and modifying object at runtime. With SPEL, we can set and retrieve the property values from Spring’s IOC container.

20
Q

Explain the modules in Spring’s Data Access/Integration group.

A

It contains JDBC, ORM, OXM and JMS modules.

JDBC module provides an abstraction layer that removes the boilerplate code.

ORM module provides a way to integrate object-relational mapping APIs such as Hibernate.

OXM module provides an abstraction layer that supports Object/XML mapping implementations.

JMS module contains features for producing and consuming messages.

21
Q

Explain the modules in Spring’s Web group.

A

It contains Web, Web-servlet, Web-socket and Web-portlet modules.

Web module provides basic features for creating web applications, such as multipart-file upload functionality and web-oriented ApplicationContext.

Web-servlet module provides Spring’s MVC implementation.

Web-Portlet module provides the MVC implementation for portlet environment.

22
Q

Explain the modules in Spring’s AOP group.

A

It contains AOP and Aspect modules.

Aspect Oriented Programming (AOP) provides method-interceptors and pointcuts to decouple the code that implements separated functionality.

The separate module Aspects provides integration with AspectJ (which is nothing but AOP implementation for Java specifically).

23
Q

How many dependencies do we need to create a basic Spring application having application context and DI feature?

A

Just one: spring-context.

Spring-context dependency is internally dependent on spring-core, spring-beans and spring-aop and hence contains all three.

24
Q

What is Maven “Bill of Materials” dependency?

A

To avoid problems arising due to different versions of various dependencies, Maven supports concept of a “bill of materials” (BOM) dependency.

We can import the spring-framework-bom in the dependencyManagement section of POM file to ensure that all spring dependencies (both direct and transitive) are at the same version. With this, we no longer have to include the version tag for Spring dependencies.

25
Q

What are the three ways in which Spring beans can be configured/ bean definition loaded?

A

Spring beans can be configured with

  1. XML-based configuration
  2. Annotation-based configuration
  3. Java-based configuration
26
Q

Explain the three phases of bean lifecycle in brief.

A
  1. Initialization: begins when the ApplicationContext is created, bean definitions are loaded and beans are created.
  2. Use
  3. Destruction
27
Q

Explain the initialization phase of a bean’s lifecycle.

A

Init phase begins when bean configuration is loaded one of three ways and bean factory is created.

  1. Bean definitions loaded: First, we set up the bean factory using any of the three configuration styles and load the bean definitions provided in them. At this point, beans are just references and metadata. No object is created yet.
  2. Instantiate bean: we go through each bean in bean factory to instantiate it and this is done in correct order to ensure dependencies are created first. Beans are instantiated eagerly, injected with default values and bean references and are wired.
  3. Setters called: BeanNameAware, BeanFactoryAware, ApplicationContextAware
  4. Bean post-processing (pre-init): BeanPostProcessor’s postProcessBeforeInitialization
  5. Initializer: InitializingBean’s afterPropertiesSet
  6. Bean post-processing (post-init): BeanPostProcessor’s postProcessAfterInitialization

At this point, beans are ready for use.

28
Q

What is a POM file?

A

Project Object Model (POM) is an XML file containing project information and configuration for the maven to build the project, such as dependencies, plugins, build directory (target), source directory(src/main/java), test directory (src/test/java) etc.

When executing a task or goal, Maven looks for the POM in current directory. It reads the POM, gets the needed configuration information and then executes the goal.

29
Q

What is the difference between BeanFactory and ApplicationContext?

A

BeanFactory is an interface representing a container that provides and manages bean instances. The default implementation instantiates beans lazily when getBean() is called.

ApplicationContext is an interface extending BeanFactory and it represents a container holding all information, metadata and beans in the application. It instantiates beans eagerly when the application starts.

30
Q

What are the types of ApplicationContext implementations?

A
  1. ClassPathXmlApplicationContext: load an XML configuration file from the classpath
  2. FileSystemXmlApplicationContext: load an XML-based Spring configuration file from the file system or from URLs
  3. XmlWebApplicationContext: XML based configuration in a web application
  4. AnnotationConfigApplicationContext: classes are annotated with @Configuration, @Component
  5. AnnotationConfigWebApplicationContext: web-based variant of AnnotationConfigApplicationContext
31
Q

What is bean wiring?

A

The act of creating associations between beans is called bean wiring. We can explicitely wire beans by configuring them one of three ways or we can autowire them by annotating beans with @Autowired and providing the “context:annotation-config” tag to enable it.

31
Q

What are the different types of Spring bean autowiring?

A

There are 4 types of autowiring:

  1. byName
  2. byType
  3. by constructor
  4. autodetect
32
Q

How does Spring bootstrap using web.xml?

A
  1. Servlet container (the server) reads web.xml
  2. The DispatcherServlet defined in the web.xml is instantiated by the container
  3. DispatcherServlet creates WebApplicationContext by reading WEB-INF/{servletName}-servlet.xml
  4. Finally, the DispatcherServlet registers the beans defined in the application context
33
Q

How does Spring bootstrap using ServletContainerInitializer class?

A
  1. The container searches for classes implementing ServletContainerInitializer and executes
  2. The SpringServletContainerInitializer finds all classes implementing WebApplicationInitializer
  3. The WebApplicationInitializer creates the context with XML or @Configuration classes
  4. The WebApplicationInitializer creates the DispatcherServlet with the previously created context.