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 not mentioned in this tutorial but the three mentioned in this tutorial 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 didInitialized() 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 Application Context is ready.

Let’s have a look at a simple example on how to listen for an event ApplicationReadyEvent which is 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 to learn more about Spring Boot, have a 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 *