Set lazy initialization using SpringApplication and SpringApplicationBuilder
@@ -3,6 +3,7 @@ package com.baeldung.lazyinitialization;
import com.baeldung.lazyinitialization.services.Writer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -20,7 +21,9 @@ public class Application {
}
public static void main(String[] args) {
+
ApplicationContext ctx = SpringApplication.run(Application.class, args);
+
System.out.println("Application context initialized!!!");
Writer writer1 = ctx.getBean("writer1", Writer.class);
@@ -29,4 +32,23 @@ public class Application {
Writer writer2 = ctx.getBean("writer2", Writer.class);
writer2.write("Second message");
}
+
+ /*
+ This method shows how to set lazy initialization and start the application using SpringApplicationBuilder
+ */
+ private static ApplicationContext runUsingSpringApplicationBuilder(String[] args){
+ return new SpringApplicationBuilder(Application.class)
+ .lazyInitialization(true)
+ .build(args)
+ .run();
+ }
+
+ /*
+ This method shows how to set lazy initialization and start the application using SpringApplication
+ */
+ private static ApplicationContext runUsingSpringApplication(String[] args){
+ SpringApplication app = new SpringApplication(Application.class);
+ app.setLazyInitialization(true);
+ return app.run(args);
+ }
}
