JAVA-19545 Move articles from spring-core-2 module to spring-di-3 module (#13708)

* JAVA-19545 Move articles from spring-core-2 module to spring-di-3 module

* JAVA-19545 Move articles from spring-core-4 module to spring-di-3 module
This commit is contained in:
anuragkumawat
2023-03-26 23:51:27 +05:30
committed by GitHub
parent 97961896d7
commit eec8618a3d
41 changed files with 4 additions and 4 deletions

View File

@@ -5,8 +5,6 @@ This module contains articles about core Spring functionality
## Relevant Articles:
- [Creating Spring Beans Through Factory Methods](https://www.baeldung.com/spring-beans-factory-methods)
- [How to dynamically Autowire a Bean in Spring](https://www.baeldung.com/spring-dynamic-autowire)
- [Spring @Import Annotation](https://www.baeldung.com/spring-import-annotation)
- [Spring BeanPostProcessor](https://www.baeldung.com/spring-beanpostprocessor)
- [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class)
- [Constructor Injection in Spring with Lombok](https://www.baeldung.com/spring-injection-lombok)

View File

@@ -1,27 +0,0 @@
package com.baeldung.dynamic.autowire;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BeanFactoryDynamicAutowireService {
private static final String SERVICE_NAME_SUFFIX = "regionService";
private final BeanFactory beanFactory;
@Autowired
public BeanFactoryDynamicAutowireService(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public boolean isServerActive(String isoCountryCode, int serverId) {
RegionService service = beanFactory.getBean(getRegionServiceBeanName(isoCountryCode), RegionService.class);
return service.isServerActive(serverId);
}
private String getRegionServiceBeanName(String isoCountryCode) {
return isoCountryCode + SERVICE_NAME_SUFFIX;
}
}

View File

@@ -1,26 +0,0 @@
package com.baeldung.dynamic.autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class CustomMapFromListDynamicAutowireService {
private final Map<String, RegionService> servicesByCountryCode;
@Autowired
public CustomMapFromListDynamicAutowireService(List<RegionService> regionServices) {
servicesByCountryCode = regionServices.stream()
.collect(Collectors.toMap(RegionService::getISOCountryCode, Function.identity()));
}
public boolean isServerActive(String isoCountryCode, int serverId) {
RegionService service = servicesByCountryCode.get(isoCountryCode);
return service.isServerActive(serverId);
}
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.dynamic.autowire;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.dynamic.autowire")
public class DynamicAutowireConfig {
}

View File

@@ -1,16 +0,0 @@
package com.baeldung.dynamic.autowire;
import org.springframework.stereotype.Service;
@Service("GBregionService")
public class GBRegionService implements RegionService {
@Override
public boolean isServerActive(int serverId) {
return false;
}
@Override
public String getISOCountryCode() {
return "GB";
}
}

View File

@@ -1,7 +0,0 @@
package com.baeldung.dynamic.autowire;
public interface RegionService {
boolean isServerActive(int serverId);
String getISOCountryCode();
}

View File

@@ -1,16 +0,0 @@
package com.baeldung.dynamic.autowire;
import org.springframework.stereotype.Service;
@Service("USregionService")
public class USRegionService implements RegionService {
@Override
public boolean isServerActive(int serverId) {
return true;
}
@Override
public String getISOCountryCode() {
return "US";
}
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ MammalConfiguration.class, BirdConfig.class })
class AnimalConfiguration {
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class AnimalScanConfiguration {
}

View File

@@ -1,4 +0,0 @@
package com.baeldung.importannotation.animal;
class Bird {
}

View File

@@ -1,13 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class BirdConfig {
@Bean
Bird bird() {
return new Bird();
}
}

View File

@@ -1,7 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.stereotype.Component;
@Component(value = "bug")
class Bug {
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(Bug.class)
class BugConfig {
}

View File

@@ -1,4 +0,0 @@
package com.baeldung.importannotation.animal;
class Cat {
}

View File

@@ -1,13 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class CatConfig {
@Bean
Cat cat() {
return new Cat();
}
}

View File

@@ -1,4 +0,0 @@
package com.baeldung.importannotation.animal;
class Dog {
}

View File

@@ -1,13 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class DogConfig {
@Bean
Dog dog() {
return new Dog();
}
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.importannotation.animal;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ DogConfig.class, CatConfig.class })
class MammalConfiguration {
}

View File

@@ -1,11 +0,0 @@
package com.baeldung.importannotation.zoo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.baeldung.importannotation.animal.AnimalScanConfiguration;
@Configuration
@Import(AnimalScanConfiguration.class)
class ZooApplication {
}

View File

@@ -1,33 +0,0 @@
package com.baeldung.dynamic.autowire;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DynamicAutowireConfig.class)
public class DynamicAutowireIntegrationTest {
@Autowired
private BeanFactoryDynamicAutowireService beanFactoryDynamicAutowireService;
@Autowired
private CustomMapFromListDynamicAutowireService customMapFromListDynamicAutowireService;
@Test
public void givenDynamicallyAutowiredBean_whenCheckingServerInGB_thenServerIsNotActive() {
assertThat(beanFactoryDynamicAutowireService.isServerActive("GB", 101), is(false));
assertThat(customMapFromListDynamicAutowireService.isServerActive("GB", 101), is(false));
}
@Test
public void givenDynamicallyAutowiredBean_whenCheckingServerInUS_thenServerIsActive() {
assertThat(beanFactoryDynamicAutowireService.isServerActive("US", 101), is(true));
assertThat(customMapFromListDynamicAutowireService.isServerActive("US", 101), is(true));
}
}

View File

@@ -1,31 +0,0 @@
package com.baeldung.importannotation.animal;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { AnimalConfiguration.class })
class AnimalConfigUnitTest {
@Autowired
ApplicationContext context;
@Test
void givenImportedBeans_whenGettingEach_shallFindOnlyTheImportedBeans() {
assertThatBeanExists("dog", Dog.class);
assertThatBeanExists("cat", Cat.class);
assertThatBeanExists("bird", Cat.class);
}
private void assertThatBeanExists(String beanName, Class<?> beanClass) {
assertTrue(context.containsBean(beanName));
assertNotNull(context.getBean(beanClass));
}
}

View File

@@ -1,25 +0,0 @@
package com.baeldung.importannotation.animal;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = BugConfig.class)
class BugConfigUnitTest {
@Autowired
ApplicationContext context;
@Test
void givenImportInComponent_whenLookForBean_shallFindIt() {
assertTrue(context.containsBean("bug"));
assertNotNull(context.getBean(Bug.class));
}
}

View File

@@ -1,31 +0,0 @@
package com.baeldung.importannotation.animal;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { BirdConfig.class, CatConfig.class, DogConfig.class })
class ConfigUnitTest {
@Autowired
ApplicationContext context;
@Test
void givenImportedBeans_whenGettingEach_shallFindIt() {
assertThatBeanExists("dog", Dog.class);
assertThatBeanExists("cat", Cat.class);
assertThatBeanExists("bird", Bird.class);
}
private void assertThatBeanExists(String beanName, Class<?> beanClass) {
assertTrue(context.containsBean(beanName));
assertNotNull(context.getBean(beanClass));
}
}

View File

@@ -1,33 +0,0 @@
package com.baeldung.importannotation.animal;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { MammalConfiguration.class })
class MammalConfigUnitTest {
@Autowired
ApplicationContext context;
@Test
void givenImportedBeans_whenGettingEach_shallFindOnlyTheImportedBeans() {
assertThatBeanExists("dog", Dog.class);
assertThatBeanExists("cat", Cat.class);
assertFalse(context.containsBean("bird"));
}
private void assertThatBeanExists(String beanName, Class<?> beanClass) {
assertTrue(context.containsBean(beanName));
assertNotNull(context.getBean(beanClass));
}
}

View File

@@ -1,26 +0,0 @@
package com.baeldung.importannotation.zoo;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ZooApplication.class)
class ZooApplicationUnitTest {
@Autowired
ApplicationContext context;
@Test
void givenTheScanInTheAnimalPackage_whenGettingAnyAnimal_shallFindItInTheContext() {
assertNotNull(context.getBean("dog"));
assertNotNull(context.getBean("bird"));
assertNotNull(context.getBean("cat"));
assertNotNull(context.getBean("bug"));
}
}