diff --git a/spring-core/src/main/java/com/baeldung/lazy/AppConfig.java b/spring-core/src/main/java/com/baeldung/lazy/AppConfig.java new file mode 100644 index 0000000000..0d76876cd8 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lazy/AppConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.lazy; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; + +@Lazy +@Configuration +@ComponentScan(basePackages = "com.baeldung.lazy") +public class AppConfig { + + @Lazy + @Bean + public Region getRegion(){ + return new Region(); + } + + @Bean + public Country getCountry(){ + return new Country(); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lazy/City.java b/spring-core/src/main/java/com/baeldung/lazy/City.java new file mode 100644 index 0000000000..27073b5fe7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lazy/City.java @@ -0,0 +1,13 @@ +package com.baeldung.lazy; + +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +@Lazy +@Component +public class City { + + public City() { + System.out.println("City bean initialized"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lazy/Country.java b/spring-core/src/main/java/com/baeldung/lazy/Country.java new file mode 100644 index 0000000000..dbac1a2bdc --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lazy/Country.java @@ -0,0 +1,8 @@ +package com.baeldung.lazy; + +public class Country { + + public Country() { + System.out.println("Country bean initialized"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lazy/Region.java b/spring-core/src/main/java/com/baeldung/lazy/Region.java new file mode 100644 index 0000000000..5211e5d622 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lazy/Region.java @@ -0,0 +1,19 @@ +package com.baeldung.lazy; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; + +public class Region { + + @Lazy + @Autowired + private City city; + + public Region() { + System.out.println("Region bean initialized"); + } + + public City getCityInstance() { + return city; + } +} diff --git a/spring-core/src/test/java/com/baeldung/Lazy/LazyAnnotationUnitTest.java b/spring-core/src/test/java/com/baeldung/Lazy/LazyAnnotationUnitTest.java new file mode 100644 index 0000000000..187d573557 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/Lazy/LazyAnnotationUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.Lazy; + +import com.baeldung.lazy.AppConfig; +import com.baeldung.lazy.Country; +import com.baeldung.lazy.Region; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class LazyAnnotationUnitTest { + + @Test + public void givenLazyAnnotation_whenConfigClass_thenLazyAll() { + // Add @Lazy to AppConfig.class while testing + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(AppConfig.class); + ctx.refresh(); + ctx.getBean(Region.class); + ctx.getBean(Country.class); + } + + @Test + public void givenLazyAnnotation_whenAutowire_thenLazyBean() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(AppConfig.class); + ctx.refresh(); + Region region = ctx.getBean(Region.class); + region.getCityInstance(); + } + + @Test + public void givenLazyAnnotation_whenBeanConfig_thenLazyBean() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(AppConfig.class); + ctx.refresh(); + ctx.getBean(Region.class); + } +}