Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to Execute something after a Spring Bean is Initialized

How to Execute something after a Spring Bean is Initialized

Posted on December 11, 2024December 23, 2024 by Toma Velev

You can use several methods to Execute something after a Spring Bean is Initialized but the most popular one is the @PostConstruct annotation.

Here’s an example:

import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @PostConstruct
    public void init() {
        System.out.println("My bean is initialized");
        // Your code here
    }
}

In this example, The Spring Engine will execute  the init() after Spring has finished configuring the bean and all fields are Initialized.

Note that this annotation is part of the Java EE standard, but Spring also supports it. This means you can use @PostConstruct in your Spring application just like you would in a Java EE environment.

Also, keep in mind that the order of initialization is not guaranteed. If you have multiple beans with @PostConstruct methods, the order in which they are called is not defined.

If you need to execute code after all beans have been initialized, you can use the @PostLoad annotation (if available) or implement a custom bean post processor.

Here’s an example of how to create a custom bean post processor:

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean " + beanName + " is initialized");
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

Then you need to register this post processor in your Spring application context:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public MyBeanPostProcessor myBeanPostProcessor() {
        return new MyBeanPostProcessor();
    }
}

In this example, the postProcessAfterInitialization method will be called after each bean is initialized.

For more Spring articles, check out https://programtom.com/dev/category/software-development/spring/

  • Skipping AI? You’re a Relic – Time to Evolve or Perish!
  • 2026 Flutter Launch Blueprint: Your 10-Step Checklist to App Store Domination
  • Product Requirements Document – for different software development levels
  • The Bottleneck Factory: AI Production Line vs. Human Quality Gate
  • Most Important Things You Could Code in 2026

Categories

  • Apps (25)
  • ChatGPT (26)
  • Choosing a Framework (38)
  • Flutter (280)
  • Graphical User Interface (14)
  • Marketing (119)
  • Software Development (292)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (16)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (87)
    • Flutter Apps (26)
    • GPT (4)
    • Java (39)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (36)
    • Utils (15)
    • Vaadin 24+ (28)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Skipping AI? You’re a Relic – Time to Evolve or Perish!
  • 2026 Flutter Launch Blueprint: Your 10-Step Checklist to App Store Domination
  • Product Requirements Document – for different software development levels
  • The Bottleneck Factory: AI Production Line vs. Human Quality Gate
  • Most Important Things You Could Code in 2026

Post Categories

  • Apps (25)
  • ChatGPT (26)
  • Choosing a Framework (38)
  • Flutter (280)
  • Graphical User Interface (14)
  • Marketing (119)
  • Software Development (292)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (16)