@Lazy annotation (#3809)
This commit is contained in:
parent
4c0a39a342
commit
7eb74160f1
23
spring-core/src/main/java/com/baeldung/lazy/AppConfig.java
Normal file
23
spring-core/src/main/java/com/baeldung/lazy/AppConfig.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
13
spring-core/src/main/java/com/baeldung/lazy/City.java
Normal file
13
spring-core/src/main/java/com/baeldung/lazy/City.java
Normal file
@ -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");
|
||||||
|
}
|
||||||
|
}
|
8
spring-core/src/main/java/com/baeldung/lazy/Country.java
Normal file
8
spring-core/src/main/java/com/baeldung/lazy/Country.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.lazy;
|
||||||
|
|
||||||
|
public class Country {
|
||||||
|
|
||||||
|
public Country() {
|
||||||
|
System.out.println("Country bean initialized");
|
||||||
|
}
|
||||||
|
}
|
19
spring-core/src/main/java/com/baeldung/lazy/Region.java
Normal file
19
spring-core/src/main/java/com/baeldung/lazy/Region.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user