Execute Method on Startup in Spring

Execute method on startup in Spring

If by "application startup" you mean "application context startup", then yes, there are many ways to do this, the easiest (for singletons beans, anyway) being to annotate your method with @PostConstruct. Take a look at the link to see the other options, but in summary they are:

  • Methods annotated with @PostConstruct
  • afterPropertiesSet() as defined by the InitializingBean callback interface
  • A custom configured init() method

Technically, these are hooks into the bean lifecycle, rather than the context lifecycle, but in 99% of cases, the two are equivalent.

If you need to hook specifically into the context startup/shutdown, then you can implement the Lifecycle interface instead, but that's probably unnecessary.

Running code after Spring Boot starts

Try:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {

@SuppressWarnings("resource")
public static void main(final String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

context.getBean(Table.class).fillWithTestdata(); // <-- here
}
}

What is the best way to run some method only on FIRST startup of Spring application?

I would suggest that you use the ApplicationReadyEvent. According to the documentation, the ApplicationReadyEvent is an:

Event published as late as conceivably possible to indicate that the application is ready to service requests.

So, you could implement your own ApplicationListener listening for the ApplicationReadyEvent and run your code only when the application is ready, for example:

@Component
@Order(0)
class CustomApplicationListener implements ApplicationListener<ApplicationReadyEvent> {

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// run your code
}

}

How to run method once on startup in Spring without @PostConsrtuct/init/@EventListener?

Well, I should check behaviour of CommandLineRunner in Spring App before posting question. It looks like CommandLineRunner perfectly working even not in the spring boot application. So CommandLineRunner perfectly fits all my needs.

Tasklet execute method called during spring boot startup

By default, Spring Boot executes any job in your application context at startup. So I guess that's why you see your tasklet being executed twice: once at application startup and once when you invoke the API.

If you want to disable running jobs at startup you need to set the the property spring.batch.job.enabled=false.

Call a method after Spring Boot app starts

Check for the @PostConstruct for e.g here https://www.baeldung.com/running-setup-logic-on-startup-in-spring

Execute Method on Spring start

You can implement InitializingBean which has a method named afterPropertiesSet(). This method will be called after all properties placeholders are replaced.

Executing a Java class at application startup using Spring MVC

There's not necessarily a "best" way. As usual, there are many ways to do it, and the "best" is whichever fits into your project the best:

  1. Use init-method="..." on a bean element in XML, as cjstehno mentioned
  2. Implement Spring's InitializingBean interface. When deployed in an ApplicationContext, the afterPropertiesSet() method will be called when the bean is created.
  3. Annotate a method on a bean with @PostConstruct. Again, if deployed to an ApplicationContext, the annotated method will be called when the bean is created.
  4. If your bean is more of an infrastructure bean to be tied into the Spring lifecycle, implement ApplicationListener<ContextRefreshedEvent>. The onApplicationEvent(..) method will be called during Spring's startup, and you can do whatever work you need there.

Run a method only at Spring Application Context startup?

Use something like the following code:

@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
// do your stuff here
}
}

Of course StartupListener will need to be within the component scan's reach

Take note however that if your application uses multiple contexts (for example a root context and a web context) this method will be run once for each context.



Related Topics



Leave a reply



Submit