Skip to content

Commit 7eb7416

Browse files
MherBaghinyanpivovarit
authored andcommitted
@lazy annotation (eugenp#3809)
1 parent 4c0a39a commit 7eb7416

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.lazy;
2+
3+
public class Country {
4+
5+
public Country() {
6+
System.out.println("Country bean initialized");
7+
}
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)