Home > Software engineering >  Print Hello World before Spring boot application start
Print Hello World before Spring boot application start

Time:09-27

Hello guys I want to ask if its possible to execute the print before the spring boot application start?

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SampleApplication implements CommandLineRunner {

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

    @Override
    public void run(String... args) {
        System.out.print("I would be appear in log first before spring application starts");
    }

}

My problem of my code it will display the print after the spring boot application start

Current Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.2)

2021-09-26 06:13:53.267  INFO 10352 --- [           main] c.s.d.SampleApplicationTests      : Starting SampleApplicationTests using Java 11.0.11 on User-Desktop with PID 10352 (started by ???)
2021-09-26 06:13:53.277  INFO 10352 --- [           main] c.s.d.Sample




ApplicationTests      : No active profile set, falling back to default profiles: default
2021-09-26 06:13:54.879  INFO 10352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-09-26 06:13:55.051  INFO 10352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 148 ms. Found 9 JPA repository interfaces.
2021-09-26 06:13:57.118  INFO 10352 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-09-26 06:13:57.299  INFO 10352 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-09-26 06:13:57.563  INFO 10352 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-09-26 06:13:58.268  INFO 10352 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-09-26 06:14:03.062  INFO 10352 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-09-26 06:14:03.094  INFO 10352 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2021-09-26 06:14:08.430  INFO 10352 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-09-26 06:14:08.453  INFO 10352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-09-26 06:14:15.216  INFO 10352 --- [           main] c.s.d.SampleApplicationTests      : Started SampleApplicationTests in 22.607 seconds (JVM running for 24.991)
I would be appear in log first before spring application starts

CodePudding user response:

Why don't you just print the log in main method before run invocation?

@SpringBootApplication
public class SampleApplication implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.print("I would be appear in log first before spring application starts");
        SpringApplication.run(SampleApplication.class, args);
    }

    @Override
    public void run(String... args) {
    }

}

run method is a method from Spring Boot so everything you do there is done after Spring Boot's start.

CodePudding user response:

If by before the spring boot application start you mean application context startup, then yes, the easiest (for singletons beans, anyway) being to annotate your method with @PostConstruct.

@PostConstruct
    public void init() {
        //do something
    }

Check for the @PostConstruct for e.g here.

  • Related