File tree Expand file tree Collapse file tree
main/java/com/baeldung/lazy
test/java/com/baeldung/Lazy Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .lazy ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .ComponentScan ;
5+ import org .springframework .context .annotation .Configuration ;
6+ import org .springframework .context .annotation .Lazy ;
7+
8+ @ Lazy
9+ @ Configuration
10+ @ ComponentScan (basePackages = "com.baeldung.lazy" )
11+ public class AppConfig {
12+
13+ @ Lazy
14+ @ Bean
15+ public Region getRegion (){
16+ return new Region ();
17+ }
18+
19+ @ Bean
20+ public Country getCountry (){
21+ return new Country ();
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .lazy ;
2+
3+ import org .springframework .context .annotation .Lazy ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Lazy
7+ @ Component
8+ public class City {
9+
10+ public City () {
11+ System .out .println ("City bean initialized" );
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .lazy ;
2+
3+ public class Country {
4+
5+ public Country () {
6+ System .out .println ("Country bean initialized" );
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .lazy ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .context .annotation .Lazy ;
5+
6+ public class Region {
7+
8+ @ Lazy
9+ @ Autowired
10+ private City city ;
11+
12+ public Region () {
13+ System .out .println ("Region bean initialized" );
14+ }
15+
16+ public City getCityInstance () {
17+ return city ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .Lazy ;
2+
3+ import com .baeldung .lazy .AppConfig ;
4+ import com .baeldung .lazy .Country ;
5+ import com .baeldung .lazy .Region ;
6+ import org .junit .Test ;
7+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
8+
9+ public class LazyAnnotationUnitTest {
10+
11+ @ Test
12+ public void givenLazyAnnotation_whenConfigClass_thenLazyAll () {
13+ // Add @Lazy to AppConfig.class while testing
14+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
15+ ctx .register (AppConfig .class );
16+ ctx .refresh ();
17+ ctx .getBean (Region .class );
18+ ctx .getBean (Country .class );
19+ }
20+
21+ @ Test
22+ public void givenLazyAnnotation_whenAutowire_thenLazyBean () {
23+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
24+ ctx .register (AppConfig .class );
25+ ctx .refresh ();
26+ Region region = ctx .getBean (Region .class );
27+ region .getCityInstance ();
28+ }
29+
30+ @ Test
31+ public void givenLazyAnnotation_whenBeanConfig_thenLazyBean () {
32+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
33+ ctx .register (AppConfig .class );
34+ ctx .refresh ();
35+ ctx .getBean (Region .class );
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments