From 7eb74160f1bd95b8c817b46e087e2267b8aabf0b Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Sat, 17 Mar 2018 13:30:44 +0400 Subject: [PATCH] @Lazy annotation (#3809) --- .../java/com/baeldung/lazy/AppConfig.java | 23 ++++++++++++ .../src/main/java/com/baeldung/lazy/City.java | 13 +++++++ .../main/java/com/baeldung/lazy/Country.java | 8 ++++ .../main/java/com/baeldung/lazy/Region.java | 19 ++++++++++ .../baeldung/Lazy/LazyAnnotationUnitTest.java | 37 +++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/lazy/AppConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/lazy/City.java create mode 100644 spring-core/src/main/java/com/baeldung/lazy/Country.java create mode 100644 spring-core/src/main/java/com/baeldung/lazy/Region.java create mode 100644 spring-core/src/test/java/com/baeldung/Lazy/LazyAnnotationUnitTest.java 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); + } +}