[JAVA-9344] Split spring-core module

This commit is contained in:
Haroon Khan
2022-01-07 22:53:16 +00:00
parent d0245b9ec3
commit 2ddcbab63f
7 changed files with 6 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
## Spring Core
This module contains articles about core Spring functionality
This module contains articles about core Spring functionality.
## Relevant Articles:
@@ -8,4 +8,5 @@ This module contains articles about core Spring functionality
- [Solving Springs “not eligible for auto-proxying” Warning](https://www.baeldung.com/spring-not-eligible-for-auto-proxying)
- [Spring Bean Names](https://www.baeldung.com/spring-bean-names)
- [AliasFor Annotation in Spring](https://www.baeldung.com/spring-aliasfor-annotation)
- More articles: [[<-- prev]](/spring-core-4)
- [A Quick Guide to the Spring @Lazy Annotation](https://www.baeldung.com/spring-lazy-annotation)
- More articles: [[<-- prev]](../spring-core-4)

View 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();
}
}

View 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");
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.lazy;
public class Country {
public Country() {
System.out.println("Country bean initialized");
}
}

View 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;
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.lazy;
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);
}
}