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:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
public interface RegionService {
|
||||
boolean isServerActive(int serverId);
|
||||
|
||||
String getISOCountryCode();
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
class Bird {
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "bug")
|
||||
class Bug {
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
class Cat {
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
class Dog {
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user