Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll show how to programmatically restart a Spring Boot application.

Restarting our application can be very handy in some cases:

  • Reloading config files upon changing some parameter
  • Changing the currently active profile at runtime
  • Re-initializing the application context for any reason

While this article covers the functionality of restarting a Spring Boot application, note that we also have a great tutorial about shutting down Spring Boot applications.

Now, let’s explore different ways we can implement the restart of a Spring Boot application.

2. Restart by Creating a New Context

We can restart our application by closing the application context and creating a new context from scratch. Although this approach is quite simple, there are some delicate details we have to be careful with to make it work.

Let’s see how to implement this in the main method of our Spring Boot app:

@SpringBootApplication
public class Application {

    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        context = SpringApplication.run(Application.class, args);
    }

    public static void restart() {
        ApplicationArguments args = context.getBean(ApplicationArguments.class);

        Thread thread = new Thread(() -> {
            context.close();
            context = SpringApplication.run(Application.class, args.getSourceArgs());
        });

        thread.setDaemon(false);
        thread.start();
    }
}

As we can see in the above example, it’s important to recreate the context in a separate non-daemon thread — this way we prevent the JVM shutdown, triggered by the close method, from closing our application. Otherwise, our application would stop since the JVM doesn’t wait for daemon threads to finish before terminating them.

Additionally, let’s add a REST endpoint through which we can trigger the restart:

@RestController
public class RestartController {
    
    @PostMapping("/restart")
    public void restart() {
        Application.restart();
    } 
}

Here, we’ve added a controller with a mapping method that invokes our restart method.

We can then call our new endpoint to restart the application:

curl -X POST localhost:port/restart

Of course, if we add an endpoint like this in a real-life application, we’ll have to secure it as well.

3. Actuator’s Restart Endpoint

Another way to restart our application is to use the built-in RestartEndpoint from Spring Boot Actuator.

First, let’s add the required Maven dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-cloud-starter</artifactId>
</dependency>

Next, we have to enable the built-in restart endpoint in our application.properties file:

management.endpoint.restart.enabled=true

Now that we have set up everything, we can inject the RestartEndpoint into our service:

@Service
public class RestartService {
    
    @Autowired
    private RestartEndpoint restartEndpoint;
    
    public void restartApp() {
        restartEndpoint.restart();
    }
}

In the above code, we are using the RestartEndpoint bean to restart our application. This is a nice way of restarting because we only have to call one method that does all the work.

As we can see, using the RestartEndpoint is a simple way to restart our application. On the other side, there is a drawback with this approach because it requires us to add the mentioned libraries. If we aren’t using them already, this might be too much overhead for only this functionality. In that case, we can stick to the manual approach from the previous section since it requires only a few more lines of code.

4. Refreshing the Application Context

In some cases, we can reload the application context by calling its refresh method.

Although this method might sound promising, only some application context types support refreshing an already initialized context. For example, FileSystemXmlApplicationContext, GroovyWebApplicationContext, and a few others support it.

Unfortunately, if we try this in a Spring Boot web application, we will get the following error:

java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts:
just call 'refresh' once

Finally, although there are some context types that support multiple refreshes, we should avoid doing this. The reason is that the refresh method is designed as an internal method used by the framework to initialize the application context.

5. Conclusion

In this article, we explored a number of different ways how to restart a Spring Boot application programmatically.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW