Mr. Journo
Home Education Spring Framework and Its Features in Java
Education

Spring Framework and Its Features in Java

by Digital raghul - 26 Jul 2022, Tuesday 231 Views Like (0)
Spring Framework and Its Features in Java

Spring Framework and its Features in java

In this article, we are going to talk about  Spring  In java and its types and features in an easy manner for beginners. Spring is one of the most popular Frameworks in java. Let’s get into the Article.


Spring Intro

An open-source framework called Spring was developed to handle the complexities of developing enterprise applications. The Spring framework's layered design is one of its main benefits because it offers a cohesion foundation for J2EE application development while also allowing you to choose which of its components you utilize.

Features


Lightweight

When it comes to size and transparency, spring is thin. The spring framework's basic version is about 1MB in size. Additionally, the processing overhead is really minimal.


Switching the control (IOC)

  • The fundamental idea behind Dependency Injection or Inversion of Control is that developers only need to specify how objects should be produced rather than actually creating them.
  •  There is no need to manually link your program's components and services together; instead, simply list which services each of your components requires in an XML or configuration file. The binding of everything is then the responsibility of the Spring IOC container.


Aspect-oriented (AOP)

Aspect-oriented programming is supported by Spring.

The programming approach known as "aspect-oriented programming" separates the business logic of the primary application from any auxiliary or supporting operations. AOP is a promising approach for object-oriented programming that makes it easier to separate cross-cutting concerns. This increases the modularity of the application and makes maintenance much simpler.

Container???????

Application objects' life cycles and configuration are handled by Spring.

Framework MVC

Using the fundamental Spring technology as a foundation, Spring includes an MVC web application framework. This framework supports several display technologies such as JSP, Velocity, Tiles, iText, and POI and is extensively configurable via strategy interfaces. But Spring MVC Framework can easily be replaced with different frameworks.

Transaction Control

A general abstraction layer for transaction management is provided by the Spring framework. This makes it simple to demarcate transactions without having to deal with low-level concerns and enables the developer to add the pluggable transaction managers. The transaction capability provided by Spring can be used in contexts without containers and is not restricted to J2EE environments.

 Architecture

Spring AOP

The AOP framework is one of Spring's essential elements. Spring is when AOP is used:


1. To offer declarative business services, particularly to replace EJB declarative services Declarative transaction management, which expands on Spring's transaction abstraction, is the most significant example of such a service.

2. To enable users to incorporate unique features, enhancing their OOP usage with AOP


Spring ORM

Database access is related to the ORM package. Well-known object-relational mapping APIs like JDO, Hibernate, and iBatis, offer integration layers.

Spring Web

 The web application development stack for Spring, which also contains Spring MVC, includes the Spring Web module.

Spring DAO

The main purpose of Spring's DAO (Data Access Object) support is to standardize the data access work that uses JDBC, Hibernate, or JDO.

Spring Situation

By adding support for message sources, the Observer design pattern, and the capability for application objects to access resources using a unified API, this package expands upon the beans package.

Spring Web MVC

The MVC implementations for web applications are provided by this module.

Spring Core

The most crucial part of the Spring Framework is the Core package.

The characteristics of Dependency Injection are provided by this component. The Bean Factory offers a factory architecture that isolates your actual program logic from dependencies like object initialization, creation, and access.


Types of Advice:

1. Before to Advice: it runs prior to a join point.

2. After Returning Advice: it starts running once a join point has finished without throwing an exception.

3. After Throwing Advice: it is called if the function throws an exception before exiting.

4. Regardless of the result, it executes after a join point.

5. Around Advice: It runs both prior to and following a connecting point.


Dependency Injection(IOC) in Spring

Spring AOP:

The fundamental idea behind the dependency injection pattern commonly referred to as the Inversion of Control pattern, is that instead of creating your objects, you should specify how they should be produced. You indicate which services are required by which components in a configuration file rather than explicitly connecting your components and services together in code. Then, the task of connecting everything is carried out by a container (in the case of the Spring framework, the IOC container).


Applying IoC means that each object in the system is coordinated by an external entity, which provides objects with their dependencies at the moment of creation. Dependencies are therefore introduced within objects. IoC stands for "inversion of responsibility," and it refers to how an item receives references to cooperating objects.

Example:

 Lets we have two classes-Car and an Engine. The car has an object Engine.

Usual manner:

An object can be instantiated in a variety of ways. Using a new operator is an easy and typical method.

In this case, the Car class has an object for the engine, which we instantiated using the new operator.

Utilizing Dependency Injection:

These days, we delegate instantiation to a third party and give them the task of doing so. In order for the car to function, the engine's object must be provided by a third party. The chosen third party chooses the type to use and the instantiation time for the instance. A third party injects the class Car and class Engine dependencies. This agreement as a whole also includes configuration information. The name of the entire procedure is dependency injection.


Why AOP?

  • It offers a pluggable approach to incorporate a concern before to, following, or surrounding business logic.
  • Let's examine using logs as our guide. You've added logging to many classes, but for some reason, if you want to remove it now, you'll need to make changes to all of the classes. However, using an aspect will make this problem go away quickly. Simply unplug the component if you want to remove logging.


AOP principles

Aspect:

 A class called an Aspect implements issues that span many classes, such as logging. Just a name, really.

Joint Point: 

This is a stage in a program's execution, such as when a method is carried out. A join point describes a method execution in Spring AOP at all times.

Advice:

 At the specific join point, an aspect took the action. As an illustration, add logging before the getting employee name() method is executed. We are using the before guidance in this instance.

Pointcut:

A pointcut is an expression that chooses how advice will be executed at the matched joint point. AspectJ pointcut expression language is what Spring uses by default.

Weaving: 

Weaving is the process of constructing proxy objects from the target object.


Dependency Injection in Spring:

1. Assures that service configuration and usage are distinct.

2. The implementations can be changed by altering the settings.

3. Increases Testability by allowing the injection of mock dependencies.

4. It is simple to spot dependencies.

5. There's no need to read code to find out which dependencies your function communicates with.


Dependency Injection Types:

Setter Injection:

 After executing a no-argument function Object() { [native code] } or no-argument static factory function to instantiate your bean, you may implement setter-based DI by calling the setter methods on your beans.

Constructor Injection:

A function Object() { [native code] } is invoked with a number of parameters, each of which represents a collaborator, to accomplish constructor-based DI.

Interface Injection: 

When using interface-based dependency injection, we create an interface and receive the instance when we implement it.