【 note 】 this article translation: https://www.theserverside.com/tip/Library-vs-framework-How-these-software-artifacts-differ

Libraries are low-level components that provide a specific function, such as establishing a network connection. A Framework is a known programming environment, such as Spring Boot. When software leaders start building new enterprise applications, they must decide which set of libraries and frameworks to use. This raises an obvious question: What’s the difference between a software library and a framework? Both libraries and software frameworks help with application development. However, the main difference between the two is the scope of their tasks and the way they ease the burden of writing code for developers. Libraries provide components, classes, and methods that developers can call to perform specific functions. In contrast, the code provided by the framework already performs the normally required functionality and invokes the developer-provided code when custom functionality is needed.

Libraries and frameworks

A library is a set of related low-level components that a developer invokes to achieve a specific result. Common library functions include date formatting and establishing a network connection.

The framework deals with higher-level problems. The framework provides an established programming environment that is itself built on top of low-level libraries.

For example, the framework might deal with how best to map all objects in an application to tables in a related database. It also addresses how to deliver a rich, Web-based experience to end users.

Software Library Examples

An example of a library is the Java Date and Time API. It is a set of classes and interfaces that define properties and methods that developers can use to format dates, perform time zone convertions, and provide global calendar support. Java developers instantiate the Date and Time classes as needed and call the library’s methods in any way they see fit. Another example of a Java library is the popular java.net network I/O package. The library consists of dozens of classes and interfaces that developers use to open network ports, handle incoming connections, and send data to interconnected clients. Simply using the java.net package, a software developer can implement all the logic necessary to handle a REST-based Web service or HTML-based request-response loop over the HTTP protocol. The java.net library provides a set of low-level APIs that any developer can use to develop applications that communicate over the network.

Popular Java libraries

Other popular Java libraries, including Spring Boot, JHipster, or Vaadin, which already take advantage of the Java framework, include:

  • Apache Commons Math
  • BouncyCastle for cryptography
  • Java Advanced Imaging
  • Java Speech
  • Java 3D
  • JavaMail
  • Joda-Time
  • Apache Collections
  • Jackson for JSON and XML
  • Java Networking

The role of software frameworks

Enterprise projects typically require developers to provide a set of RESTful APIs for external clients to integrate with online applications. However, many developers have not only figured out how to use low-level Java.net libraries to handle RESTful API calls, but have also shared their work as open source projects. This is the nature of software frameworks. It is an existing project that solves a common and challenging problem that developers can use in their own projects.

A framework is an existing project that solves a common and challenging problem that developers can use in their own projects.

There are usually many frameworks to choose from within a given problem domain. There are a number of popular frameworks in the Java ecosystem that use the standard Java and Jakarta EE libraries to address the question of how best to handle the REST-based request-response cycle, including:

  • Spring Boot
  • Jersey
  • RESTEasy
  • Restlet
  • Micronaut
  • Apache CFX
  • JAX-RS

The frame has a point

Another key characteristic that distinguishes a framework from a repository is that the latter usually includes ideas about how to solve a given problem, the scope of the problem, and how to solve the framework’s goals in the most efficient and effective way. The framework will compete on various non-functional requirements, such as ease of use, performance, pluggability, and compatibility.

Framework and Inversion of Control

Finally, a key differentiating factor between libraries and frameworks is the relative inversion of control (IOC) between the two. The IOC is often referred to as the Hollywood Principles. Unlike libraries, frameworks take a “don’t call us, we’ll call you” approach to code written by developers. To develop applications using libraries, developers must instantiate library components and call methods on the classes and interfaces that exist in them. For example, if developers wanted to use the java.net library to create their own RESTful applications, they would need to call various APIs to open ports, handle HTTP connections, format data as XML or JSON representations to communicate back and forth, And initiate a request when the client initiates the request. The framework, on the other hand, is responsible for supporting all the underlying pipes needed to support the core functionality of the project. The framework invokes code provided by the developer only when needed.

The IoC and Spring the Boot

RESTful Web services written with Spring Boot are a good example of the IOC’s Hollywood principles. With Spring Boot RESTful Web services, developers only need to write the logic to be executed when an HTTP-based GET call occurs. When a GET call occurs, the Spring Boot framework processes it, interprets it, converts the data exchange format to JSON or XML, and then routes the RESTful request to the developer’s business logic. Developers only need to write the business logic. It is up to the framework to decide when to invoke business logic. IoC frameworks are common in Java. Here are ten common examples.

  • Hibernate
  • Struts
  • JavaServer Faces
  • Play for Scala
  • Vaadin
  • JHipster
  • JRuby
  • JUnit
  • Quarkus
  • Grails

The framework is used together with the library

Although frameworks and libraries do different things, they can both help developers get more done. No software professional wants to waste time reinventing the wheel when many common programming challenges have been solved and shared in the form of licensed open source projects. Whenever you start a new software development project, it is important to choose a proven and established software development framework. At the same time, be consistent with your personal development and design philosophy. In the rare case where a particular situation is outside the scope of your chosen framework, the standard library is always there to help your developers bridge the gap.