Open In App

Invasive and Non-Invasive Frameworks in Java

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are two categories under which Java technologies or frameworks can be placed:

  1. Invasive Technologies / Frameworks
  2. Non-Invasive Technologies / Frameworks

Invasive Framework

Invasive frameworks are those that require application classes to extend framework API-provided classes or implement framework API-provided interfaces while making any framework-based application.

Making application classes that implement or extend from framework-provided API interfaces is restricted. Application classes cannot be moved to another technology or framework to reuse their logic since they are tightly connected with the technology or framework’s API.

In-short invasive frameworks are:

  • The developer class will extend or implement an interface given by the framework API.
  • Because of extends and implementation, the developer code would be tightly coupled with framework API.
  • It won’t promote portability(moving the classes to a new framework would not execute).
  • Examples: Servlet, Struts(1.X).

Example Using Java Servlet:

Java




// Invasive Framework Example: Java Servlets
  
// Import the necessary classes from the Servlet API
import javax.servlet.*;
import javax.servlet.http.*;
  
// Create a servlet by extending the HttpServlet class
public class MyServlet extends HttpServlet {
  
    // Handle GET requests
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        // Code to handle GET request
    }
      
    // Handle POST requests
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        // Code to handle POST request
    }
}


Explanation: In the first code snippet, we have an example of an invasive framework using Java Servlets. The MyServlet class extends the HttpServlet class provided by the Servlet API. It overrides the doGet() and doPost() methods to handle GET and POST requests, respectively. The code is tightly coupled with the Servlet API, making it difficult to switch to a different web framework.

Limitation of invasive framework:

Application classes created for invasive frameworks must always be used with such frameworks. We cannot switch to a different framework. If we used Servlet technology when developing our program and better Java technology emerged later, we would be unable to switch to that technology. Our program’s classes are complex classes linked to the Servlet API, not typical classes. We are forced to utilize that Servlet-based program indefinitely, or start from scratch and create another application using new technologies. Once we’ve decided on an invasive framework to build our application, we have to stick with it through the entire process.
Example: JDBC, Servlet, EJB (old), and so on.

Non-Invasive Framework

If there is no limitation on building classes for framework-based applications that implement or extend from framework API interfaces/classes, then such a framework is referred to as a non-invasive framework.

Since the program’s classes in this case are only weakly tied to the technology/framework’s provided API, we may reuse our application logic by moving these classes to another technology/framework. Application classes must be converted to and used with any framework or technology that is comparable before being migrated to non-invasive frameworks or technologies.
In-short non-invasive frameworks are:

  • The developer class will not extend or implement any interface given by the framework API.
  • No extends and implements keyword, the developer code would be loosely coupled with framework API.
  • It promotes portability(moving the classes to a new framework would execute).
  • Examples: Spring, JSF, Hibernate, etc. There is no non-invasive Java framework as of now.

Example using Spring Framework:

Java




// Non-invasive Framework Example: Spring Framework
  
// Create a service class
public class MyService {
    private MyRepository repository;
      
    // Dependency Injection via constructor
    public MyService(MyRepository repository) {
        this.repository = repository;
    }
      
    // Method in the service class
    public void doSomething() {
        // Use the repository to
          // perform an action
        repository.saveData();
    }
}


Explanation: In this code snippet, we demonstrate a non-invasive framework using the Spring Framework. The MyService class is a regular Java class that does not inherit from any framework-specific classes. It accepts a MyRepository dependency via constructor injection. The doSomething() method uses the injected repository to perform an action. This approach allows for the decoupling of code from the framework, making it more flexible and modular.



Similar Reads

7 Best Testing Frameworks for Java Developers
Java is one of the most popular languages for programming & development and a wide range of applications is developed in this particular language. And when an individual opts for making a career in Java, he is required to acquire the knowledge of testing frameworks also to develop secure and efficient applications or software. The prime motive
5 min read
7 Best Frameworks For Full Stack Java Developers
Being one of the oldest and widest used programming languages, Java is being considered for all scale industries from small to medium to large and that’s why companies like Spotify, Netflix, Amazon, and many others are still relying on this “old school” Java Programming Language. Its nature of being robust, versatile, and highly scalable has made i
10 min read
Top 10 Most Popular Java Frameworks for Web Development
Do you know more than 50 million websites including some most popular ones like Google, LinkedIn, eBay, Amazon, and Stack Overflow are written extensively using which language? the answer is nothing other than the most popular Java. Java just reached its Silver Anniversary but Still Ruling the software industry with 'Old is Gold' kinda reliability.
7 min read
10 Most Popular Java Frameworks That You Must Try
In today's technology-driven world, we all can see how frequently tech advancements are coming into existence and subsequently how old tools & technologies are becoming obsolete and uncompetitive. But the case is not the same with every single technology as JAVA is one of those few technologies out there that has been enjoying a strong position
9 min read
Top 5 Open Source Java Frameworks in 2024
Almost 30 years and still Ruling the software industry, over the years Java has ranked among the top three most popular programming languages in the world with numerous applications, including back-end development projects, big data and machine learning projects, and obviously, web and Android development. If you are a developer and searching for t
8 min read
Top 10 Java Frameworks You Should Know in 2024
In today's digital world, web applications play a crucial role across various industries. Building robust and scalable applications often requires choosing the right development tools. Among these tools, Java frameworks stand out for their efficiency and reliability, making them a popular choice for enterprise-level development. This article explor
8 min read
5 Best Java Frameworks For Microservices
Microservices are extensively being used to create complex applications with multi-functionality by combining every piece and putting them layer by layer in a single unit. Many of us might not be aware of the fact that Microservices is an approach to crafting a single app in a set of small services where each service runs on its own (process). In o
6 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Difference between static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access any static method and static variable, without cr
6 min read
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is declared as static, then a single copy of the vari
4 min read
Article Tags :
Practice Tags :