1. Overview

In this tutorial, we’ll analyze the technique to list all the classes loaded by a specific class loader in Java, using the Java Instrumentation API. We’ll also see how to create and load a Java agent to acquire an Instrumentation instance and invoke the required methods to accomplish our task.

2. Class Loaders in Java

The class loaders are an integral part of the JRE (Java Runtime Environment). Their job is to dynamically load classes into the Java Virtual Machine. In other words, they load classes into memory on-demand when required by the application. The article on Java class loaders talks about their different types and provides a detailed understanding of how they work.

阅读全文 »

1. Introduction

In this article, we’ll discuss in detail a core concept in Java – the lifecycle of a thread.

We’ll use a quick illustrated diagram and, of course, practical code snippets to better understand these states during the thread execution.

To get started understanding Threads in Java, this article on creating a thread is a good place to start.

2. Multithreading in Java

In the Java language, multithreading is driven by the core concept of a Thread. During their lifecycle, threads go through various states:

img](https://www.baeldung.com/wp-content/uploads/2018/02/Life_cycle_of_a_Thread_in_Java.jpg)

阅读全文 »

1. Introduction

In this tutorial, we’ll learn about the event support mechanism provided by the Spring framework. We’ll explore the various built-in events provided by the framework and then see how to consume an event.

To learn about creating and publishing custom events, have a look at our previous tutorial here.

Spring has an eventing mechanism which is built around the ApplicationContext. It can be used to exchange information between different beans. We can make use of application events by listening for events and executing custom code.

For example, a scenario here would be to execute custom logic on the complete startup of the ApplicationContext.

阅读全文 »

1. Overview

In this tutorial, we’ll have a look at various ways to override properties in Spring’s tests.

Spring actually provides a number of solutions for this, so we have quite a bit to explore here.

2. Dependencies

Of course, in order to work with Spring tests, we need to add a test dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>

This dependency also includes Junit 5 for us.

阅读全文 »

1. Overview

In this tutorial, we’re going to learn different ways to use shutdown callbacks with Spring.

The main advantage of using a shutdown callback is that it gives us control over a graceful application exit.

2. Shutdown Callback Approaches

Spring supports both the component-level and the context-level shutdown callbacks. We can create these callbacks using:

  • @PreDestroy
  • DisposableBean interface
  • Bean-destroy method
  • Global ServletContextListener

Let’s see all of these approaches with examples.

阅读全文 »