spring Flashcards
What is Spring Framework?
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
Spring Frameworks - Overview
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
Core Container IOC
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
Context
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.
SpEL (Spring Expression Language)
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
Data Access/Integration
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
What do you mean by IoC (Inversion of Control) Container
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.
What do you understand by Dependency Injection?
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 many ways of achieving dependency injection
2 major ways of achieving dependency injection are
1-Constructor injection:
2- Setter injection:
the difference between constructor and setter injection
- 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.
What are Spring Beans
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.)
What are different ways to configure a class as Spring Bean?
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
What is autowiring and name the different modes of it?
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
Types of IoC Containers in Spring
Spring BeanFactory Container
Spring ApplicationContext Container
What Is Spring MVC (Model-View-Controller)?
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
What is Spring bean?
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.
Ioc Inversion of control
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
How do you define a class as bean in Spring boot?
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:
what annotation put to make the class bean in java
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 .
How do you declare a bean?
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).
What is Spring Bean Scope?
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.
Bean Lifecycle
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.
What is a REST API?
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.
How REST APIs work
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.