Spring Framework Flashcards
What is Spring Framework?
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.
Why is Spring popular?
Spring simplifies Java development by
- being lightweight and minimally invasive
- loose coupling through Dependency Injection (DI)
- managing dependencies using Inversion of Control (IoC)
- providing declarative programming through aspects
- eliminating boilerplate code with aspects and templates
How is Spring lightweight and minimally invasive?
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.
Explain Spring’s loose coupling through Dependency Injection (DI).
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; }
What is Application Context?
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
What is Spring bean?
A Spring bean is simply a Java Object.
When Java objects are created and managed by Spring container, they are called Spring beans.
What is Spring container?
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.
What does Spring container do?
Spring container
- creates beans
- configures them
- wires them together
- manages their complete lifecycle
How many types of Spring container are there?
There are two types:
- Bean factories- simplest and provides basic support for DI, creation and management of beans
- Application Context- extends BeanFactory interface and provides application framework support such as resolving textual messages from properties file, holds bean information and metadata
What is Inversion of Control (IoC)?
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.
What is Dependency Injection (DI)?
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.
What are the three ways in which dependency can be injected in Spring?
There are 3 ways to perform dependency injection in Spring:
- via constructors
- via setter methods
- Fields in class (@Autowired annotation)
Explain Constructor-based DI.
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.
Explain Setter-based DI.
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.
What is Spring MVC?
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.