Showing posts with label spring in java. Show all posts
Showing posts with label spring in java. Show all posts

Friday, March 1, 2024

SPRING IN JAVA

 SPRING


  • WHAT IS SPRING 
  • ADVANTAGES 
  • APPLICATIONS

A basic overview of the Spring framework is shown with examples. Rod Johnson invented it in 2003
The Spring framework makes it easy to construct JavaEE applications.
Spring Framework 
A free-floating spring is the shape. It appears to be a framework of frameworks since it 
supports several other frameworks, such as Struts, Hibernate, Tapestry, EJB, JSF, and more. 
Another way 
to look at it as a framework that we may use to tackle various technological problems.
Numerous modules for IOC, AOP, DAO, Context, ORM, WEB MVC, and many more topics are included 
in the Spring framework. We shall learn about these courses on the next page. Let's first 
review dependency injection and IOC.
Why to use spring?
The Spring framework is an example of an open-source Java platform. In June 2003, Rod Johnson 
first publicly released it under the Apache 2.0 license. Spring is a featherweight in terms of viscosity 
and volume. The typical Spring Framework installation takes up around 2 MB of space. The Java EE 
platform may be used to construct web apps with the Spring Framework, but any Java program can 
be created by leveraging its basic features. The POJO-based programming paradigm provided by the 
Spring framework encourages excellent programming practices and facilitates J2EE development.
Inversion of control (IOC) and dependency injection 
By removing dependencies, these design principles aim to simplify programming. They facilitate code 
maintenance and testing. We shall understand this with the aid of the following code.
Here, the place and the employee are closely related to one another. In a scenario involving reverse 
control, we arrive at this.

As a result, the IOC only applies the code rigorously. We won't have to deal with the code 
when we apply our logic in a new setting.
It is the IOC container's responsibility to install the dependency in the Spring framework. 
We contribute metadata to the IOC repository in two formats: annotations and XML files.
Dependency injection benefits include:
makes code easily testable 
loosely linked for easier maintenance.
Advantages of spring framework 
Spring Framework has many advantages. They are as follows:
1)      Predefined templates
Spring provides templates for several technologies, including JDBC, Hibernate, JPA, and 
others. Having said that, a ton of code is unnecessary. hiding these technologies 
underlying mechanisms.
Take the Jdbc Template for example. There is no need to write code to handle exceptions, 
create a connection, create an expression, bind an event, close a connection, etc. To 
execute the query, all we have to do is write code. Therefore, it saves a ton of JDBC 
code.
2)      Loose coupling
        Dependency injection makes Spring apps loosely connected.
3)      Easy to test
Dependency injection makes testing applications easier. Spring apps don't need servers
to run, in contrast to Struts and Enterprise JavaBeans (EJB) applications.
4)      Lightweight
The lightweight framework's low weight is a result of the POJO implementation. The 
Spring Framework does not require the implementation of interfaces or class 
inheritance. That explains why its name is non-intrusive.
5)      Fast development
Spring
The framework's dependency injection and multi-framework support make it easy to
develop JavaEE applications.
6)      Powerful abstraction
Strong abstraction is offered for JavaEE standards, including JMS, JDBC, JPA, and JTA.
7)      Declarative support
For caching, validation, events, and formatting, it offers declarative support.
Applications of spring 
POJO-based
With POJO applications and Spring, developers may create enterprise-level
applications. You may forego using an EJB container as an application server if
you solely use POJOs. An alternative is to use a reliable server container like
Tomcat or any other commercial program.
Modular
Spring is installed modularly. Even with many packages and classes, you only need to
care about what you need and ignore the others.
Integrate with existing frameworks
Rather than building from the ground up, Spring makes extensive use of existing
technologies. These include several JEE, Quartz, JDK timers, logging
frameworks, object relationship management (ORM) frameworks, and other
visualization tools.
Testability
Because environment-dependent code is placed in this framework, testing the application written in 
Spring is negligible. Additionally, using JavaBean-style POJOs makes it easy to use dependency 
injection to inject test data.
Web MVC
The well-designed Spring Web Framework is a good choice as an alternative to too-complex web frameworks like Struts. This MVC framework is based on the web.
alternative web frameworks.
Central Exception Handling
With Spring's simple API, exceptions thrown by technologies such as JDBC, Hibernate,
or JDO may be simply converted into consistent unchecked exceptions.
Lightweight
One common feature of IoC containers is their lightweight nature, especially when
compared to EJB containers. This makes it simple to design and distribute
programs with low memory and CPU requirements.

