spring Flashcards

1
Q

What is Spring Framework?

A

Spring is an umbrella term
for a family of powerful open-source frameworks
which can be used to rapidly create loosely Java applications for reducing the complexity of developing

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

Spring Frameworks - Overview

A

The Spring family of frameworks consist of close to 20 modules,
each focusing on a particular task or service

These are grouped into the following layers: 
1- Core Container
2- Data Access/Integration
3- Messaging 
4- Web 
5- AOP (aspect orientation programing)
6- Aspects 
7- Instrumentation 
8- Test
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Core Container IOC

A

The Spring IOC container is the core of Spring Framework.

The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods.

The design principle of Inversion of Control (IOC) emphasizes keeping the Java classes independent of each other and the container frees them from object creation and maintenance.

The container, use for
1- creating objects using Dependency Injection which called Spring Beans.
2- Wiring them together along with configuring
3- Managing their overall life cycles.

The container uses configuration metadata which represent by
Java code / annotations / XML

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

Context

A

This modules builds off from the core and bean modules used for more enterprise functionality.
The main feature, Application Context represents the Spring IoC container and is used to instantiate, configure and assemble beans.

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

SpEL (Spring Expression Language)

A
A module which provides a powerful expression language which can be used to query and manipulate an object graph at runtime, including 
setting and getting property values, 
property assignment, 
method invocation, 
accessing array content, 
collections and indexer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Data Access/Integration

A

The Data Access/Integration layer provides support for database management or layers of abstraction for ease of use.

JDBC (Java Database Connectivity): A module which provides a layer of abstraction for JDBC
ORM (Object Relational Mapping): A module which provides integration layers for ORM APIs, such as JPA, JDO and Hibernate
OXM (Object/XML Mapping): A module which provides a layer of abstraction for mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream
JMS (Java Messaging System): A module which provides feature to produce and consume messages.
Transaction: A module which provides programmatic and declarative support for transaction management in classes that implement special interfaces as well as POJOs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do you mean by IoC (Inversion of Control) Container

A

Spring IoC Container is the core of Spring Framework.
It creates the objects, configures and assembles their dependencies, manages their entire life cycle.
The Container uses Dependency Injection(DI) to manage the components that make up the application.

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

What do you understand by Dependency Injection?

A

The main idea in Dependency Injection is that you don’t have to create your objects but you just have to describe how the object should be created.

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

How many ways of achieving dependency injection

A

2 major ways of achieving dependency injection are
1-Constructor injection:
2- Setter injection:

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

the difference between constructor and setter injection

A
  • In constructor injection, partial injection is not allowed
    whereas it is allowed in setter injection.
  • The constructor injection doesn’t override the setter property
    whereas the same is not true for setter injection.
  • Constructor injection creates a new instance if any modification is done.
    setter can not creation of a new instance injection.
  • if the bean has many properties, then constructor injection is preferred.
    If it has few properties, then setter injection is preferred.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Spring Beans

A

They are the objects forming the backbone of the user’s application and are managed by the Spring IoC container.

Spring beans are instantiated, configured, wired, and managed by IoC container.

Beans are created with the configuration metadata that the users supply to the container (by means of XML or java annotations configurations.)

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

What are different ways to configure a class as Spring Bean?

A

1 - XML-Based configuration: This starts and close with a bean tag

2 - Annotation-Based configuration: the beans can be configured into the component class itself by using annotations on the relevant class, method, or field declaration.

3- Java-based configuration: use of the @Configuration annotated classes and @Bean annotated methods

4-groovy-based configuration, when configuration is file with Groovy code

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

What is autowiring and name the different modes of it?

A

Autowiring enables you to inject the object dependency implicitly.
It internally uses setter or constructor injection.

Autowiring can’t be used to inject primitive and string values.
It works with reference only.

There are many autowiring modes
no , byName, byType, constructor, autodetect

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

Types of IoC Containers in Spring

A

Spring BeanFactory Container

Spring ApplicationContext Container

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

What Is Spring MVC (Model-View-Controller)?

A

Model-View-Controller.
Model component handles the application data

View component handles the presentation layer

Controller handles the incoming requests, creates proper models, and returns the appropriate request to the View

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

What is Spring bean?

A

A bean is an object that is instantiated, wired, and otherwise managed by a Spring IoC container.

These beans are created with the configuration metadata that you supply to the container.

The management of a Spring bean includes:

creating an object
providing dependencies (e.g. other beans, configuration properties)
intercepting object method calls to provide additional framework features
destroying an object
A Spring bean is a fundamental concept of the framework. As a user of Spring, you should have a deep understanding of this core abstraction.

17
Q

Ioc Inversion of control

A

Inversion of control- It means giving the control of creating and instantiating the spring beans to the Spring IOC container and the only work the developer does is configuring the beans in the spring xml file

18
Q

How do you define a class as bean in Spring boot?

A

With the @Autowired annotation we inject our AppName bean into the field. Here we create the AppName bean; the bean is managed by Spring container. While the @Component annotation is used to decorate classes that are auto-detected by Spring scanning, the @Bean annotation is used to explicitly declare a bean creation.

Method 1 : Declaring a bean in XML configuration file
This is the most primitive approach of creating a bean. A bean is declared in Spring’s XML configuration file.
Upon startup, Spring container reads this configuration file and creates and initializes all the beans defined in this file which can be used anytime during application execution.

Method 2 : Using @Component annotation
@Component annotation above a class indicates that this class is a component and should be automatically detected and instantiated.
Hence, for instantiating annotated beans, we need to add following declaration in our Spring XML configuration file :

Method 3 : Using @Configuration annotation
This method does not require any XML file and it can be used to create a bean without XML configuration file.
Create a class which you want to get as a bean, say a user and annotate it with @Configuration as:
19
Q

what annotation put to make the class bean in java

A

Spring @Bean annotation tells that a method produces a bean to be managed by the Spring container.
It is a method-level annotation. During Java configuration ( @Configuration ), the method is executed and its return value is registered as a bean within a BeanFactory .

20
Q

How do you declare a bean?

A

To declare a bean, simply annotate a method with the @Bean annotation or annotate a class with the @Component annotation (annotations @Service , @Repository , @Controller, @RestController could be used as well).

21
Q

What is Spring Bean Scope?

A

singleton
This scopes the bean definition to a single instance per Spring IoC container (default).

prototype
This scopes a single bean definition to have any number of object instances.

request
This scopes a bean definition to an HTTP request.
Only valid in the context of a web-aware Spring Application Context.

session
This scopes a bean definition to an HTTP session.
Only valid in the context of a web-aware Spring Application Context.

global-session
This scopes a bean definition to a global HTTP session.
Only valid in the context of a web-aware Spring Application Context.

22
Q

Bean Lifecycle

A

The management of Beans, conducted by the BeanFactory or Application Context, includes instantiation, configuration and the eventual removal (or destruction) of beans. As a high-level overview:

Beans are first instantiated.
Their properties are set.
Any associated interfaces or objects are made aware of their existence.
The bean is made aware of any associated interfaces as well.
Any other methods, particularly custom created methods, are invoked.
Then the bean is ready for use.
Once the bean is no longer used, it is marked for removal and a destroy method is invoked for the bean
Custom destroy methods are invoked, if any.
Bean is the destroyed.

23
Q

What is a REST API?

A

it is a collection of URLs, which available on the server

www. myserver.com/fetchnotes
www. myserver.com/createnotes
www. myserver.com/deletenotes

each URL is called it a REST endpoint
each REST endpoint perform a certain operation
each REST endpoint also contains a HTTP method (get, post, put, delete)

For HTTP POST and PUT we pass data in Request Body
we use JSON data Format to communicate with REST API
Jackson API help to map JSON to Java Objects

is a set of rules that define how applications or devices can connect to and communicate with each other

A REST API is an API that conforms to the design principles of the REST, or representational state transfer architectural style. For this reason, REST APIs are sometimes referred to RESTful APIs.

24
Q

How REST APIs work

A

REST APIs communicate via HTTP requests to perform standard database functions like creating, reading, updating, and deleting records
All HTTP methods can be used in API calls.
A well-designed REST API is similar to a website
running in a web browser with built-in HTTP functionality.

25
Q

Life Cycle of a Servlet

A

There are three life cycle methods of a Servlet :
init()
service()
destroy()

  • Life Cycle of a Servlet
  • Loading a Servlet.
  • Initializing the Servlet. .init()
  • Request handling. service()
  • Destroying the Servlet. destroy()
26
Q

What do you mean by bean wiring?

A

The act of creating associations between beans within the Spring container

27
Q

What is Bean Factory?

A

Holds Bean Definitions of multiple beans within itself and then instantiates the bean when requested

28
Q

What is dependency injection

A

Dependency Injection (DI) is a design pattern that removes
the dependency from the programming code
so that it can be easy to manage and test the application.

Dependency Injection makes our programming code loosely coupled.

29
Q

Name some of the important Spring Modules?

A

Spring Context – for dependency injection.
Spring AOP – for aspect-oriented programming.
Spring DAO – for database operations using DAO pattern.
Spring JDBC – for JDBC and DataSource support.
Spring ORM – for ORM tools support such as Hibernate.

30
Q

What is the importance of Spring bean configuration file?

A

We use the Spring Bean configuration file to define all the beans that will be initialized by Spring Context.
When we create the instance of Spring ApplicationContext, it reads the spring bean XML file and initializes all of them.

31
Q

What is the fundamental functionality of spring framework

A

Dependency Injection

32
Q

ApplicationContext

A
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage(“Hello World!”);
helloWorld.getMessage();

33
Q

@ComponentScan

A

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

Configuration
@ComponentScan(basePackages = {“com.baeldung.annotations.componentscanautoconfigure.healthcare”,
“com.baeldung.annotations.componentscanautoconfigure.employee”},
basePackageClasses = Teacher.class)