DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

spring-007: classloader-in-java-vs-spring

ClassLoader Concept in Java

ClassLoader in Java is a part of the Java Runtime Environment responsible for dynamically loading classes into the Java Virtual Machine (JVM). Java applications use ClassLoader to load class files when required, instead of loading all classes at startup.

Key Features:

  1. Dynamic Loading: Classes are loaded on demand when referenced.
  2. Delegation Model: Follows a parent-delegation model where the request to load a class is passed to the parent ClassLoader before attempting to load it locally.
  3. Class Isolation: Helps to maintain class isolation, especially useful in complex systems like application servers.

Types of ClassLoaders:

  1. Bootstrap ClassLoader:
    • Loads core Java classes from the java.lang, java.util, etc.
    • Part of the JVM and implemented natively.
  2. Extension ClassLoader:
    • Loads classes from the Java extension directories (jre/lib/ext).
  3. Application/System ClassLoader:
    • Loads classes from the application's classpath (CLASSPATH environment variable).
  4. Custom ClassLoaders:
    • User-defined ClassLoaders used to extend or modify the default loading behavior.

ClassLoader Methods:

  • findClass(String name): Finds a class with the specified name.
  • loadClass(String name): Loads the class using the delegation model.
  • defineClass(byte[] b): Converts an array of bytes into a Class instance.

ClassLoader Hierarchy:

Bootstrap ClassLoader
       ↑
Extension ClassLoader
       ↑
Application ClassLoader
       ↑
Custom ClassLoader (if any)
Enter fullscreen mode Exit fullscreen mode

ClassLoader in Spring

In the Spring Framework, the ClassLoader concept is crucial because Spring applications dynamically load classes and manage resources at runtime. Spring integrates with Java's ClassLoader mechanism to enhance class and resource loading.

Use Cases in Spring:

  1. Loading Beans and Configurations:

    • Spring uses ClassLoader to load beans defined in the configuration files or classes annotated with @Component.
  2. Loading Resources:

    • Spring provides a ResourceLoader abstraction to load files, URLs, or classpath resources. Behind the scenes, it uses the ClassLoader to resolve resources.

Example:

   Resource resource = new ClassPathResource("application.properties");
Enter fullscreen mode Exit fullscreen mode
  1. Hot Reloading:

    • Tools like Spring Boot DevTools use custom ClassLoaders to reload classes dynamically during development.
  2. Custom ClassLoader Scenarios:

    • When deploying Spring applications in containers or microservices, custom ClassLoaders are often implemented to isolate dependencies (e.g., in Tomcat or Kubernetes).
  3. Aspect-Oriented Programming (AOP):

    • Spring's AOP uses proxy classes, which are often loaded dynamically via the ClassLoader.

Spring's ClassLoader Abstraction:

  • Spring's DefaultResourceLoader wraps the ClassLoader to simplify resource loading.
  • ContextClassLoader is often used to access the ClassLoader for specific threads, allowing Spring to integrate smoothly with third-party libraries.

Common Example:

Using ClassLoader to load a bean definition from a file:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("bean-definitions.xml");
Enter fullscreen mode Exit fullscreen mode

Key Differences in Java and Spring:

Aspect Java ClassLoader Spring ClassLoader
Purpose Load Java classes dynamically. Used for resource loading and Spring-specific operations.
Implementation Follows Java’s delegation model. Wraps and enhances Java’s ClassLoader with abstractions.
Customization Allows creating custom ClassLoaders. Integrates with Java ClassLoader and provides utility APIs.
Use Cases Loading classes, jars, and libraries. Loading beans, resources, proxies, and configuration files.

Top comments (0)