EXCEPTION HANDLING IN JAVA

 Java Exception Handling


  • EXCEPTION HIERARCHY
  • USER-DEFINED EXCEPTIONS
  • HOW DOES JVM HANDLE EXCEPTION

Exception handling is a highly efficient method of addressing runtime errors in Java programs as it guarantees regular program operation.

We will study the types of Java exceptions and how they differ from one another in this lesson. and unreviewed exclusions.

A non-trivial or unexpected event that occurs during program execution and interferes with, in Java, the usual execution of a program's instructions is called an exception. The software understands how to handle an exception. Every time a procedure throws an exception, an object is produced. An exception object is what is referred to as such. This file contains the name, description, and program status as of the exception occurrence.

The main reasons for the exception occurring are Incorrect user input Device error Network connection failure limitations (external disk storage) Code error and inability to open an accessible file.

In Java, non-trivial or unexpected events that occur during program execution that obstruct regular program execution are referred to as exceptions. The software understands how to handle an exception. Every time a procedure throws an exception, an object is produced. An exception object is what is referred to as such. This file includes details about the exception, including its name, description, and the state of the application at the time it occurred.

The main reasons for the exception occurring are user input Device error Network connection failure limitations (external disk storage)Code error unable to open an accessible file.

Error: An error provides information or indicates or shows a serious problem that no sane software should attempt to detect.

Exception: An exception gives details or points out circumstances that a reasonable program could try to detect.

Exception hierarchy

Every other exception and error type is descended from the Throwable class. One section is overseen by a reception. This class is used by user programs when they need to catch exceptions. The Null-Pointer-Exception is one of these exceptions. The Java Runtime Environment (JRE) itself uses the second branch, Error, to detect runtime-related errors made by the Java Runtime System (JVM). One such error example is the Stack-Overflow-Error.

Types of exception

Exceptions can be classified in two ways: Built-in Exceptions-Challenged Exception-Unhandled Exception-User Defined Exceptions

Let us discuss the above exception which is as follows: 1. Built-in Exceptions - these are exceptions available in Java libraries. These exceptions are useful for explaining certain error situations.

Checked exceptions: Checked exceptions are called compile-time exceptions because the compiler checks for them during compilation.

Unchecked exceptions: these are not checked, in contrast to checked exceptions. The compiler does not search for these exceptions during compilation. To put it another way, a compilation fault won't be triggered whether we handle or report an unchecked exception that a program raises.

User-Defined Exceptions:

Regretfully, there are times when Java's built-in exceptions just aren't sufficient. Thus, "user-defined exceptions" might also be made by users.

The advantage of using exception handling in Java is Program completion.

We can easily identify the errors in the code and handle them using code-Propagation of errors-Important error message-Identification of error typescript methods Exception information.

1.      printStackTrace()

 The Name of the Exception, Stack Trace, and Description of the Exception are all included in the method's formatted output.

Let us try this with a program:

//program to print the exception information using the printStackTrace() method 

 


Output

java. lang.ArithmeticException: / by zero

at sdf.main(File.java:10)
2.      toString()
Exception information is printed via the ToString() function. Name: Exception Description.

Ex. // program that uses the toString() function to output the error information


Output

java. lang.ArithmeticException: / by zero

     getMessage()

 The getMessage() method only prints the exception description.

Ex. // program that uses the getMessage() function to output the exception information 

 

Output

by zero

How does JVM handle Exception?

A method's default response to an exception is to generate an exception object and send it to the Java Virtual Machine (JVM). The name, description, and current state of the application, along with any other relevant data, are all contained in the exception object. When the runtime system creates and handles an exception object, an exception is "thrown". It's possible that several methods were called to enable the problematic method. This collection of techniques is called the Call Stack. This is when something else occurs.

The runtime system looks for a method in the call stack that contains the exception handling code. This portion of code is also known as the exception handler.

The runtime system searches for the code that threw the exception by starting at the top of the call stack and moving down.

When it finds a suitable handler, it steps through the exception whom it The thrown object type of an exception must match the exception's type for a handler to be considered suitable.

The exception is issued again if after the runtime system has examined every method on the call stack, no suitable handler is discovered. To the default exception handler of the runtime system. Should an unusual program termination occur, this handler will output pertinent exception data in the manner shown below.

Exception in thread "xxx" Name of Exception: Description
... ...... ..  // Call Stack

Here we will examine an example that shows how the runtime system searches the call stack for the correct 
code to handle exceptions.



                  } 
}
Output 
by zero

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular