Run Code When Spring Bean Is Initialized

In this tutorial, you will learn a few ways how to run your code when Spring Bean has initialized. There are other ways that are not mentioned in this tutorial, but the three mentioned here are my favourite.

Using @PostConstruct

One of the ways to run your code right after a Bean has been initialized is to use @PostConstract annotation.

In the below code example, the class MyBean is annotated with @Component annotation. This Bean will be created at application startup time.

Note the use of @PostConstruct annotation. The method didInitialize() is annotated with @PostConstruct annotation, which will instruct Spring Framework to call this method only one time, right after the bean is initialized. The name of the method can be anything you like. Also, please note that this method does not take any arguments.

If you add the below class to your Spring Boot application and run it, you should see a logger output “MyBean is initialized”.

import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MyBean {
    
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @PostConstruct
    private void didInitialize() {
        logger.info("MyBean is initialized");
    }
}

Using the InitializingBean Interface

Another way to run your custom code when a Bean is initialized is to make your Bean implement the InitializingBean interface. Let’s have a look at a very simple example.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class MyBean2 implements InitializingBean {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("MyBean2 is initialized");
    }
}

Notice that the MyBean2 class implements InitializingBean interface and overrides only one method called afterPropertiesSet(). The afterPropertiesSet() method will be called one time only right after the Bean has been initialized. If you add the below class to your Spring Boot application and run it, you should see a logger output “MyBean2 is initialized”.

Using @EventListener Annotation

Run custom code when all Beans have been initialized.

You can also use @EventListener annotation to make your method listen for Spring Application Context events. This way, we can run our code not when a particular Bean is initialized but when all the Beans in our application have been initialized and the Application Context is ready.

Let’s look at a simple example of how to listen for an event ApplicationReadyEvent sent by Spring Framework to indicate that the application is ready to service requests.

ApplicationReadyEventAll Beans have been initialized, and the application is ready to service requests.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean3 {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @EventListener
    public void onApplicationEvent(ApplicationReadyEvent event) {
        logger.info("All Beans have been initizalized. Application is ready to service requests");

    }
}

I hope this very short Spring Boot tutorial was helpful to you and you now know how to run custom code when a specific or even all Beans in your applications have been initialized.

If you are interested in learning more about Spring Boot, look at the below list of online video courses. One of them might help you a great deal to speed up your learning progress.

 

Leave a Reply

Your email address will not be published. Required fields are marked *